Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.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嵌套while循环缺少迭代 到这个问题的底部去寻找答案_Php_Sql_Loops_Nested_While Loop - Fatal编程技术网

PHP嵌套while循环缺少迭代 到这个问题的底部去寻找答案

PHP嵌套while循环缺少迭代 到这个问题的底部去寻找答案,php,sql,loops,nested,while-loop,Php,Sql,Loops,Nested,While Loop,我目前正在制作一个表输出,它基本上会逐行列出用户。表输出上的列将由一个特定的SQL列填充,但来自多行(例如John Smith有4行,但此表中每行的X列值不同) 为了提供更多上下文,这是当前表状态的文本表示 Name | Col1 | Col2 | Col3 --------------------------------- Name1 | 0 | X | 1 Name2 | 3 | 2 | <--- Value missing

我目前正在制作一个表输出,它基本上会逐行列出用户。表输出上的列将由一个特定的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..
Name | Col1 | Col2 | Col3
---------------------------------
名称1 | 0 | X | 1
名称2 | 3 | 2 |

我假设问题是有时$col['id']!=$行['id']

通过查看您的代码,我可以建议进行调整

通过调用内部
WHILE
循环中的
mysql\u fetch\u assoc($result)
,可以移动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
.....
祝你好运

扩展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;
                            }

                        }
                    }
                }
//声明新的数组变量
$resultSet[]=array();
//设置偏移整数
$i=0;
//While循环返回查询中的所有行,并放入多维数组格式
while($row=mysql\u fetch\u assoc($result)){
$resultSet[$i]=$row;
$i++;
}
//检查数组是否为空
如果($resultSet){
$innerCount=“0”;
//计算数组中有多少行
$rowCount=count($resultSet);
//循环遍历数组中的每一行,然后遍历列的内部循环
对于($row=0;$row<$rowCount;$row=$row+$numRows){
//为行数据声明变量
$foreName=$resultSet[$row]['foreName'];
$LANSAME=$resultSet[$row]['LANSAME'];
呼应“$foreName.”“$name.”;
对于($col=$row;$col<$rowCount;$col++){
如果($innerCount<$numRows){
$innerCount++;
$currStatus=$resultSet[$col]['status'];
回显“$currStatus.”;
}
否则{
回声“;
$innerCount=“0”;
打破
}
}
}
}

您在
mysql\u fetch\u assoc()的两个实例中都使用了
$result
。您不应该在不同的结果集上循环吗?所有数据都来自一个将两个表连接在一起的查询。我想说的是:John Smith有3行,我使用的列的值是1,2,3。然后是下一个用户,比如:Alan Man也有3行,并且每行有3个不同的值(但是相同的列)。我只希望用户名出现一次,但我希望该用户的每一行中的3个不同值设置为表中的列值。您确定查询返回的数据正确吗?当我在SQL中运行查询以获取表的原样时,返回的数据是即时的。就在第一行之后,它似乎错过了第一个状态值-但是后面的其他2个与表查询中的相同。谢谢你的回答,但是你能详细解释一下你的意思吗?是的,我明白你的意思。我稍后会试一试,看看结果如何。干杯:)谢谢Okeke,我用你的提纲用了一种新方法,效果很好!:当你真的应该给他“适当答案”的分数时
$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
.....
//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;
                            }

                        }
                    }
                }