gpt4 book ai didi

php - 陷入 PHP 循环的中间

转载 作者:行者123 更新时间:2023-11-29 06:24:02 28 4
gpt4 key购买 nike

这是我到目前为止所得到的-

$awards_sql_1 = mysql_query('SELECT * FROM categories WHERE section_id = 1') or die(mysql_error());
$awards_sql_2 = mysql_query('SELECT * FROM categories WHERE section_id = 2') or die(mysql_error());
$awards_sql_3 = mysql_query('SELECT * FROM categories WHERE section_id = 3') or die(mysql_error());
$awards_sql_4 = mysql_query('SELECT * FROM categories WHERE section_id = 4') or die(mysql_error());

$loop = 1;

while($row_sections = mysql_fetch_array($sections_query)) {
$category = 1;
echo "<h3>" . $row_sections['section_name'] . " (Loop# $loop)</h3>";

while($categories = mysql_fetch_array(${"awards_sql_{$loop}"})) {
${"winners_sql_{$loop}"} = mysql_query("SELECT * FROM 2009_RKR_bestof WHERE section = $loop && category = $category ORDER BY result_level ASC") or die(mysql_error());
echo "<h4><strong>{$categories['category_name']}</strong></h4>";
echo "<ul class=\"winners\">";
>> while($winners = mysql_fetch_array(${"winners_sql_{$loop}"})) {
switch ($winners['result_level']) {
case 1: $result_level = "Platinum"; break;
case 2: $result_level = "Gold"; break;
case 3: $result_level = "Silver"; break;
}
if (isset($winners['url'])) { $anchor = "<a href=\"http://{$winners['url']}\" target=\"_blank\">"; $close = "</a>"; }
echo "<li>$anchor{$winners['winner']}$close ($result_level)</li>";

unset($anchor);
unset($close);
}
echo "</ul>";
$category++;
}
$loop++;
}

让我感到困惑的是,我正在让这个东西正确循环,我的循环计数器($loop)正在工作,但是当在第一次循环获胜者之后有时间吐出实际奖励接收者时,它只生成类别标题,列表项不会被循环出去。

我添加了一个小指针,指向我认为问题开始或集中在 (>>) 的位置。

我的猜测是我可能需要在某个地方取消设置 var,但我不知道,我看不到它。

最佳答案

我支持 KM - 您正在显示一个页面,并且通过循环,您会同时发生大量查询 - 如果 1,000 人同时访问该页面怎么办?哎哟...

也许考虑一个更大的查询(带有一些重复的数据)并循环一次?

例如:

SELECT 
section_name,
category_name,
result_level,
url,
winner
FROM 2009_RKR_bestof
INNER JOIN categories ON 2009_RKR_bestof.category = categories.id
INNER JOIN sections ON 2009_RKR_bestof.section = sections.id
ORDER BY section_name,category_name ASC

在循环中,您可以进行检查以确定您是否位于新部分(类别/其他):

//pseudo-code
$current_section = "";
while($stuff = mysql_fetch_array($sql))
{
if ($current_section == "")
{
$current_section = $stuff["section_name"];
}
if ($current_section == $stuff["section_name"])
{
//keep going in your loop
}
else
{
//we've gotten to a new section - so close your html and start a new section
}
}

你明白了..

关于php - 陷入 PHP 循环的中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1214176/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com