Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php Foreach循环仅显示1个值,而不是所有不同的值_Php_Loops_Foreach - Fatal编程技术网

php Foreach循环仅显示1个值,而不是所有不同的值

php Foreach循环仅显示1个值,而不是所有不同的值,php,loops,foreach,Php,Loops,Foreach,我的foreach循环不会循环数据库中的所有结果。它只显示最后一行,并且显示xx乘以总行数 我的代码: $query = db_query('SELECT * FROM push_notifications_messages WHERE msg_appID = :app_id', array(':app_id' => $id)); $qCount = db_query('SELECT * FROM push_notifications_messages WHERE msg_appID =

我的foreach循环不会循环数据库中的所有结果。它只显示最后一行,并且显示xx乘以总行数

我的代码:

$query = db_query('SELECT * FROM push_notifications_messages WHERE msg_appID = :app_id', array(':app_id' => $id));
$qCount = db_query('SELECT * FROM push_notifications_messages WHERE msg_appID = :app_id', array(':app_id' => $id))->rowCount();

$form['table'] = array(
    '#theme' => 'table',
    '#header' => array(t('Message'), t('Device'), t('Date')),
    'rows' => array(),
    );

foreach($query as $result) {
  for($i = 1; $i <= $qCount; $i++) {
    $form['table']["#rows"]["'r$i'"] = array(
        'c1' => array(
            'data' => array('#type' => 'item', '#markup' => t('@message', array('@message' => $result->msg_message))),
          ),
        'c2' => array(
            'data' => array('#type' => 'item', '#markup' => t('@message', array('@message' => $result->msg_device))),
          ),
        'c3' => array(
            'data' => array('#type' => 'item', '#markup' => t('@message', array('@message' => date("d F Y H:i:s", $result->msg_timestamp)))),
          ),      
      );
  }
}
数据库截图和foreach循环结果:

提前谢谢

您正在为从数据库获取的每一行运行for循环$qCount time。因此,它将最后一行分配给所有键。这可以在没有for循环的情况下完成。试试-

foreach($query as $key => $result) {
$form['table']["#rows"]["r".$key] = array(
    'c1' => array(
        'data' => array('#type' => 'item', '#markup' => t('@message', array('@message' => $result->msg_message))),
      ),
    'c2' => array(
        'data' => array('#type' => 'item', '#markup' => t('@message', array('@message' => $result->msg_device))),
      ),
    'c3' => array(
        'data' => array('#type' => 'item', '#markup' => t('@message', array('@message' => date("d F Y H:i:s", $result->msg_timestamp)))),
      ),      
  );
}

为什么要迭代foreach和for循环?仅仅使用foreach不合适吗?$form如何知道要渲染多少行?它现在说的部分是:r$i@user3428971foreach将循环查找所有记录。OP为什么要尝试此操作?请添加一个解释,说明你做了什么,以及你为什么这样做,不仅是为了OP,也是为了SO的未来访客。更新了答案@Rizier123谢谢你的回答和解释。现在很清楚: