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? Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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/ Quote Link to comment 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++; } 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.