[thelist] Re: php/mysql question - solved

Dunstan Orchard dunstan at 1976design.com
Wed Mar 19 06:42:21 CST 2003


> > I have a db with the following fields in:
> > 
> > postid     int(11)
> > postdate   date
> > posttitle  tinytext
> > postbody   text
> > 
> > And I'd like to do the following:
> > 
> > [1] Select the last 30 posts
> > [2] For each month that the posts fall into print a heading of that month
> > [3] Then print all the posts within that month
> > [4] Keep going until my 30 posts have been printed
> >
> > Could anyone recommend the best way of achieving this in php?
> 
> /a/ way (don't know about best...):

Do you know, I must have the brain of a duck today.

Thankyou very much Kae, though your code didn't work out, it did give me the 
ridiculously simple answer to the question.

A cut-down version is shown below:

****************************************
$query = ("SELECT posttitle, DATE_FORMAT(postdate, '%M') AS postmonthname FROM 
newspost ORDER BY postid DESC LIMIT 30");

// run query
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result))
 {
 $postmonthname = $row['postmonthname'];
 $posttitle = $row['posttitle'];


 if ($m <> $postmonth)
   {
   $m = $postmonth;
   print '<h3>' . $postmonthname . '</h3>';
   }

 print '<p>' . $poststitle . '</p>';
 }
****************************************


---------------------------
Dorset, England
http://www.1976design.com/
http://www.orchard.it/
http://www.maccaws.org/


More information about the thelist mailing list