Grouping result's output? PHP, SQL

Hi All,

I have a simple list of results extracted and outputted to the page like this:

$sql_qry_tsk = "SELECT * FROM main WHERE tsk_meeting='$qry_e24' ORDER BY tsk_date DESC ;";
$result_qry_tsk = $conn_show_actions->query($sql_qry);


    if ($result_qry_tsk ->num_rows > 0) {

      while($row = $result_qry_tsk->fetch_assoc()) {

        $qry_tsk_nme = $row["tsk_nme"];
				$qry_tsk_date = $row["tsk_date"];
       
        
        echo "$qry_tsk_date - $qry_tsk_nme<br>";
        }
        
        } else {
        
        echo "No results.";
        
        }

Open in new window



I would like to output these results but group by the tsk_date so for example if I had these results for tsk_date instead of outputting them in the list (as below, as they currently do):

2018/08/25 - a result's name
2018/08/25 - a result's name
2018/08/25 - a result's name
2018/08/24 - a result's name
2018/08/24 - a result's name
2018/08/23 - a result's name
2018/08/23 - a result's name
2018/08/23 - a result's name
2018/08/20 - a result's name
2018/08/20 - a result's name

I instead would like:

Saturday - 8/25
2018/08/25 - a result's name
2018/08/25 - a result's name
2018/08/25 - a result's name

Friday - 8/24
2018/08/24 - a result's name
2018/08/24 - a result's name

Thursday - 8/23
2018/08/23 - a result's name
2018/08/23 - a result's name
2018/08/23 - a result's name

Monday - 8/20
2018/08/20 - a result's name
2018/08/20 - a result's name

How would this be approached?
gisvpnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Branislav BorojevicWeb EnthusiastCommented:
Can you try the code below? I would better structure it inside a table, for better visibility.

<table>
      <thead>
        <tr><th>Date</th> <th>Result Name</th></tr>
      </thead>
      <tbody>
<?php
$sql_qry_tsk = "SELECT * FROM main WHERE tsk_meeting='$qry_e24' ORDER BY tsk_date DESC ;";
$result_qry_tsk = $conn_show_actions->query($sql_qry);


    if ($result_qry_tsk ->num_rows > 0) {
        $currentDate = false;
                while($row2 = $result_qry_tsk->fetch_assoc()) {
                    $i++;
                    if ($row2['tsk_date'] != $currentDate){
        ?>
       <tr>
          <td colspan='2'> <?php echo $row2['tsk_date']; ?></td></tr>

        <?php $currentDate = $row2['date'];
            }
        ?>
        <tr>
            <td><?php echo $row2['tsk_name']; ?> </td><td> <?php echo $row2['tsk_date']; ?></td>
            
    
        <?php
              }
            } else { ?>
        <tr>
        <td colspan="2"><?php echo "No results."; ?></td></tr>
        
        } ?>
              </tbody>
</table>
 

Open in new window

1
Chris StanyonWebDevCommented:
There are a few ways you can do what you need. If you choose to use the PDO libraries for your DB  work, it has a built in method for doing it. Something like this:

$query = $db->query("SELECT tsk_date, tsk_nme FROM main WHERE tsk_meeting='$qry_e24' ORDER BY tsk_date DESC");
$records = $query->fetchAll(PDO::FETCH_GROUP);

foreach ($records as $date => $values):
    echo $date;

    foreach ($values as $value):
        echo $value->tsk_nme;
    endforeach;  
endforeach;

Open in new window

If you want to stick with mysqli, I would suggest you loop through your records and build a grouped array. Something like this:

$query = $db->query("SELECT tsk_date, tsk_nme FROM main WHERE tsk_meeting='$qry_e24' ORDER BY tsk_date DESC");

while ($record = $query->fetch_object()):
    $records[$record->tsk_date][] = $record;
endwhile;

Open in new window

Now you've built a grouped array, you can loop through in a similar fashion to the PDO way:

foreach ($records as $date => $values):
    echo $date;

    foreach ($values as $value):
        echo $value->tsk_nme;
    endforeach;  
endforeach;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.