简体   繁体   中英

php output from the database to html table

I have table entries in my database with structure:

id | card | event | time | day | hm
  • > id - is unique ID of each entry.
  • > card - is number.
  • > event - is number (0, 1, 2, or 3)
  • > time - is mysql_timestamp
  • > day - is day of month when entry was added (date("j", time())
  • > hm - some other info...

I want to display all entries for one month in html table. Now I'm using this simple query:

$sql = "SELECT event, time, day, hm FROM `entries` 
WHERE card=$card_t 
AND YEAR(time)=$year 
AND MONTH(time)=$month
AND event IN (0,1,3) 
ORDER BY `time` ASC;";

Conditions of output: table must have minimum one row for a day (31 rows for January). if there is no entries for day(1-31) row must be: if there is only two entries for day row must be:

<tr>
   <td>$day_number</td>
   <td>$time_of_first_event</td>
   <td>$first_event</td>
   <td>$time_of_last_event</td>
   <td>$last_event</td>
 </tr>

 if there is more than two entries a day two must be (I will hide second one with jquery later):

  <tr>
   <td>$day_number</td>
   <td>$time_of_first_event</td>
   <td>$first_event</td>
   <td>$time_of_last_event</td>
   <td>$last_event</td>
 </tr>
 <tr>
     <table>
       <tr>
        <td>$time</td>
        <td>$event</td>
       </tr>
     </table>
 </tr>

So it looks like:

| 1 | no entry                                          |
| 2 | no entry                                          |
| 3 | no entry                                          |
| 4 | time_first | event_fisrt | time_last  |event_last |
                 | time2 | event 2                      |
                 | time3 | event 3                      |
| 5 | no entry                                          |
... 

But all i got now is output of 31 row and if one day have more than it duplicates this day row and displays all rows for this day:

| 1 | no entry       |
| 2 | no entry       |
| 3 | no entry       |
| 4 | time1 event 1  |
| 4 | time2 event 2  |
| 4 | time3 event 3  |
| 5 | no entry       |
...

Please help me guys and sorry for mistakes, english is not my native lang.

You want to group the output, if I understand you correctly. And the criterion for building the groups is the date. One way would be to define a variable before your loop, in which you store the date of the last processed entry. And before you output the entry, you check whether the entry's date is the same as in this variable...then you are still within a group and should output it differently.

Might be a little difficult to achieve this in your case because of your very special output format. It might be easier to first loop over the query result and store the entries in a multidimensional array where the date is the key of the first level and in the next level you store all the entries related to this date. So you get:

array (
   'xxxx-xx-xx' => array(entry1, entry2, entry3),
   'yyyy-yy-yy' => array(entry1),
   ...
)

Then you can do another loop over this array and use if (count($array[$currentKey]) == 1) to check whether there is only a single event for this date or more.

Use GROUP_CONCAT in sql and explode foreach loop

$sql = 


"SELECT 
        GROUP_CONCAT(event SEPARATOR ","), GROUP_CONCAT(time SEPARATOR ","), 
        day , GROUP_CONCAT(hm SEPARATOR ",") FROM `entries` 
    WHERE 
        card=$card_t 
        AND YEAR(time)=$year 
        AND MONTH(time)=$month
        AND event IN (0,1,3) 
    GROUP BY 
        day
    ORDER BY 
        `time` ASC;";

In foreach,

foreach ($entries as $entry) {
    if ($entry['event'] == "") {
        echo '<tr>';
             echo '<td>'.$no++.'</td>';
             echo '<td>No events</td>';
             ...................
        echo </tr>';
        continue;
    }

    $events = explode("," , $entry['event']);
    if (count($events) > 0) {
       // Have multiple row here
       foreach($events as $event) {
           ........................
       }
    } else {
        echo '<tr>';
             echo '<td>'.$no++.'</td>';
             echo '<td>'.$entry['event'].'</td>';
             ...................
        echo </tr>';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM