[thelist] php array

Simon Perry thelist at si-designs.co.uk
Mon Nov 21 13:35:19 CST 2005


Mark Groen wrote:

>Getting a couple columns from the database with event dates in
>2005-11-29 format, and would like to display the result with each new
>month showing the month and day, just for the first event of that month
>and the rest of the days in the month showing just the days.
>
>  
>
A couple of pointers for a starter.

Use MySQL to get the date in the format you require. [0]

Retrieve the results as an associative array that way if your database
schema changes the code won't break plus it also makes it so much easier
to maintain.

$sql='SELECT *, DATE_FORMAT(`event_date`,\'%a %D \') AS day_date,
DATE_FORMAT(`event_date`,\'%M \') AS month FROM `table_name` ';

while($row = mysql_fetch_array($eventList,MYSQL_ASSOC)
{
if($current_month!=$row['month'])
  {
  echo $row['day_date'].' '.$row['month'].' '.$row['title'];
  $current_month=$row['month'];
  }
  else
    {
    echo $row['day_date'].' '.$row['title'];
    }
}

Will output

Mon 28th November Rick Astley
Tue 29th Little Britain
Wed 30th The Bravery
Fri 2nd December Blondie

Simon

[0] http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html




More information about the thelist mailing list