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
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
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
Share on other sites

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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.