PDA

View Full Version : Split Results to Three Columns


keith1995
01-16-2005, 08:04 AM
I've never actually figured out how to do this. What I'm looking to do is query our database, get the results (which are simple names) and then output the results to a three column table with the results evenly divided up into each column. So, the table would look similar to:

result 1 | result 4 | result 7
result 2 | result 5 | result 8
result 3 | result 6 | result 9

The database is a very simple one. Just two rows:

ID
Name

How do I accomplish this?

raf
01-16-2005, 07:12 PM
what server side language will you use to turn the recordset into a table?

regardles of the language, you best ue an incrementing variable and then check the modulus when you devide this incremental by 3.

keith1995
01-16-2005, 10:52 PM
Raf,

You are always so quick to resond to my questions and are always so helpful! Thanks for that.

I will be using PHP. If you could show me some sample code that I could use to edit to fit my needs, that will be very helpful.

raf
01-17-2005, 08:35 AM
here you go http://www.codingforums.com/showthread.php?t=40859&highlight=modulus
--> contains links to two threads with examplecode

keith1995
01-18-2005, 09:09 PM
Raf,

I've got the following code that doesn't what I need. However, this lists the results as such:

Name 1 | Name 2 | Name 3
Name 4 | Name 5 | Name 6
Name 7 | Name 8 | Name 9

I'd like to get it to list it like this:

Name 1 | Name 4 | Name 7
Name 2 | Name 5 | Name 8
Name 3 | Name 6 | Name 9

The code is:
echo "<table width='100%' border='0' cellpadding='2' cellspacing='2' class='twelve'>";

$column=3;

while($value=mysql_fetch_assoc($result))
{
$id = $value['id'];
$first_name = $value['first_name'];
$last_name = $value['last_name'];

if ($column==3)
{
echo "<tr>";
}

echo "<td>$first_name $last_name</td>";

$column++;

if ($column==6)
{
echo "</tr>";
$column=3;
}

}
echo "</table>";

Any ideas how I would change it to get it to list the results like above?

raf
01-18-2005, 09:56 PM
so kinda like a three column article or so ...

i would first build a 2D array where the first dimension is the row, and the second dimension is the column. Like
$arr_cells[1][1] = Name1
$arr_cells[2][1] = Name2
$arr_cells[1][2] = Name4

and then turn that array into a table like

$table = '<table>';
foreach ($arr_cells as $row){
$table .= '<tr>';
foreach ($row as $cell){
$table .= '<td>'. $cell .'</td>';
}
$table .= '</tr>';
}
$table = '</table>';


the harder bit is to build the array. You need to have a count and devide it by the number of columns, and then ceil it to know the number of rows. And then you need a variable that is incremented till you reach the number of rows, and then reset + increment another variable that contains the columnvalue. Like

$number_of_collumns = '3';
$row_num = '1';
$coll_num = '1';
$number_of_rows = ceil(mysql_num_rows($result)/$number_of_collumns);
$arr_cells = array();
while($value=mysql_fetch_assoc($result)) {
$arr_cells[$row_num][$coll_num] = $value['first_name'] . ' ' . $value['last_name'];
$row_num ++;
if ($row_num > $number_of_rows){
$row_num = '1';
$coll_num ++;
}
}
// and then turning the array into a table
$table = '<table>';
foreach ($arr_cells as $row){
$table .= '<tr>';
foreach ($row as $cell){
$table .= '<td>'. $cell .'</td>';
}
$table .= '</tr>';
}
$table = '</table>';