Re: rows by day

From: Date: Mon, 16 Jun 2003 14:08:53 +0000
Subject: Re: rows by day
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-151659@lists.php.net to get a copy of this message
"Sebastian" <sebastian@dslr.net> wrote in message news:001701c333b2$6a3fd7b0$6cec5d42@level3... >[snip] > $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]"; > } > > 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 ... If you're sorting in descending order (as your query seems to intend), shouldn't it be Wednesday ... Tuesday ... Monday? $query = "SELECT title, author, text, tstamp, DAYNAME(tstamp) AS day " ."FROM news " ."SORT BY tstamp DESC " ."LIMIT {$_GET['page']}, $pagelimit"; $result = $db->sql($query); $first = true; $lastday = ""; while($row = mysql_fetch_array($result)) { if ($row['day'] != $lastday) { $lastday = $row['day']; if ($first) $first = false; else echo "</ul>"; echo date("l M j", $row['tstamp']) . "<br/><ul>"; } echo "<li><b>{$row['title']}</b><br/>" ."Written by {$row['author']}<br/>" ."{$row['text']}</li>"; } if ($first) echo "No results returned!<br/>"; else echo "</ul>"; In production, I would rewrite this to separate the logic from the presentation... but if you trace through it, this should give you a good idea of how to proceed. -- Hugh Bothwell hugh_bothwell@hotmail.com Kingston ON Canada v3.1 GCS/E/AT d- s+: a- C+++ L++>+++$ P+ E- W+++$ N++ K? w++ M PS+ PE++ Y+ PGP+ t-- 5++ !X R+ tv b++++ DI+++ D-(++) G+ e(++) h-- r- y+

« previous php.general (#151659) next »