Check for same rows in a while loop and put them in a separate table


I would want to first check for all equal rows and then put them into a separate table.

This is what I have done so far:

table1 | id | name | | 1 | JUS | | 1 | NUM | | 2 | SET | /** * this is the the query for retrieving the data * from table1 */ $query="SELECT id, name FROM table1 order by id"; $results=$db->query($query); $previous=''; while($row=mysqli_fetch_assoc($results)){ $id=$row['id']; $name=$row['name']; if($id==$previous){ /** * This is where i am stucked up */ $current=''; } $previous=$id; }

I want to get the id with 1 as the value into one html table, like below

first html table ID | 1 | 1 | Name | JUS | NUM |

and also get the id with 2 as the value into another html table. So in all we will get separate tables if the id are not the same:

second html table ID | 2 | Name | SET |

Any idea as to how to go about it is appreciated.


You could just gather all them first in a container, using ids as your keys so that they'll be grouped together. After that, just print them accordingly:

$data = array(); while($row = $results->fetch_assoc()){ $id = $row['id']; $name = $row['name']; $data[$id][] = $name; // group them } foreach($data as $id => $values) { // each grouped id will be printed in each table echo '<table>'; // header echo '<tr>'; echo '<td>ID</td>' . str_repeat("<td>$id</td>", count($values)); echo '</tr>'; echo '<tr>'; echo '<td>Name</td>'; foreach($values as $value) { echo "<td>$value</td>"; } echo '</tr>'; echo '</table><br/>'; }

This will work if those fields are just like that, if you need something more dynamic, you need another dimension, and instead of just pushing name, you'll need the push the entire row:

$results = $db->query('SELECT id, name, age FROM table1'); $data = array(); while($row = $results->fetch_assoc()){ $id = $row['id']; unset($row['id']); $data[$id][] = $row; // group them } $fields = array('name', 'age'); foreach($data as $id => $values) { // each grouped id will be printed in each table echo '<table>'; // header echo '<tr>'; echo '<td>ID</td>' . str_repeat("<td>$id</td>", count($values)); echo '</tr>'; foreach($fields as $field) { // construct td $temp = ''; echo "<tr><td>$field</td>"; for($i = 0; $i < count($values); $i++) { $temp .= '<td>' . $values[$i][$field] . '</td>'; } echo $temp; // constructed td echo '</tr>'; } echo '</table><br/>'; }


  • Match incremented class with incremented ids from different loops [jQuery / Wordpress]
  • “multiple types in one declaration” but no ; missing
  • Rabbitmq crash when boot failed
  • How to generate dynamic dropdown PHP Javascript mySQL
  • video streaming over http in blackberry
  • Passing data-* to bootstrap Modal
  • Create a list like array when echoed in php
  • Why does the compiler find this ambiguous?
  • Using BOOST_FOREACH with a constant intrusive list
  • TensorFlow stuck into endless loop using tf.while_loop()
  • Retrieving last value of LSTM sequence in Tensorflow
  • PHP + MySQL - Autocomplete from database not getting data from table
  • Is playing sound in Javascript performance heavy?
  • How can I extract results of aggregate queries in slick?
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Custom Tabgroup Appcelerator
  • MongoDB in PHP using aggregate to group by _id is null not working
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Insert into database using onclick function
  • Projection media query: browser support and workarounds?
  • Different response to non-authenticated users and AJAX calls
  • Knitr HTML Loop - Some HTML output, some R output
  • Updated Ionic CLI but shows previous version (Windows)
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Unanticipated behavior
  • PHP: When would you need the self:: keyword?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Acquiring multiple attributes from .xml file in c#
  • Turn off referential integrity in Derby? is it possible?
  • Can't mass-assign protected attributes when import data from csv file
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • How can I use threading to 'tick' a timer to be accessed by other threads?