simcoweb Posted February 24, 2007 Share Posted February 24, 2007 I want to display sub-category titles/links in a two column format. I've got this code currently but it's only displaying a single column. <?php // run query $sql = "SELECT * FROM subcategories"; $results = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($results) or die(mysql_error()); while ($row = mysql_fetch_array($results)){ $totalColumns = 2; $i = 1; echo "<table border='0' cellpadding='2'>"; for ($b=0;$b<3;$b++) { if ($i == 1) echo "<tr>"; echo "<td>"; echo "<a href='getcat.php?". $row['subcat_id'] . "'>" . $row['subcat_name'] . "</a>"; echo "</td>"; if ($i == $totalColumns) { echo "</tr>"; $i = 0; } $i++; } echo "</table>"; } ?> I want it to look like: boatsbooks stuffjunk thingscrap etc. Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 24, 2007 Share Posted February 24, 2007 i would like to know the samething with my admin panel for my website. i was going to post something about this but you beat me to it Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 24, 2007 Share Posted February 24, 2007 *bump* could anyone help with this? i would like to know myself XD Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 Uhm... when I run the code it shows two columns. Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 24, 2007 Share Posted February 24, 2007 if ($i == 1) echo "<tr>"; don't you need { }? Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 24, 2007 Share Posted February 24, 2007 Nvm you don't but i wrote this and it works fine: $rows = 5; echo "<table align=\"center\">"; for ($i=1; $i<5; $i++) { if ($i == 1) echo "<tr>"; echo "<td>test</td>"; if ($i == $rows) { echo "</tr>"; $i = 0; } } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 24, 2007 Share Posted February 24, 2007 sorry im half asleep lol the first one worked but its not correct. I ran your code and it works. Quote Link to comment Share on other sites More sharing options...
simcoweb Posted February 24, 2007 Author Share Posted February 24, 2007 Ok, i'm lost. My code creates just one column. Not two. Is there a tweak here that I missed that someone posted? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 It creates two. Post a link to the page. Quote Link to comment Share on other sites More sharing options...
simcoweb Posted February 24, 2007 Author Share Posted February 24, 2007 Jesirose, i'm doing it on my local machine (XAMP) so I can attach a screenshot only. Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 24, 2007 Share Posted February 24, 2007 Here try my code. Just change the #10 in the for() function for how many links you have and the number linked to $rows for how many links you want in each row: $rows = 7; $i = 1; echo "<table align=\"center\">"; for ($n=1; $n<=10; $n++) { if ($i == 1) echo "<tr>"; echo "<td>test</td>"; if ($i == $rows) { echo "</tr>"; $i = 0; } $i++; } echo "</table>"; Just replace "test" with your info Quote Link to comment Share on other sites More sharing options...
emehrkay Posted February 24, 2007 Share Posted February 24, 2007 there is a thread about multi column output in the code vault Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 24, 2007 Share Posted February 24, 2007 I just noticed from your screenshot that you use Firefox...that might be it too i dunno... Quote Link to comment Share on other sites More sharing options...
emehrkay Posted February 24, 2007 Share Posted February 24, 2007 and just looking at the original code, you never account for $i being zero for ($b=0;$b<3;$b++) { if ($i == 1) Quote Link to comment Share on other sites More sharing options...
sasa Posted February 24, 2007 Share Posted February 24, 2007 try <?php // run query $sql = "SELECT * FROM subcategories"; $results = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($results) or die(mysql_error()); $totalColumns = 2; $i = 1; echo "<table border='0' cellpadding='2'>"; while ($row = mysql_fetch_array($results)){ if ($i == 1) echo "<tr>"; echo "<td>"; echo "<a href='getcat.php?". $row['subcat_id'] . "'>" . $row['subcat_name'] . "</a>"; echo "</td>"; if ($i == $totalColumns) { echo "</tr>"; $i = 0; } $i++; } echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
simcoweb Posted February 24, 2007 Author Share Posted February 24, 2007 sasa, your changes worked. Thanks for that. I now have two columns! Yay! Quote Link to comment Share on other sites More sharing options...
simcoweb Posted February 24, 2007 Author Share Posted February 24, 2007 Ok, before we all warmy swarmy and take showers together.... another small issue has arisen. The best way I can describe it is it's not showing all the entries in the database. It's leaving one item out of each list. For example, I have this: $sql = "SELECT * FROM subcategories WHERE cat_id='1'"; cat_id #1 has 7 entries. It only displays 6. cat_id #2 has 24 entries. It only displays 23. There's 7 main categories ( cat_id=' ' ). The item that is getting dropped is the very first item in the database in each incident. Ideas? Quote Link to comment Share on other sites More sharing options...
sasa Posted February 24, 2007 Share Posted February 24, 2007 sorry i jusr copy your code and i don't see line 5 remove line 5 ($row = mysql_fetch_array($results) or die(mysql_error()) Quote Link to comment Share on other sites More sharing options...
Archadian Posted February 24, 2007 Share Posted February 24, 2007 Try this: (hopefully you get my idea of what im trying to do) <?php // run query $sql = "SELECT * FROM subcategories"; $results = mysql_query($sql) or die(mysql_error()); $count = mysql_num_rows($sql) or die(mysql_error()); $totalColumns = 2; $i = 1; $cat = $count['cat_id']; echo "<table border='0' cellpadding='2'>"; while ($row = mysql_fetch_array($results)){ for ($c=1; $c<= $cat; $c++) { if ($i == 1) echo "<tr>"; echo "<td>"; echo "<a href='getcat.php?". $row['subcat_id'] . "'>" . $row['subcat_name'] . "</a>"; echo "</td>"; if ($i == $totalColumns) { echo "</tr>"; $i = 0; } $i++; } } echo "</table>"; ?> Your code: <?php // run query $sql = "SELECT * FROM subcategories"; $results = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($results) or die(mysql_error()); while ($row = mysql_fetch_array($results)){ $totalColumns = 2; $i = 1; echo "<table border='0' cellpadding='2'>"; for ($b=0;$b<3;$b++) { if ($i == 1) echo "<tr>"; echo "<td>"; echo "<a href='getcat.php?". $row['subcat_id'] . "'>" . $row['subcat_name'] . "</a>"; echo "</td>"; if ($i == $totalColumns) { echo "</tr>"; $i = 0; } $i++; } echo "</table>"; } ?> you don't need to define $row twice, if you are using a while statement always define $row in the while statement. You weren't counting the number of rows you had so the for() didn't know where to start or when to stop. I hope this doesn't confuse you. Quote Link to comment Share on other sites More sharing options...
simcoweb Posted February 24, 2007 Author Share Posted February 24, 2007 Makes perfect sense. I think that $row line was a leftover from other versions of my code. I didn't catch that it was declaring it twice. Thanks for the help, everyone. It's displaying properly now. Quote Link to comment 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.