new_webmaster Posted August 17, 2011 Share Posted August 17, 2011 Hello everyone and thank you for having an interest in reading my post As the subject stated, I want the output produced by the following code to be split into 2 columns. Here's the code: <?php // Get categories $sql = "SELECT catid, catname AS catname FROM $t_cats WHERE enabled = '1' $sortcatsql"; // Version 5.0 $res = mysql_query($sql); while ($row = mysql_fetch_array($res)) { ?> <a href="?view=post&cityid=<?php echo $xcityid; ?>&catid=<?php echo $row['catid']; ?>&shortcutregion=<?php echo $_GET['shortcutregion']; ?>"><?php echo $row['catname']; ?></a><br> <?php } ?> Can anyone point me in the right direction on how to get this accomplished? I am currently studying web development (so its not like I dont know a little php) but my classes haven't taught us this yet. Anywho, any help will be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/ Share on other sites More sharing options...
dougjohnson Posted August 17, 2011 Share Posted August 17, 2011 You could use table rows and columns? <table> <? select and while statement here ?> <tr> <td> <? echo data here ?> </td> <td> <? echo data here ?> </td> </tr> <? end while ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/#findComment-1258736 Share on other sites More sharing options...
new_webmaster Posted August 17, 2011 Author Share Posted August 17, 2011 Thats how I tried attempting it at first but the thing is I need the code to split it for me seeing how it outputs until all categories have been listed Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/#findComment-1258740 Share on other sites More sharing options...
jcbones Posted August 17, 2011 Share Posted August 17, 2011 <?php // Get categories $sql = "SELECT catid, catname AS catname FROM $t_cats WHERE enabled = '1' $sortcatsql"; // Version 5.0 $res = mysql_query($sql); $num = mysql_num_rows($res); $split = (int) $num/2; $i = 0; while ($row = mysql_fetch_array($res)) { echo '<div style="width:48%;float:left">'; ?> <a href="?view=post&cityid=<?php echo $xcityid; ?>&catid=<?php echo $row['catid']; ?>&shortcutregion=<?php echo $_GET['shortcutregion']; ?>"><?php echo $row['catname']; ?></a><br> <?php echo ($split == ++$i) ? '</div><div style="width:48%;float:left">' : NULL; } echo '</div>'; ?> Let us know! Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/#findComment-1258782 Share on other sites More sharing options...
new_webmaster Posted August 17, 2011 Author Share Posted August 17, 2011 Thanks for your reply, I just tried it but it didnt work... however it did give me an idea where I might be able to make it work by using a table... let me code it real quick (so you all know what I mean) and if anyone would care to help me with it as always i'll greatly appreciate it! Thanks JCBONES! Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/#findComment-1258790 Share on other sites More sharing options...
new_webmaster Posted August 17, 2011 Author Share Posted August 17, 2011 How could I make it work so the divider is </td><td>? Here's my attempt at moding your code (I know its bad but like I said im learning) <table><tr><td> <?php // Get categories $sql = "SELECT catid, catname AS catname FROM $t_cats WHERE enabled = '1' $sortcatsql"; // Version 5.0 $res = mysql_query($sql); $num = mysql_num_rows($res); $split = (int) $num/2; $i = 0; while ($row = mysql_fetch_array($res)) { ?> <a href="?view=post&cityid=<?php echo $xcityid; ?>&catid=<?php echo $row['catid']; ?>&shortcutregion=<?php echo $_GET['shortcutregion']; ?>"><?php echo $row['catname']; ?></a><br> <?php echo ($split == ++$i) ? '</td><td>' : NULL; } ?> </td></tr></table> Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/#findComment-1258796 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 It's much easier to write this chunk ?> <a href="?view=post&cityid=<?php echo $xcityid; ?>&catid=<?php echo $row['catid']; ?>&shortcutregion=<?php echo $_GET['shortcutregion']; ?>"><?php echo $row['catname']; ?></a><br> <?php Like this instead echo '<a href="?view=post&cityid='.$xcityid.'&catid='.$row['catid'].'&shortcutregion='.$_GET['shortcutregion'].'">'.$row['catname'].'</a><br>'; As far as your solution goes, I'm not quite following the logic. Here's how I'd do it, using an array rather than MySQL results. <?php // Make dummy array $array = range(0,32); // This will keep track of how many rows we've echo'd $i = 0; // This tells us how many columns $cols = 3; $total = count( $array ); echo '<table><tr>'; foreach( $array as $value ) { // Echo the value in a cell echo '<td style="border:1px solid black;">'.$value.'</td>'; // Increase our counter by 1 before we check $i++; // Check to see if we need to start a new row by using the modulus operator // It'll give us the remainder of the two numbers divided. // We check if $i != $total to make sure we don't have a redundant empty row // at the end if( ($i % $cols) == 0 && $i != $total ) echo '</tr><tr>'; // Start a new row } // Now after the foreach, if we have an odd number of results, we'll have an // extra empty column we need to fill, we can do this automatically while( ($i % $cols) != 0 ) { echo '<td> </td>'; $i++; } echo '</tr></table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/#findComment-1258805 Share on other sites More sharing options...
new_webmaster Posted August 17, 2011 Author Share Posted August 17, 2011 Thanks for your reply, the logic I was trying to do was to modify JC Bones idea but rather than making a split using div tags to instead create a table and then find a way for his code to divide my stuff in 2 by using </td><td>? It might be confusing, but here a second attempt at coding it: <table><tr><td> <?php // Get categories $sql = "SELECT catid, catname AS catname FROM $t_cats WHERE enabled = '1' $sortcatsql"; // Version 5.0 $res = mysql_query($sql); $num = mysql_num_rows($res); $split = (int) $num/2; $i = 0; while ($row = mysql_fetch_array($res)) { echo '<a href="?view=post&cityid=<?php echo $xcityid; ?>&catid=<?php echo $row['catid']; ?>&shortcutregion=<?php echo $_GET['shortcutregion']; ?>"><?php echo $row['catname']; ?></a><br>'; ?> <?php echo ($split == ++$i) ? '</td><td>' : NULL; } echo '</td>'; ?> </td></tr></table> I really appreciate your guy's patience with me (hope you guys see that i'm REALLY trying to learn) Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/#findComment-1258815 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Take a look at my example. It does exactly what you want, just using an array as the source. It's only about 15 lines of code as well, once you take out the comments. Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/#findComment-1258817 Share on other sites More sharing options...
new_webmaster Posted August 17, 2011 Author Share Posted August 17, 2011 But how would I integrate that code on my site? I mean i'm not gonna lie but im a bit lost with your code (but i'm honestly trying) Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/#findComment-1258824 Share on other sites More sharing options...
jcbones Posted August 18, 2011 Share Posted August 18, 2011 Second thought, wrap this in intval() function. $split = (int) $num/2; Should be: $split = intval($num/2); //gets rid of decimals. Quote Link to comment https://forums.phpfreaks.com/topic/245061-split-php-output-into-2-columns/#findComment-1258844 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.