Avatar of gisvpn
gisvpnFlag for United States of America

asked on 

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?
HTMLPHPSQL

Avatar of undefined
Last Comment
Chris Stanyon
Avatar of Branislav Borojevic
Branislav Borojevic
Flag of Canada image

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

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo