Jump to content

[SOLVED] Echo Results into a Complicated Table


Fluoresce

Recommended Posts

This one's a challenge (or at least it's proved to be one for a newbie like me).

 

  • I have a database of shops (e.g., Walmart, etc.).
  • The shops have been given a category and a subcategory (e.g., Clothing > Women's Clothing).
  • I am trying to echo the shops into a table like this:

 

1b5f8631311302.gif

 

Notice how the category names are in a row of their own and how their subcategories sit beneath them with a maximum of 4 per row.

 

So far, I have managed to produce the code below. I can't figure out how to count the subcategories and make them fall into a new row after four.

 

Can anyone help, please?

 

<?php

$conn = mysql_connect('localhost','', '') or trigger_error("SQL", E_USER_ERROR);  
mysql_select_db('', $conn) or trigger_error("SQL", E_USER_ERROR);

$sql = "SELECT category, subcat, shop, url, . . .";

$st = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

$last_category = '';
$last_subcat = '';

echo "<table>";

while ($row = mysql_fetch_assoc($st))
{

      if ($row['category'] != $last_category)
      {   
               
           if ($last_subcat)
           {
               echo "</td></tr>";
           }

           $last_category = $row['category'];
           echo "<tr><td colspan=\"4\"><h2>{$last_category}</h2></td></tr><tr>";

           $last_subcat = $row['subcat'];
           echo "<td><h4>{$last_subcat}</h4>";
       } 
       
       else       
       {     

           if ($row['subcat'] != $last_subcat)
           {
               echo "</td>";
               $last_subcat = $row['subcat'];
               echo "<td><h4>{$last_subcat}</h4>";

           } 

   else
           {
               echo "<br />";
           }   
       }


       if ($row['shop'] == '')
       {
           echo "None";
       }
    
       else
       {
           echo "<a href=\"{$row['url']}\">{$row['shop']}</a>";
       }

}

echo "</td></tr></table>";

?>

Fluoresce,

 

See my previous post about using modulo operator ( % ) :

 

http://www.phpfreaks.com/forums/index.php/topic,227931.msg1052507.html#msg1052507

 

It should set you on the right track...

 

Scot L. Diddle, Richmond VA

use a simple counter $i for the subcategories

 

if $i = 1, output <tr> (new row)

if $i = 4, output </tr> (end row)

 

based on your example you always want the 4 rows displayed, and if there is no subcategory content specified, then you will just output a blank

Thanks, Lonewolf217. That gave me an idea which allowed me to do it.

 

All I done was count how many <td>s were echoed. After every fourth one, I started a new <tr>. Each time a new category was echoed, I reset the counter, ensuring that a maximum of four <td>s would ever be echoed beneath a category. I'll post the code once I have it cleaned up.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.