Question:

Help with PHP and MySQL!

by Guest60485  |  earlier

0 LIKES UnLike

Hi all, I'm relatively new to php and mysql so please bear with me!

I have a database which stores the results of matches. One of my fields is a date field. I want to be able to display the info in my database like this:

1/1/2008

Team A 12 - 13 Team B

Team C 12 - 13 Team D

Team E 12 - 13 Team F

1/1/2007

Team G 12 - 13 Team H

Team I 12 - 13 Team J

Currently I can display all my data all grouped together descending by date. I want to be able to have it similar to the above so when the date changes theres a bit of a gap and then a table for the next date opens and so on.

Thank you

 Tags:

   Report

2 ANSWERS


  1. I guess you already have created MySQL table and have no problem storing matches there.

    Try

    $query    = 'SELECT distinct date FROM tablename';

    $result    = mysql_query($query);

    if (mysql_num_rows($result)>0)    {

       while ($row    = mysql_fetch_array($result))    {

           $query    = 'SELECT * FROM tablename WHERE date="'.$row['date'].'"';

           $resb    = mysql_query($query);

           if (mysql_num_rows($resb)>0)    {

               echo '<table>';

               while ($rowb    = mysql_fetch_array($resb))    {

                   echo '<tr><td>'. $row['date'] .'</td><td>'. $rowb['team'] .'</td></tr>';

               }

               echo '</table>';

           }

       }

    }

    It's only an example so replace table name, mysql cell name and output code


  2. I assume you are looping through the data displaying each item. First you want to but an order by in your sql query so the data is sorted by date. Then you can setup a variable called something like LastDate which will hold the date of the previous record. Each time through the loop you compare the current records data with the previous and if they changed, insert the space. At the end of the loop, set LastDate to the current records date.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.