shafikshaon
shafikshaon

Reputation: 6404

Group MySQL data by day in PHP

$query="SELECT * FROM cse_routine WHERE batch ='$batch' and section='$section'  order by 'day'";
$post = $db->select($query);
?> 

<?php
  if ($post) {
    while ($result = $post->fetch_assoc()) {
      $var = "select * from cse_routine where day = '".$result['day'] ."'";
      print $result['day']."<br>";
        if ($results=$db->select($var))
          {
             while ($rows = mysqli_fetch_assoc($results))
               {
                  print $rows['t_slot']. " " . $rows['room']. " " . $rows['day']. " " . $rows['c_code'];
                  echo "<br>";
               }
          }
          echo "<br>";
        }
  }
  echo "<br>";  
?>

Running this code; I found this:

code

It's ok. But actually, I want output that not print the same date repeatedly. See here Saturday, Sunday print 3 each of them but I want that Saturday print 1 and Sunday print for 1.

Upvotes: 0

Views: 107

Answers (2)

Karpfen93
Karpfen93

Reputation: 73

You could try using DISTINCT constraint (http://www.mysqltutorial.org/mysql-distinct.aspx) in first SQL:

$query="SELECT DISTINCT day  FROM cse_routine WHERE batch ='$batch' and section='$section' order by 'day'";

So you will have Saturday and Sunday only once each.

Upvotes: 0

kumaravel
kumaravel

Reputation: 74

You just change you query to

$query="SELECT * FROM cse_routine WHERE batch ='$batch' and section='$section'

  GROUP by 'day'";

Upvotes: 2

Related Questions