php 一个 While 循环中的多个 While 循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5730695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple While Loops within a While Loop?
提问by Tristan Heffernan
Total newbie trying to learn... thanks for all the help so far!
正在努力学习的新手......感谢到目前为止的所有帮助!
UPDATE - updated code below - Thanks 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. 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?
我现在正在尝试从一个表中获取相关的马匹信息horses
,我可以将它加入到race_form
Horse_name 字段中。然后我想在 while($racecard) 信息下方显示比赛中每匹马的信息。这是否使它更清楚一点?
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 在我已经有了的下面进行一个 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:
我想知道你是否可以在一个 while 循环中一个接一个地运行 2 个 while 循环?我正在编写赛马表格指南 - 我可以显示每个比赛列表,但在下面我想显示比赛中每匹马的个别马匹详细信息。无论如何,如果您查看此页面,您可以看到我正在尝试做的事情:
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. 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.
问题是,每当我在比赛列表后放置第二个 while 循环时,它都不会出现。我正在尝试运行查询以从我的马匹表中获取马匹详细信息,然后为比赛中的每匹马运行一段时间,但没有任何显示。
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>";
回答by Calum
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)){
回答by Rudie
I theory you could and in practice you can, but a while
in a while
in a for
in a while
seems a little bitover the top...
我理论上你可以,在实践中你可以,但是while
a while
in a for
in awhile
似乎有点过头了......
I'm sure we can help you make it more efficient if you explain what it is you're trying to do.
我相信如果你解释你想要做什么,我们可以帮助你提高效率。
回答by Phill Pafford
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'>";
}