我希望有人可以帮助我...
这是我的疑问:
$query_articles = mysql_query("SELECT * FROM articles ORDER BY article_date DESC LIMIT 30") or die(mysql_error());
$row_articles = mysql_fetch_assoc($query_articles);
我想要做的是显示所有30个结果,但我希望它是这样的:
// Show article #1
// Then loop through articles 2-7
// Then show article #8
// Then loop through articles 9-30
原因是因为我对上面列出的每个集合都有不同的格式。我可以很容易地进行单独的查询,但这并不理想,加上它会在事后搞砸我的分页。
那么如何用这一个查询做这些循环呢?任何帮助,将不胜感激!感谢。
/////更新/////
好的,这就是我现在所拥有的,它给了我我想要的东西,但它也显示了31-42行(现在我的数据库中根本不存在):
$query_articles = mysql_query("SELECT * FROM articles ORDER BY article_date DESC LIMIT 32") or die(mysql_error());
$row_articles = mysql_fetch_assoc($query_articles);
$shown_articles = array(1, 8);
$article_index = 1;
foreach ($row_articles as $article) {
do {
if (in_array($article_index, $shown_articles)) {
echo '<p>'.$article_index.' ';
echo $row_articles['title'];
echo '</p>';
} else {
echo '<p>'.$article_index.' ';
echo $row_articles['title'];
echo '</p>';
}
$article_index++;
} while($row_articles = mysql_fetch_assoc($query_articles));
}
知道为什么它会显示额外的行吗?另外,如果我使用mysql_fetch_array,它会上升到第55行。
/////更新/////
如果有其他人需要,这是我的最终代码。我还添加了另一个条件因为我也想分开9-30行。
$query_articles = mysql_query("SELECT * FROM articles ORDER BY article_date DESC LIMIT 32") or die(mysql_error());
$shown_articles = array(1, 8);
$article_range = range(9, 30);
$article_index = 1;
while($row_articles = mysql_fetch_assoc($query_articles)) {
if (in_array($article_index, $shown_articles)) {
echo '<p>'.$article_index.' ';
echo $row_articles['title'];
echo '</p>';
} elseif (in_array($article_index, $article_range)) {
echo '<p>'.$article_index.' ';
echo $row_articles['title'];
echo ' - 9-30</p>';
} else {
echo '<p>'.$article_index.' ';
echo $row_articles['title'];
echo ' - 2-7</p>';
}
$article_index++;
}
答案 0 :(得分:2)
首先根据结果创建一个迭代器:
$rows = new ArrayIterator($row_articles);
然后你想要阻止迭代器倒带:
$articles = new NoRewindIterator($rows);
这可以帮助您创建子迭代器(例如,对于您的列表)不倒带但继续。
然后你可以像你认为合适的那样对它进行操作:
foreach ($articles as $i => $article)
{
switch ($i)
{
case 1:
article_display($article);
article_list(new LimitIterator($articles, 0, 6));
break;
case 8:
article_display($article);
article_list(new LimitIterator($articles, 0, 21));
break;
default:
throw new Excpetion(sprintf("Unexpected i: %d", $i));
}
}
注意:可能$i
基于零,然后您需要更改case
语句中的硬编码数字,因为我已经基于它编写了1。但我认为你明白了。
答案 1 :(得分:1)
您可以使用modulo operator来实现此目的:
$query_articles = mysql_query("SELECT * FROM articles ORDER BY article_date DESC LIMIT 30") or die(mysql_error());
$row_articles = mysql_fetch_assoc($query_articles);
$i=0;
foreach($row_articles as $article) {
if($i % 8 == 0) {
// do stuff for row 0, 8 ..
}
else {
// do stuff for other rows
}
$i++;
}
答案 2 :(得分:0)
$shown_articles = array(1, 8);
$article_index = 1;
foreach ($row_article as $article) {
if (in_array($article_index, $shown_articles)) {
// Show article
} else {
// Do something else
}
$article_index++;
}
答案 3 :(得分:0)
你(以及所有其他答案)对mysql_fetch_assoc
返回的内容感到困惑。它没有返回包含所有行的数组,它只返回一行。您通常不应该遍历此数组,只需访问所需的特定列。
if
声明,因为它在两个分支中做同样的事情。我猜真正的应用程序做了不同的事情,我只是插入了占位符。
$query_articles = mysql_query("SELECT * FROM articles ORDER BY article_date DESC LIMIT 32") or die(mysql_error());
$shown_articles = array(1, 8);
$article_index = 1;
while ($row_articles = mysql_fetch_assoc($query_articles)) {
if (in_array($article_index, $shown_articles)) {
echo '<p>'.$article_index.' ';
echo $row_articles['title'];
echo '<br>Stuff for articles in $shown_articles';
echo '</p>';
} else {
echo '<p>'.$article_index.' ';
echo $row_articles['title'];
echo '<br>Stuff for articles NOT in $shown_articles';
echo '</p>';
}
$article_index++;
}