
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20The bad thing using this solution is the effect that will have to your readers. When we (we as the readers in this case) see a column we read it from the top to bottom. The nested solution will arrange your data from left to right for each line. I will share some code with you that will arrange the categories from top to bottom and I really want you to try and compare both of them to see what I'm talking about. Here's an example of the output:
1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20
$items = array('ALABAMA','ALASKA');//create an array with your data
// Default # of Columns
$numcols = 4;
// Number of Items
$numitems = count($items);
// Number of Rows
$numrows = ceil($numitems/$numcols);
echo '<table>';
for ($row=1; $row <= $numrows; $row++)
{
$cell = 0;
echo ' <tr>'."\n";
for ($col=1; $col <= $numcols; $col++)
{
echo ' <td>'."\n";
if ($col===1)
{
$cell += $row;
print $items[$cell - 1];
}
else {
$cell += $numrows;
print $items[$cell - 1];
}
echo ' </td>'."\n";
}
echo ' </tr>'."\n";
}
echo '</table>';
I had this pasted on a forum and a member tried to use his database to interact with the array so for those of you who don't know how to work around it here's the modified version:
$query_cat = "SELECT * FROM "table" WHERE";
$query_cat .= " "filtered_row" = "parameter"";
$cat = mysql_query($query_cat) or die(mysql_error());
$total = mysql_num_rows($cat);
while($row = mysql_fetch_array($cat))
{
// -- This array will hold your data,
// -- I used it to hold a title, an id and an author
// -- but it can be extended based on your own needs
$items[] = array(1 => $row['title'], 2 => $row['id'], 3 => $row['author']);
}
// Default # of Columns
$numcols = 4;
// Number of Items
$numitems = count($items);
// Number of Rows
$numrows = ceil($numitems/$numcols);
echo '<table width="100%">';
for ($row=1; $row <= $numrows; $row++)
{
$cell = 0;
echo ' <tr>'."\n";
for ($col=1; $col <= $numcols; $col++)
{
echo ' <td width="'.round(100/$numcols).'%">'."\n";
if ($col===1)
{
$cell += $row;
print $items[$cell - 1][1]; /* $items[$cell - 1][1] to display title $items[$cell - 1][2] etc... */
}
else {
$cell += $numrows;
print $items[$cell - 1][1];
}
echo ' </td>'."\n";
}
echo ' </tr>'."\n";
}
echo '</table>';
Added by roScripts on April-18-2007, 3:53 pm
