Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could add an if statement to test if the $current_cat is equal to the previous loops $current_cat. Do so by adding a new variable $last_cat, setting it equal to the current iterations $current_cat at the end of the while loop. Here is an example</p> <pre><code> $select = "SELECT * FROM `table` ORDER BY `category`"; $result = mysql_query($select); $current_cat = null; $last_cat = null; while ($rows = mysql_fetch_array($result)) { if ($current_cat == null) { // Create a table with an id name of the first table echo "&lt;table id='" . $rows["category"] . "'&gt;"; // Write the first row of the table - Category Title echo "&lt;tr class='categoryTitle'&gt;&lt;td&gt;" . $rows["category"] . "&lt;/td&gt;&lt;/tr&gt;"; } // Set the $current_cat to current loop category value $current_cat = $rows["category"]; if ($last_cat != null) { if ($current_cat != $last_cat) { // Close table from previous $current_cat echo "&lt;/table&gt;"; // Create new table with id name of the category echo "&lt;table id='" . $rows["category"] . "'&gt;"; // Write the first row of the table - Category Title echo "&lt;tr class='categoryTitle'&gt;&lt;td&gt;" . $rows["category"] . "&lt;/td&gt;&lt;/tr&gt;"; } } } // Write new row in table with the value of the title echo "&lt;tr&gt;&lt;td&gt;" . $rows[title] . "&lt;/td&gt;&lt;/tr&gt;"; // set the $last_cat to the value of $current_cat at the end of the loop $last_cat = $current_cat; } // Close the last table after while loop ends echo "&lt;/table&gt;"; </code></pre> <p>This will allow you to create separate tables based on the category name no matter how many categories you have and allow you to style the tables based on the category name.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload