tags:

views:

28

answers:

2

Hi,

I have a mysql query that brings in a list of results, say 10, each with its own 'last edited' date, and was wondering if it was possible to display/echo/print ONLY the most recent of these dates using php?

Now I know I could do this with a mysql query, but I need to display all of the available results, but only 1 date, (the most recent).

Hope this makes sense. Any help greatly appreciated. S.

<?php                         
                    $availQuery=mysql_query("select * from isavailability");         
                    $availResult=mysql_fetch_array($availQuery);

                    $date =  $availResult['editDate'];    
                    $newDate = date('D  dS  F  Y',strtotime($date));
                    $time = date('G:i:s',strtotime($date));
                    echo '<p>This page was last updated on '.$newDate.' at '.$time.'</p>' . PHP_EOL;                     

                    while($availR=mysql_fetch_array($availQuery)) {                                                    
                                echo '<tr class="rows">'. PHP_EOL;
                                echo '<td><p>'.$availR['title'].'</p></td>'. PHP_EOL;
                                echo '<td>'; if ($availR['availability']==1) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
                                echo '<td>'; if ($availR['availability']==2) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
                                echo '<td>'; if ($availR['availability']==3) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
                                echo '</tr>'. PHP_EOL;
                        }
                    ?>  
A: 

If your query is ordered by your timestamp then you could just grab the first one - if I understand your question:

$myDate = $myData[0]['myDateColumn'];
foreach($myData as $row)
{
    echo $myDate . ' ' . $row['someotherColumn'] . "\n";
}

or something similar? Presuming your results are in an array.

boodle
Thanks for the reply; The results are ordered by their titles.
ss888
+1  A: 

It can be done in a single query: SELECT isavailabilit.*, MAX(editDate) AS LastEditDate FROM isavailabilit;

Use it this way in the loop: $availR['LastEditDate']

Otar
Exactly what I was looking for! Many thanks.
ss888