Reputation:
I'm hoping someone can help me on this...
Here's my query:
$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);
What I would like to do is show all 30 results, but I want it to be like this:
// Show article #1
// Then loop through articles 2-7
// Then show article #8
// Then loop through articles 9-30
The reason is because I have different formatting for each set listed above. I could easily do separate queries, but it's not ideal, plus it would screw up my pagination afterwards.
So how would I do these loops with that one query? Any help would be appreciated! Thanks.
///// UPDATE /////
Ok this is what I have now and it gives me exactly what I want BUT it also shows rows 31-42 (which don't even exist right now in my DB):
$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));
}
Any idea as to why it's showing extras rows? Also, if I use mysql_fetch_array, it goes up to row 55.
///// UPDATE /////
Here's my final code if anyone else needs it. I also added another conditional because I also wanted to separate the 9-30 rows.
$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++;
}
Upvotes: 2
Views: 203
Reputation: 780798
You (and all the other answers) are confused about what mysql_fetch_assoc
returns. It doesn't return an array with all the rows, it returns just one row. You normally shouldn't be iterating through this array, you just access the specific columns you want.
I don't understand your if
statement, though, since it does the same thing in both branches. I guess the real application does different things, I've just inserted placeholders.
$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++;
}
Upvotes: 0
Reputation: 197659
You first create an iterator based on your result:
$rows = new ArrayIterator($row_articles);
Then you want to prevent that iterator to rewind:
$articles = new NoRewindIterator($rows);
This helps you to create sub-iterators (e.g. for your listing) to not rewind but to continue.
Then you can operate on it like you see fit:
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));
}
}
Note: It might be that $i
is zero based, then you need to change the hardcoded numbers in the case
statements as I have written it 1 based. But I think you get the idea.
Upvotes: 2
Reputation: 21856
You can use the modulo operator to achieve this:
$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++;
}
Upvotes: 1
Reputation: 11467
$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++;
}
Upvotes: 0