在While循环中多次循环?

时间:2011-04-20 12:58:18

标签: php mysql while-loop

总新手试图学习......感谢迄今为止的所有帮助!

更新 - 下面更新的代码 - 谢谢Phill

我现在正试图从表horses获取相关的马信息,我可以使用horse_name字段加入race_form。然后我想在while($ racecard)信息下显示比赛中每匹马的信息。这会让它更清晰吗?

我以为我可以做另一个查询,然后使用mysql_fetch_array进行一个while循环,我将已经显示它然后进入下一场比赛,但显然不起作用......?!< / p>

我想知道你是否可以在while循环中相继运行2个while循环?我正在制作一个赛马表格 - 我可以显示每个比赛名单,但在下面我想在比赛中显示每匹马的单独马匹细节。无论如何,如果你看这个页面,你可以看到我正在尝试做什么:

http://tasmanianracing.com/superform/formguide.php?meetingID=21042011LAUN&Submit=View+Form

问题是,任何时候我在比赛名单之后放置第二个while循环,它都不会出现。我正在尝试运行一个查询,以便从我的马桌上获取马的细节,然后在比赛中为每匹马跑一段时间,但没有任何出现。

我希望这很清楚......我的代码片段如下:如果我错过了一些重要内容,请告诉我:


echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";

$racedetails = mysql_query("SELECT * 
    FROM race_info
    WHERE meetingID = ('" . $safemeetingID . "')");

while($row = mysql_fetch_array($racedetails))
{
    echo "<tr><td>Race " . $row['race_number'] . " at " . $row['start_time'] . " " . $row['class'] . " over " . $row['distance'] . "m</td></tr>";

    $racecard = mysql_query("SELECT *
        FROM race_form
        INNER JOIN race_info ON race_form.raceID = race_info.raceID
        WHERE race_form.raceID = ('" . $row['raceID'] . "')");

    while($row = mysql_fetch_array($racecard))
    {
        echo "<tr><td>" . $row['number'] . "</td><td>" . $row['past10'] . "</td><td>" . $row['horse_name'] . "</td></tr>";
    }

    echo "</table><br>";

    echo "<br>I wish I could put in some horse form here...<br>";

    echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";

}

echo "</table>";

3 个答案:

答案 0 :(得分:4)

您可能需要更改某些$row变量的名称,它们可能会相互干扰。

例如:

while($row_races = mysql_fetch_array($totalraces)){
while($row_details = mysql_fetch_array($racedetails)){
while($row_card = mysql_fetch_array($racecard)){

答案 1 :(得分:1)

我理论上你可以并且在实践中可以,但是while while for中的while似乎有点超过顶部...

如果您解释一下您正在尝试做什么,我相信我们可以帮助您提高效率。

答案 2 :(得分:1)

我认为你可以摆脱一个问题:

第一个查询通过选择COUNT获得比赛次数,这将返回一个带有计数值的记录。

// This returns one record with the count
$total_races    = "SELECT COUNT(race_number) AS totalraces 
                   FROM race_info WHERE meetingID = ('".$safemeetingID."') ";

接下来,迭代相同的记录,因为为比赛详细信息返回的行与计数相同。

// This looks to return the record(s) with the race details
$race_details   = "SELECT * FROM race_info 
                    WHERE meetingID = ('" . $safemeetingID . "')";

我认为您可以使用它来获得所需的结果:(我同意将$ row变量重命名为每个while循环的描述)

$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");

while($details_row = mysql_fetch_array($racedetails))
{
    echo "<tr><td>Race " . $details_row['race_number'] . " at " . $details_row['start_time'] . " " . $details_row['class'] . " over " . $details_row['distance'] . "m</td></tr>";

    $racecard = mysql_query("SELECT *
    FROM race_form
    INNER JOIN race_info ON race_form.raceID = race_info.raceID
    WHERE race_form.raceID = ('" . $details_row['raceID'] . "')");

    while($rc_row = mysql_fetch_array($racecard))
    {
    echo "<tr><td>" . $rc_row['number'] . "</td><td>" . $rc_row['past10'] . "</td><td>" . $rc_row['horse_name'] . "</td></tr>";
    }

    echo "</table><br>";

    echo "Testing<br>Testing<br>I wish I could put in some horse form here...<br>";

    echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";

}

未经测试/伪造的代码

"SELECT * 
FROM horses AS h, 
INNER JOIN race_info  AS ri ON race_form.raceID = race_info.raceID
WHERE horse_name IN (
    SELECT horse_name
    FROM race_form AS srf
    WHERE h.horse_name = srf.horse_name
)
AND race_form.raceID = ('" . $details_row['raceID'] . "')"

我的想法是将两个查询合并为一个,我知道这不是正确的语法,但它可能会让你知道如何去做。

或者你可以在为马名称循环时进行另一个查询

$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");

while($details_row = mysql_fetch_array($racedetails))
{
    echo "<tr><td>Race " . $details_row['race_number'] . " at " . $details_row['start_time'] . " " . $details_row['class'] . " over " . $details_row['distance'] . "m</td></tr>";

    $racecard = mysql_query("SELECT *
    FROM race_form
    INNER JOIN race_info ON race_form.raceID = race_info.raceID
    WHERE race_form.raceID = ('" . $details_row['raceID'] . "')");

    while($rc_row = mysql_fetch_array($racecard))
    {
        echo "<tr><td>" . $rc_row['number'] . "</td><td>" . $rc_row['past10'] . "</td><td>" . $rc_row['horse_name'] . "</td></tr>";

        $horses = mysql_query("SELECT *
        FROM horses
        WHERE horse_name = ('" . $rc_row['horse_name'] . "')");

        while($horse_row = mysql_fetch_array($horses))
        {
            // echo horse details here
        }
    }

    echo "</table><br>";

    echo "Testing<br>Testing<br>I wish I could put in some horse form here...<br>";

    echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";

}