owner Posted July 30, 2009 Share Posted July 30, 2009 Hello, I am trying to figure out how to sort my data into 3 columns, but distribute the data so it is all equal. For example, lets say I get a result out of my mySQL database and it has 101 items. Instead of displaying my list of items (all 101 in 1 column), I would like to distribute them equally into 3 columns. So in this case, I would have 34 items in my first column, 34 in the second column, and 33 in my 3rd column. How would I go about doing this? Thanks in advance! -owner Link to comment https://forums.phpfreaks.com/topic/168058-sort-into-3-columns/ Share on other sites More sharing options...
Third_Degree Posted July 30, 2009 Share Posted July 30, 2009 Make an array with all the results, then use a for loop to go through by threes. Add a check to see if its the last row, and figure out the remainder, and output the last row as either one, two, or three cells. Sorry, I don't have the time to code it out for you right now. Link to comment https://forums.phpfreaks.com/topic/168058-sort-into-3-columns/#findComment-886406 Share on other sites More sharing options...
gevans Posted July 30, 2009 Share Posted July 30, 2009 Count the number of results. Divide by 3. Use a loop so that while a counter (lets say $i), while $i is less than the number of total results divided by 3 print them. When that number is reached, reset the counter and move to the next column. If it's a table try something like this.. <?php //db stuff missing $result = mysql_query("SELECT * FROM `table`"); $results = mysql_num_rows($result); $per_column = ceil($results/3); $i = 0; echo '<table><tr><td>'; while($row = mysql_fetch_assoc($result)) { if($i == $per_column) { echo '</td><td>'; $i=0; } echo $row['your_field']."<br />"; $i++; } echo '</td></tr></table>'; Link to comment https://forums.phpfreaks.com/topic/168058-sort-into-3-columns/#findComment-886408 Share on other sites More sharing options...
owner Posted July 30, 2009 Author Share Posted July 30, 2009 I have the results in an array right now, however I am not quite sure what to do after that. If I loop by 3s won't it miss some data? Link to comment https://forums.phpfreaks.com/topic/168058-sort-into-3-columns/#findComment-886409 Share on other sites More sharing options...
vineld Posted July 30, 2009 Share Posted July 30, 2009 I have the results in an array right now, however I am not quite sure what to do after that. If I loop by 3s won't it miss some data? That's why he used ceil for rounding. Link to comment https://forums.phpfreaks.com/topic/168058-sort-into-3-columns/#findComment-886415 Share on other sites More sharing options...
Third_Degree Posted July 30, 2009 Share Posted July 30, 2009 I have the results in an array right now, however I am not quite sure what to do after that. If I loop by 3s won't it miss some data? no, <tr><td><?= $results[$i]; ?></td><td><?= $results[$i + 1]; ?></td><td><?= $results[$i + 2]; ?></td></tr> Link to comment https://forums.phpfreaks.com/topic/168058-sort-into-3-columns/#findComment-886479 Share on other sites More sharing options...
owner Posted July 30, 2009 Author Share Posted July 30, 2009 If I do things that way, I wont get my list correct. I would like the list to be 1 | 8 | 15 2 | 9 | 16 3 | 10 | 17 4 | 11 | 18 5 | 12 | 19 6 | 13 | 20 9 | 14 | 21 As in, rows 0-33 in first column, 34-67, 68-101 Link to comment https://forums.phpfreaks.com/topic/168058-sort-into-3-columns/#findComment-886517 Share on other sites More sharing options...
Third_Degree Posted July 30, 2009 Share Posted July 30, 2009 ok, divide the number of results by three, then change the $i + 1 and $i + 2 to $i + (total/3) and $i + 2(total/3), and increment by 1. Link to comment https://forums.phpfreaks.com/topic/168058-sort-into-3-columns/#findComment-886523 Share on other sites More sharing options...
fooDigi Posted July 30, 2009 Share Posted July 30, 2009 if you are not forced to populate a table you can just float:left some divs, and just reset the <div> tag at the right time... i threw this together, so it might be a bit messy, but it seems to work... at least a working towards your needed logic ... <? $rec_ct = 103; $cols = 3; $col_len = ceil($rec_ct / $cols); for($i=1;$i<=$rec_ct;$i++) { if($i % $col_len == 1) { echo '</div><div style="float:left;border:1px solid #999; width:100px;">'; } echo "$i<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/168058-sort-into-3-columns/#findComment-886536 Share on other sites More sharing options...
gevans Posted July 30, 2009 Share Posted July 30, 2009 Count the number of results. Divide by 3. Use a loop so that while a counter (lets say $i), while $i is less than the number of total results divided by 3 print them. When that number is reached, reset the counter and move to the next column. If it's a table try something like this.. <?php //db stuff missing $result = mysql_query("SELECT * FROM `table`"); $results = mysql_num_rows($result); $per_column = ceil($results/3); $i = 0; echo '<table><tr><td>'; while($row = mysql_fetch_assoc($result)) { if($i == $per_column) { echo '</td><td>'; $i=0; } echo $row['your_field']."<br />"; $i++; } echo '</td></tr></table>'; That does what you're asking, try it Link to comment https://forums.phpfreaks.com/topic/168058-sort-into-3-columns/#findComment-886693 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.