While循环中有多个While循环? [英] Multiple While Loops within a While Loop?

查看:129
本文介绍了While循环中有多个While循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试学习的新手总数...到目前为止,感谢您提供的所有帮助!

Total newbie trying to learn... thanks for all the help so far!

更新-下面更新了代码-感谢菲尔

UPDATE - updated code below - Thanks Phill

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

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. I want to then display the information for each horse in the race below the while($racecard) info. Does this make it a bit clearer?

我以为我可以再进行一次查询,然后在一个已经存在的mysql_fetch_array下方的while循环中进行显示,然后将其显示出来,然后进入下一轮比赛,但这显然行不通...?!

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...?!

我想知道您是否可以在while循环内互相运行2个while循环?我正在制定赛马指南-我可以显示每个比赛列表,但是在下面我想显示比赛中每匹马的个人赛马细节.无论如何,如果您查看此页面,就可以看到我正在尝试做的事情:

I'm wondering if you can run 2 while loops after each other inside a while loop? 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

问题是,每当我在比赛列表之后放置第二个while循环时,它都不会显示.我正在尝试运行查询以从我的赛马表中获取赛马详细信息,然后为比赛中的每匹马运行一会儿,但没有任何反应.

Problem is, any time I put a second while loop after the race list, it won't show up. 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>";

推荐答案

我认为您可以摆脱其中一项查询:

I think you can get rid of one of your queries:

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

The first query gets the number of races by selecting a COUNT, This returns one record with the count value.

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

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

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)

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

这个想法是将两个查询合并为一个,我知道这不是正确的语法,但是它可能会让您对如何进行查询有所了解.

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

}

这篇关于While循环中有多个While循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆