【问题标题】:PHP nested while loop missing iterationPHP嵌套while循环缺少迭代
【发布时间】:2012-06-05 18:47:21
【问题描述】:

转到此问题的底部以获得答案

我目前正在制作一个表格输出,它基本上会逐行列出用户。表输出中的列将由一个特定的 SQL 列填充,但来自多行(例如 John Smith 有 4 行,但 X 列在此表的每一行中具有不同的值)。

为了给您更多上下文,这是当前表格状态的文本表示。

Name  |  Col1  |  Col2  |  Col3
---------------------------------
Name1 |   0    |   X    |   1

Name2 |   3    |   2    |   <--- Value missing

Name3 |   2    |   1    |   <--- Value missing (and this continues through the table..

正如您在该表上看到的那样,第一行填充良好 - 但之后的其余行似乎忽略了数据的迭代(因此其余行仅填充 2 列。

循环的相关代码如下:

while ($row = mysql_fetch_assoc($result)) {

    echo "<tr><td>". $row['forename'] . "</td>
      <td>". $row['status'] ."</td>";

             while ($col = mysql_fetch_assoc($result)) {

                 if ($col['id'] == $row['id']) {

                     echo "<td>" . $col['status'] . "</td>";

                 }

                 else if ($col['id'] != $row['id']){
                     echo "</tr>";
                     break;
                 }
             }
}

有没有人知道为什么会发生这种情况?我希望我已经提供了足够的信息,但如果没有,请告诉我! :)

【问题讨论】:

  • 您在mysql_fetch_assoc() 的两个实例中都使用了$result。你不应该在不同的结果集上循环吗?
  • 所有数据都来自一个查询,尽管该查询已将两个表连接在一起。我要做的就是说:John Smith 有 3 行,而我使用的列的值是 1、2、3。然后下一个用户说:Alan Man 也有 3 行,并且每行也有 3 个不同的值(但相同的列)。我只希望用户名一次,但我希望将该用户的每一行中的 3 个不同值设置为我表中的列值。
  • 您确定查询返回的数据正确吗?
  • 好吧,当我在 SQL 中运行查询以按原样获取表时,返回的数据是正确的。就在第一行之后,它似乎错过了第一个状态值 - 但之后的其他 2 个与表查询中的相同。

标签: php sql loops nested while-loop


【解决方案1】:
if ($col['id'] == $row['id'])
{
     make a td
}

我认为问题在于有时 $col['id'] != $row['id']

【讨论】:

  • 感谢您的回答,但您能否详细说明您的意思?
【解决方案2】:

通过查看您的代码,我可以提出调整建议。

通过在内部 WHILE 循环中调用 mysql_fetch_assoc($result),您正在移动结果对象中的指针(尽管不是有意的)。

您应该改用在第一个循环中检索到的 $row 数组。

如果您真的需要像我看到的那样进行比较,最好的办法是遍历整个结果集,首先将单个记录放入一个数组中,然后您可以在循环中使用,如下所示:

$records = array();
//array to hold the retrieved records from the database
while($row = mysql_fetch_assoc($result)){
    //loop through the data
    $records[] = $row;
}
//continue with your processing, this time using $records as your result set
//an looping through it this time as an array -- you don't need mysql_fetch_assoc
.....

祝你好运!

【讨论】:

  • 对,是的,我明白你的意思。我稍后会试一试,看看结果如何。干杯:)
  • 谢谢 Okeke 我用你的大纲尝试了一种新方法,并且效果很好! :D
  • 那你真的应该奖励他“适当的答案”分
【解决方案3】:

扩展 Okekes 的想法(干杯伙伴)我修改了另一种方法,现在效果很好!

//declare new array variable
                $resultSet[] = array();

                //set offset integer
                $i = 0;

                //While loop to return all rows from query, and place into multi dimensional array format
                while ($row = mysql_fetch_assoc($result)) {
                    $resultSet[$i] = $row;
                    $i++;
                }

                //check that the array isn't empty
                if ($resultSet) {

                    $innerCount = "0";

                    //count how many rows are in the array
                    $rowCount = count($resultSet);

                    //loop through the array, each row, then go through inner loop for columns
                    for ($row = 0; $row < $rowCount; $row=$row+$numRows) {

                        //declare variables for the row data
                        $foreName = $resultSet[$row]['forename'];
                        $surName = $resultSet[$row]['surname'];

                        echo "<tr><td>" . $foreName . " " . $surName . "</td>";

                        for ($col = $row; $col < $rowCount; $col++) {

                            if ($innerCount < $numRows) {

                            $innerCount++;
                            $currStatus = $resultSet[$col]['status'];
                            echo "<td>" . $currStatus . "</td>";

                            }

                            else {

                                echo "</tr>";
                                $innerCount = "0";
                                break;
                            }

                        }
                    }
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 2014-10-22
    • 2018-05-14
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多