kevin_newbie Posted September 4, 2009 Share Posted September 4, 2009 Hello, I have been trying to search for a way where I can split my results dynamcially but everyone seems to do it with tables. I like to do this in divs. Right now it is matching $count and $split at 13 then the rest of the entries go to the second divs. It only displays 2 divs I want to be able to have num_col be what ever number and still have the columns display how it should. This is what I have so far; <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="" /> <style type="text/css"> div.right { float: left; padding: 10px; margin: 0px 30px 0px 0px; border: 1px solid #000000; } </style> </head> <body> <div class="right"> <?php $s = mysql_query("SELECT name FROM us_states"); $num_row = mysql_num_rows($s); //Number of Columns $num_col = 4; //$split = 13 $split = ceil($num_row / $num_col); $count = 1; while($r = mysql_fetch_array($s)) { $name = $r['name']; echo $name . "<br />"; if ($count == $split) { echo "</div><div class=\"right\">"; } $count++; } ?> </div> </body> </html> Is this a good start if so where should i go from here? Thanks. Link to comment https://forums.phpfreaks.com/topic/173076-solved-split-result-in-multiple-columns/ Share on other sites More sharing options...
trq Posted September 4, 2009 Share Posted September 4, 2009 There is an example of this in our code/snippet repository under PHP Coding. Which, by the way is where this thread belongs. Link to comment https://forums.phpfreaks.com/topic/173076-solved-split-result-in-multiple-columns/#findComment-912240 Share on other sites More sharing options...
kevin_newbie Posted September 4, 2009 Author Share Posted September 4, 2009 Are you talking about this thread http://www.phpfreaks.com/forums/index.php/topic,95426.0.html? If so that is done using tables, I want divs. Link to comment https://forums.phpfreaks.com/topic/173076-solved-split-result-in-multiple-columns/#findComment-912248 Share on other sites More sharing options...
trq Posted September 4, 2009 Share Posted September 4, 2009 The logic is the same. Link to comment https://forums.phpfreaks.com/topic/173076-solved-split-result-in-multiple-columns/#findComment-912253 Share on other sites More sharing options...
kevin_newbie Posted September 4, 2009 Author Share Posted September 4, 2009 I did that code just now and it is not what I wanted. The thing is I want the code is go column by column not row by row. My snippet is doing all of the data in one column then when it hits the if statement it closes the first column and dumps the rest of the record in the second column. I want that same concept but spread in how ever many columns I type in. So that the reminder is in the last column I specified. Link to comment https://forums.phpfreaks.com/topic/173076-solved-split-result-in-multiple-columns/#findComment-912261 Share on other sites More sharing options...
kickstart Posted September 4, 2009 Share Posted September 4, 2009 Hi If I understand correctly what you are after is something like a newspaper layout. Ie, a set number of columns and splitting the data between them. You only check that $count is equal to $split, which will only happen once (ie, with 1000 rows you would have 250 per column and $split would be 250). Use the modulus of $count / $split and check it is 0 (which would be OK for 250, 500 and 750):- if (($count % $split) == 0) All the best Keith Link to comment https://forums.phpfreaks.com/topic/173076-solved-split-result-in-multiple-columns/#findComment-912312 Share on other sites More sharing options...
kevin_newbie Posted September 4, 2009 Author Share Posted September 4, 2009 AWESOME! Thank you very much. Is there a tutorial that you recommend that explains the % for php? Also I was playing around with the if statement i had in the loop I changed to this: if ($count == $split) { echo "</div><div class=\"right\">"; $count = 1; continue; } It seems to work but just a little bit more lines of code. What do you think? Link to comment https://forums.phpfreaks.com/topic/173076-solved-split-result-in-multiple-columns/#findComment-912526 Share on other sites More sharing options...
kickstart Posted September 4, 2009 Share Posted September 4, 2009 Hi % is modulus. Basically does a divide and gives you the remainder. If you google for modulus you should find plenty on it (googling for % is pretty pointless!) Your solution would also work. May actually be more efficient as there are no calculations to be done (in this case as it is all you are using $count for). Personally I would leave out the continue (either put the $count++ in an else, or just set $count = 0 within that if statement), as I do not like jumping around like that (a GOTO by another name). All the best Keith Link to comment https://forums.phpfreaks.com/topic/173076-solved-split-result-in-multiple-columns/#findComment-912587 Share on other sites More sharing options...
kevin_newbie Posted September 4, 2009 Author Share Posted September 4, 2009 Thank you very much. Link to comment https://forums.phpfreaks.com/topic/173076-solved-split-result-in-multiple-columns/#findComment-912643 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.