Fluoresce Posted March 31, 2009 Share Posted March 31, 2009 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: 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151890-solved-echo-results-into-a-complicated-table/ Share on other sites More sharing options...
ScotDiddle Posted March 31, 2009 Share Posted March 31, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/151890-solved-echo-results-into-a-complicated-table/#findComment-797706 Share on other sites More sharing options...
Yesideez Posted March 31, 2009 Share Posted March 31, 2009 I'd be inclined to look at using DIVs instead for that, would be much easier. Quote Link to comment https://forums.phpfreaks.com/topic/151890-solved-echo-results-into-a-complicated-table/#findComment-797733 Share on other sites More sharing options...
Fluoresce Posted March 31, 2009 Author Share Posted March 31, 2009 Thanks for the effort, ScotDiddle, but I don't think that'll work. I need the counter to reset after each category, otherwise subcategories will be getting broken up. Quote Link to comment https://forums.phpfreaks.com/topic/151890-solved-echo-results-into-a-complicated-table/#findComment-797905 Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/151890-solved-echo-results-into-a-complicated-table/#findComment-797912 Share on other sites More sharing options...
Fluoresce Posted March 31, 2009 Author Share Posted March 31, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151890-solved-echo-results-into-a-complicated-table/#findComment-797983 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.