1internet Posted July 7, 2013 Share Posted July 7, 2013 If I wanted to display database results in 4 different columns, would that essentially mean I have to run 4 different sql queiries with each of them limited, or is there another way around that? Link to comment https://forums.phpfreaks.com/topic/279947-columns-of-results/ Share on other sites More sharing options...
litebearer Posted July 8, 2013 Share Posted July 8, 2013 what differentiates the content of the display columns? Link to comment https://forums.phpfreaks.com/topic/279947-columns-of-results/#findComment-1439844 Share on other sites More sharing options...
1internet Posted July 8, 2013 Author Share Posted July 8, 2013 Nothing, its just returning a list of rows, but in 4 columns rather than one long list. Link to comment https://forums.phpfreaks.com/topic/279947-columns-of-results/#findComment-1439853 Share on other sites More sharing options...
Psycho Posted July 8, 2013 Share Posted July 8, 2013 You only run one query. Then you use PHP to format the output using PHP code. You don't state how the columns should be constructed 1 2 3 4 5 6 7 8 or 1 3 5 7 2 4 6 8 Plus, how will you display them? DIVs, in a table, or what? Link to comment https://forums.phpfreaks.com/topic/279947-columns-of-results/#findComment-1439854 Share on other sites More sharing options...
1internet Posted July 8, 2013 Author Share Posted July 8, 2013 e.g. $q = mysql_query(SELECT * FROM `table`); <div> while($r = mysql_fetch_assoc($q)) { echo $r['name']; } </div> But how do I split that into 4 divs: <div id="1"></div><div id="2"></div><div id="3"></div><div id="4"></div>, withouth having to do 4 sepate queries? Link to comment https://forums.phpfreaks.com/topic/279947-columns-of-results/#findComment-1439861 Share on other sites More sharing options...
cyberRobot Posted July 8, 2013 Share Posted July 8, 2013 You could use a counter. For every 4th entry, reset the counter. Link to comment https://forums.phpfreaks.com/topic/279947-columns-of-results/#findComment-1439910 Share on other sites More sharing options...
litebearer Posted July 8, 2013 Share Posted July 8, 2013 If you elect to use a table layout, check here ... http://forums.phpfreaks.com/topic/11572-multi-column-results/ Link to comment https://forums.phpfreaks.com/topic/279947-columns-of-results/#findComment-1439928 Share on other sites More sharing options...
AbraCadaver Posted July 8, 2013 Share Posted July 8, 2013 Not very elegant, but should give you the idea: $q = mysql_query(SELECT * FROM `table`); $total = mysql_num_rows($q); $groups = ceil($total / 4); $i = 0; while($r = mysql_fetch_assoc($q)) { if($i % $groups == 0) { if($i != 0) { echo '</div>'.PHP_EOL; } $id = $i / $groups + 1; echo '<div id="'.$id.'">'.PHP_EOL; } echo $r['name'].PHP_EOL; $i++; } Link to comment https://forums.phpfreaks.com/topic/279947-columns-of-results/#findComment-1439931 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.