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 在嵌套循环中引用外部循环变量_Php_Loops_While Loop_Nested Loops - Fatal编程技术网

Php 在嵌套循环中引用外部循环变量

Php 在嵌套循环中引用外部循环变量,php,loops,while-loop,nested-loops,Php,Loops,While Loop,Nested Loops,我希望下面的代码在两个不同的mysql表中循环。外部while循环应该打印产品列表,每个表行都是一个新产品。内部while循环应该打印该产品中的每个列变量。内部循环设置为查看$tableheaders,以获得打印列的指导 内部循环是用外部循环数组中的内容回显表单元格,内部循环数组指定它应该查看外部数组中的哪一列 这是我的密码 $tableheaders=mysql_query("SELECT * from `winetable`"); $products=mysql_query("SELECT

我希望下面的代码在两个不同的mysql表中循环。外部while循环应该打印产品列表,每个表行都是一个新产品。内部while循环应该打印该产品中的每个列变量。内部循环设置为查看$tableheaders,以获得打印列的指导

内部循环是用外部循环数组中的内容回显表单元格,内部循环数组指定它应该查看外部数组中的哪一列

这是我的密码

$tableheaders=mysql_query("SELECT * from `winetable`");

$products=mysql_query("SELECT * FROM `wines`");

while ($row=mysql_fetch_assoc($products)){
    echo '<tr>';
            while ($rowi=mysql_fetch_assoc($tableheaders)){
            $column=$rowi['Title'];
            echo '<td>';
            echo $row[$column];
            echo '</td>';
        }
    echo '</tr>';
    }
它所做的一切都是空白的


现在不使用mySQLi,因为我的WebMatrix在使用它时遇到了一些问题。我的MySQL安装已损坏,我想:

请阅读以下代码,看看我将如何调试您的代码。我还意识到,您需要使用mysql\u data\u seek重置一个阵列:

$tableheaders=mysql_query("SELECT * from `winetable`");
$products=mysql_query("SELECT * FROM `wines`");

//check the number of results before looping
if(mysql_num_rows($tableheaders) > 0 && mysql_num_rows($products) > 0){

    while ($row=mysql_fetch_assoc($products)){
        echo '<tr>';

        //check what is in the array
        var_dump($row);

        while ($rowi=mysql_fetch_assoc($tableheaders)){

            //check what is in the array
            var_dump($rowi);

            $column=$rowi['Title'];
            echo '<td>';
            echo $row[$column];
            echo '</td>';
        }
        echo '</tr>';

        //reset the $tableheaders array pointer back to 0 for next loop
        mysql_data_seek($tableheaders, 0);
    }
}
试试这样的。 这样做,您首先将结果加载到一个数组中,您可以进一步操作该数组并在任何时候重用它

<?php

$t = mysql_query("SELECT * FROM `winetable`");
$p = mysql_query("SELECT * FROM `wines`");

if(mysql_num_rows($p) > 0 && mysql_num_rows($t) > 0){
    while ($row=mysql_fetch_assoc($p)){
        $products[] = $row
    }

    while ($row=mysql_fetch_assoc($t)){
        $tableheaders[] = $row;
    }

    foreach($products as $i => $product){ ?>

    <tr><?php
    foreach($tableheaders as $j => $tableheader){
        ?>

        <td><?php echo $row[$tableheader['Title']]; ?></td><?php
    }   
    ?>

    </tr><?php
    }
}

?>

看看你的代码,我注意到你没有或者包括你可能有,但这并没有显示在你的问题。能这么容易吗?我想有些浏览器ie在没有这些标签时不会显示表

包括

echo '<table>';

当然,正如西奥·库泽利斯(Theo Kouzelis)所提到的,不要使用mysql_函数。

我建议使用mysql函数,因为mysql会贬值。除此之外,您的代码看起来还不错,请尝试在$row和$rowi上运行var_dump,以查看返回的内容。可能值得向您展示MySQL stations和var dumps,以获得更多关于您的问题的帮助。var_dump在这两个方面都返回boolfalse…您是否在while循环中执行var dumps?如果是这样,请尝试在while循环之前在$tableheaders和$products上运行mysqli_num_行。如果返回0,您就知道您的DB表是空的,或者您的MySQL表名是错误的。如果它们大于0,那么您的var转储就错了。我已经尝试并打印了MySQL表中的数组,但是它们都显示得很好……非常感谢!正是$tableheaders的重置起到了关键作用:欢迎使用SO,但为了每个人的利益:请不要在您的答案中使用。。。查看链接,注意页面顶部的红色框,并查看PDO和mysqli_*
echo '</table>';
SELECT * FROM `wines` w LEFT JOIN `winetable` wt ON (w.id = wt.id)