简体   繁体   English

While循环中有多个While循环?

[英]Multiple While Loops within a While Loop?

Total newbie trying to learn... thanks for all the help so far! 尝试学习的新手总数...谢谢您到目前为止的所有帮助!

UPDATE - updated code below - Thanks Phill 更新-下面的更新代码-感谢Phill

I'm now trying to get the relevant horse information from a table horses which I can join to race_form with the horse_name field. 现在我想从一个表中的相关信息马horses ,我可以加入race_form与horse_name领域。 I want to then display the information for each horse in the race below the while($racecard) info. 然后,我想在while($ racecard)信息下方显示比赛中每匹马的信息。 Does this make it a bit clearer? 这是否使它更清晰?

I thought I could just do another query and then a while loop with mysql_fetch_array below the one I have already and that would display it and then move onto the next race, but that apparently doesn't work...?! 我以为我可以再进行一次查询,然后在一个已经存在的查询下面的mysql_fetch_array下进行一会儿循环,这将显示该查询,然后进入下一轮竞赛,但这显然行不通...?!

I'm wondering if you can run 2 while loops after each other inside a while loop? 我想知道您是否可以在while循环内互相运行2个while循环? I am working on a horse racing formguide - I can display each race list, but underneath I want to display individual horse details on each horse in the race. 我正在制作赛马指南-我可以显示每个比赛的清单,但是在下面我想显示比赛中每匹马的个人赛马细节。 Anyway if you look at this page you can see what I'm trying to do: 无论如何,如果您查看此页面,则可以看到我正在尝试做的事情:

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

Problem is, any time I put a second while loop after the race list, it won't show up. 问题是,每当我在比赛清单之后放置第二个while循环时,它都不会显示。 I'm trying to run a query to get the horse details from my horse table and then run a while for each horse in the race, but nothing shows up. 我正在尝试运行查询以从我的赛马表中获取赛马详细信息,然后为比赛中的每匹马运行一段时间,但没有任何反应。

I hope this is clear enough ... my snippet of code is below - please let me know if I've missed out something important: 我希望这已经足够清楚了……我的代码段如下-如果我错过了一些重要的事情,请告诉我:


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>";

You'll probably need to change the names of some of the $row variables, they may interfere with each other. 您可能需要更改某些$row变量的名称,它们可能会相互干扰。

For example: 例如:

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

I theory you could and in practice you can, but a while in a while in a for in a while seems a little bit over the top... 我理论上可以在实践中就可以了,但whilewhile在一个forwhile ,似乎有点过顶...

I'm sure we can help you make it more efficient if you explain what it is you're trying to do. 我敢肯定,如果您解释您要做什么,我们可以帮助您提高效率。

I think you can get rid of one of your queries: 我认为您可以摆脱以下疑问之一:

The first query gets the number of races by selecting a COUNT, This returns one record with the count value. 第一个查询通过选择一个COUNT获得比赛数,这将返回一个带有计数值的记录。

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

Next you iterate over the the same records as the rows returned for the race details are the same as the count. 接下来,您要遍历相同的记录,因为竞赛详细信息返回的行与计数相同。

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

I think you can just use this to get the desired results: (I agree to rename the $row variable(s) to something descriptive for each while loop) 我认为您可以使用它来获得所需的结果:(我同意将$ 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'>";

}

NOT TESTED/PSEUDO CODE 未测试/伪代码

"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'] . "')"

The idea is to join the two queries into one, I know this is not the correct syntax but it might give you an idea on how to go about it. 我的想法是将两个查询合并为一个,我知道这不是正确的语法,但是它可能会让您了解如何进行。

Or you can do another query while loop for the horse names 或者,您可以在循环时对马名进行另一个查询

$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'>";

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM