Re: rows by day
| From: | John W. Holmes | Date: | Mon, 16 Jun 2003 03:09:58 +0000 |
| Subject: | Re: rows by day | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-151619@lists.php.net to get a copy of this message | ||
Sebastian wrote:
I posted this at several other discussion forums, and i haven't received any real way of doing this. i am about 3/4 done with a article management i am working on and came across a problem i can't seem to figure out. On to the question .. I currently have this query: $result = $db->sql("SELECT * FROM $news GROUP BY time DESC LIMIT $_GET[page], $pagelimit"); while($row = mysql_fetch_row($result)) { echo "$row[title] <br/> Written by $row[author] <br/> $row[text]";Probably a typo, but fetch_row() will not give you $row['title'], $row['text'], etc... fetch_array() and fetch_assoc() will, though. More below...
} I'd like to sort the results by day, example: Monday's Articles: all rows that are posted on Monday..... Tuesday's Articles: all rows that are posted on Tuesday ... Etc, etc.. How would I group them like that. I want to echo the actual day with all the rows of that day falling under it. can i do this with one query? Or would i have to create multiple queries? any examples?You only need one query. One of your columns should be what you want to "change" on, like DAYOFWEEK(date_column). As you loop through your rows, you echo out your "heading" whenever this column changes... while($row = mysql_fetch_assoc($result)) { if($row['dayofweek_column'] != $old_dayofweek) {
echo "<tr><th>{$row['dayofweek_column']}'s Columns</th></tr>";
$old_dayofweek = $row['dayofweek_column'];
}
echo "$row[title] <br/> Written by $row[author] <br/> $row[text]";
}
Untested, but something like that. :)
---John Holmes...
Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/
PHP|Architect: A magazine for PHP Professionals – www.phparch.com