forumnz Posted September 25, 2007 Share Posted September 25, 2007 I have managed to scrape some code together (cleanly), which displays all my categories - one in each row. However, I want to be able to have it so that it displays them evenly in 3 columns. Category Category Category Category Category Category Category Category Category Category Category Category Like above (each one is its own category). How can I go about this? Below is the current code snippet for displaying it. <?php $background = ($counter++%2) ? 'c1' : 'c2'; $subcategories_content .= '<tr class="' . $background . '"> '. ' <td width="100%"> » <a href="categories.php?parent_id=' . $cat_details['category_id'] . '">' . $category_lang[$cat_details['category_id']] . '</a> '. (($setts['enable_cat_counters']) ? (($cat_details['items_counter']) ? '(<strong>' . $cat_details['items_counter'] . '</strong>)' : '') : '') . '</td> '. '</tr> '; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/ Share on other sites More sharing options...
GingerRobot Posted September 25, 2007 Share Posted September 25, 2007 Take a look at this post here: http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Should be what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355198 Share on other sites More sharing options...
gromer Posted September 25, 2007 Share Posted September 25, 2007 Very rough outline: <html> <head> </head> <body> <table> <?php $currentColumn = 0; while (condition) { if ($currentColumn == 0) { echo "<tr>"; } echo "<td>" . $somedatafromyourdatabase . "</td>"; if ($currentColumn == 2) { echo "</tr>"; $currentColumn = 0; } else { ++$currentColumn; } } ?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355202 Share on other sites More sharing options...
gromer Posted September 25, 2007 Share Posted September 25, 2007 Take a look at this post here: http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Should be what you're looking for. Beat me to it Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355204 Share on other sites More sharing options...
forumnz Posted September 25, 2007 Author Share Posted September 25, 2007 Thanks! I tried what you said GingerRobot. I fixed all the errors, but I still get all of the categories in one list. This is my current code for it: <?php $sql_select_categories = $db->query("SELECT category_id, items_counter FROM " . DB_PREFIX . "categories WHERE parent_id='" . $parent_id . "' AND user_id=0 ORDER BY order_id ASC, name ASC"); if($sql_select_categories && mysql_num_rows($sql_select_categories) > 0) { $i = 0; $max_columns = 3; while ($cat_details = $db->fetch_array($sql_select_categories)) { $subcategories_content .= '<tr> '. ' <td width="100%"> » <a href="categories.php?parent_id=' . $cat_details['category_id'] . '">' . $category_lang[$cat_details['category_id']] . '</a> '. (($setts['enable_cat_counters']) ? (($cat_details['items_counter']) ? '(<strong>' . $cat_details['items_counter'] . '</strong>)' : '') : '') . '</td> '; } if(++$i == $max_columns) { echo "</tr>"; $i=0; } } ?> Any idea? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355207 Share on other sites More sharing options...
forumnz Posted September 25, 2007 Author Share Posted September 25, 2007 I have been working on it some more. It still doesn't work. This is my current code - thanks for any help you can give. <?php $sql_select_categories = $db->query("SELECT category_id, items_counter FROM " . DB_PREFIX . "categories WHERE parent_id='" . $parent_id . "' AND user_id=0 ORDER BY order_id ASC, name ASC"); $result = mysql_query($sql_select_categories) or die("There was a problem with the SQL query: " . mysql_error()); if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { extract($row); if($i == 0) echo "<tr>"; echo '<td width="100%"> » <a href="categories.php?parent_id=' . $cat_details['category_id'] . '">' . $category_lang[$cat_details['category_id']] . '</a> '. (($setts['enable_cat_counters']) ? (($cat_details['items_counter']) ? '(<strong>' . $cat_details['items_counter'] . '</strong>)' : '') : '') . '</td>'; if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results ?> Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355225 Share on other sites More sharing options...
forumnz Posted September 25, 2007 Author Share Posted September 25, 2007 Help? Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355259 Share on other sites More sharing options...
GingerRobot Posted September 25, 2007 Share Posted September 25, 2007 In what way does it not work? What happens when you run the code? I know it may sound obvious, but i take it you are also echoing a table tag somewhere? Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355266 Share on other sites More sharing options...
forumnz Posted September 25, 2007 Author Share Posted September 25, 2007 Hi, Yes I am closiing it. It just echos all the categories in one column, not 3. Thanks, Sam. Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355267 Share on other sites More sharing options...
gromer Posted September 25, 2007 Share Posted September 25, 2007 Hi, Yes I am closiing it. It just echos all the categories in one column, not 3. Thanks, Sam. Do you have a url to the live site with this code we could look at? Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355298 Share on other sites More sharing options...
forumnz Posted September 25, 2007 Author Share Posted September 25, 2007 www.bidbuddy.co.nz/test.php Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355345 Share on other sites More sharing options...
forumnz Posted September 26, 2007 Author Share Posted September 26, 2007 Bump Quote Link to comment https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355378 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.