Jump to content

Sort into 3 columns


owner

Recommended Posts

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.