Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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中使用嵌套for循环打印多个表_Php - Fatal编程技术网

在PHP中使用嵌套for循环打印多个表

在PHP中使用嵌套for循环打印多个表,php,Php,我是PHP新手。我在使用嵌套for循环打印多个表时遇到了一些问题。因此,在当前代码中,我有一个嵌套的for循环,它只打印出一个表,这与我取出外部for循环时得到的相同。数组中有一些硬编码数据,我希望嵌套for循环能够打印出4个相同的表。出于某种原因,基于我所拥有的,这是不起作用的,我似乎不明白为什么不起作用 最终,我们将从数据库中提取硬编码数据,而不是硬编码数据。我们将不得不从一个数据库中提取,该数据库需要根据会议和我当前的逻辑正确填充订单。我想知道,根据我自己的指导方式,这是否仍然是一种可能性

我是PHP新手。我在使用嵌套for循环打印多个表时遇到了一些问题。因此,在当前代码中,我有一个嵌套的for循环,它只打印出一个表,这与我取出外部for循环时得到的相同。数组中有一些硬编码数据,我希望嵌套for循环能够打印出4个相同的表。出于某种原因,基于我所拥有的,这是不起作用的,我似乎不明白为什么不起作用

最终,我们将从数据库中提取硬编码数据,而不是硬编码数据。我们将不得不从一个数据库中提取,该数据库需要根据会议和我当前的逻辑正确填充订单。我想知道,根据我自己的指导方式,这是否仍然是一种可能性。我真的想弄明白这一点,如果能解释一下为什么它不起作用,我将不胜感激。提前谢谢大家

PHP代码:

<?php print( '<?xml version = "1.0" encoding = "utf-8"?>') ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>User selection page</title>
    </head>


    <?php
        /*
        Version 1.1: Instead of printing the teams in different cells we are going to print the 
        games in one row and we select the game itself to see if an upset will occur.
        */  

        require_once('Conference.php'); 

        for ($i = 0; $i<4; $i++)
        {       
            $loadGameClass = new Conference();
            $loadGameClass->loadTeams(array("(1)Gonzaga vs (16)Southern U", "(8)Pittsburgh vs (9)Wichita St", "(5)Wisconsin vs (12)Ole Miss", "(4)Kansas st vs (13)Boise St", "(6)Arizona vs (11)Belmont", "(3)New Mexico vs (14) Harvard", "(7)Notre Dame vs (10)Iowa St", "(2)Ohio St vs (15) Iona"));
            $teams = $loadGameClass->getTeams();

            echo '<table border="1">';

            for ($i = 0; $i < 8; $i++) 
            {
                $highSeed = $teams[$i];
                echo '<tr><td>'.$highSeed.'</td><tr>';
            }

            echo '</table>';
            echo "\n";
        }
    ?>

    <body>
    </body>
</html>

用户选择页

在内部和外部循环中使用
$i
。只需将内部循环迭代器替换为
$j
,代码即可正常工作

此代码对您来说应该可以正常工作:

<?php print( '<?xml version = "1.0" encoding = "utf-8"?>') ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>User selection page</title>
    </head>
<body>
<?php
//added the php in the body tag because you can't add content between </head> and <body>
            /*
        Version 1.1: Instead of printing the teams in different cells we are going to print the 
        games in one row and we select the game itself to see if an upset will occur.
        */  

        require_once('Conference.php'); 

        for ($i = 0; $i<4; $i++)
        {       
            $loadGameClass = new Conference();
            $loadGameClass->loadTeams(array("(1)Gonzaga vs (16)Southern U", "(8)Pittsburgh vs (9)Wichita St", "(5)Wisconsin vs (12)Ole Miss", "(4)Kansas st vs (13)Boise St", "(6)Arizona vs (11)Belmont", "(3)New Mexico vs (14) Harvard", "(7)Notre Dame vs (10)Iowa St", "(2)Ohio St vs (15) Iona"));
            $teams = $loadGameClass->getTeams();

            echo '<table border="1">';

            for ($x = 0; $x < 8; $x++) //replaced $i by $x
            {
                $highSeed = $teams[$x];//replaced $i by $x
                echo '<tr><td>'.$highSeed.'</td><tr>';
            }

            echo '</table>';
            echo "\n";
        }
    ?>
</body>
</html>
你的内环应该是:

while($row = mysql_fetch_array($loadGame)){
    print '<tr><td>'.$row['match_text'].'</td></tr>';
}

这就是您要找的吗?

谢谢!这很有效,我大概扫描了100次代码,但我没有注意到那个小错误谢谢你的回答!我不太清楚你拉数据库的逻辑是如何工作的。这里有一个链接,你可以看到有4个会议。最外层的游戏是我现在正在处理的。我想把每一次会议打印在一张8个游戏的表格中。我们必须正确地提取数据,并按照从上到下的顺序排列,就像每次会议在表中括号中显示的那样。我只是想看看这在逻辑上是否可以用PHP实现,因为我以前从未用MySQL和PHP做过任何事情。
while($row = mysql_fetch_array($loadGame)){
    print '<tr><td>'.$row['match_text'].'</td></tr>';
}
Teams:
id:int primary key ai
match_text:string
conferenced:int