Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.