Help displaying data in four columns?
I have 28 rows of data, i know how to display the data.
The problem is that it lists the data in one column, I would like to display the 28 items within FOUR columns, equally.
In the future I'll have more than 28 rows...
data data data data
data data data data
data data data data
data data data data
Again, i would like the coulumns to contain the same amount of items, (unless it's an odd number of course)
Thanks!
Re: Help displaying data in four columns?
Code:
$query = "SELECT * FROM recent";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
echo ceil($num_rows/4);
the echo displays 7
I just need to know how to put this data in FOUR columns, equally (unless odd of course)
Anyone?
Re: Help displaying data in four columns?
????.............................
Re: Help displaying data in four columns?
Are you displaying this using text or HTML or what?
Also, please don't keep bumping your thread, especially not at such short intervals.
Re: Help displaying data in four columns?
Quote:
Originally Posted by penagate
Are you displaying this using text or HTML or what?
Also, please don't keep bumping your thread, especially not at such short intervals.
I'm displaying the content with text and html.
Re: Help displaying data in four columns?
something like this? some old script I had lying around (in this case, it's 5 columns, rather than 4).
PHP Code:
<table border="1">
<?php
$pictures = array("picture1", "picture2", "picture3", "picture4", "picture5", "picture6", "picture7");
$ii = 0; //index for the row
$cols = 5; //number of columns in each row
$ncol = true; //whether or not to make a new column
for($i = 0; $i <= count($pictures) - 1; $i++){
$ii++;
//end a row?
if($ii > $cols){
$ii = 0; //reset row index
echo '</tr>' . "\n";
$ncol = true;
}
//start a new row?
if($ncol == true){
$ncol = false;
echo '<tr>' . "\n";
}
//display image
echo ' <td>' . $pictures[$i] . '</td>' . "\n";
//is the table finished?
if($i == count($pictures) - 1){
//finish filling the column if we need to
if($ii < $cols){
for($j = $ii; $j < $cols - 1; $j++){
echo ' <td> </td>' . "\n";
}
}
echo '</tr>' . "\n";
}
}
?>
</table>
working example of this code is here, still not sure if that's what you're looking for or not. if it is what you're looking for, it can easily be changed to work with a while() loop with mysql results rather than a for() loop with an array.
Re: Help displaying data in four columns?
PHP Code:
<table style="width: 100%;">
<tr><th>Header 1</th><th>Header 2</th><th>Header 3</th><th>Header 4</th></tr>
<?php
$col = 0;
$query = mysql_query("SELECT * FROM whatever WHERE whatever1 = whatever2");
while($row = mysql_fetch_array($query)) {
switch($col) {
case 0:
echo '<tr><td>'.$row[$col].'</td>';
$col++;
break;
case 3:
echo '<td>'.$row[$col].'</td></tr>';
$col = 0;
break;
default:
echo '<td>'.$row[$col].'</td>';
$col++;
break;
}
}
?>
</table>
Hope that helps :wave: