03-06-2008, 04:18 PM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
|
Hi Steven,
Here's a little cut/paste example and the results it displays
PHP Code:
<?php
$items = array(
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Eleven',
'Twelve',
'Thirteen',
'Fourteen',
'Fifteen',
'Sixteen',
'Seventeen',
);
function array_chunk_vertical($input, $size, $preserve_keys = false, $size_is_horizontal = true)
{
$chunks = array();
if ($size_is_horizontal) {
$chunk_count = ceil(count($input) / $size);
} else {
$chunk_count = $size;
}
for ($chunk_index = 0; $chunk_index < $chunk_count; $chunk_index++) {
$chunks[] = array();
}
$chunk_index = 0;
foreach ($input as $key => $value)
{
if ($preserve_keys) {
$chunks[$chunk_index][$key] = $value;
} else {
$chunks[$chunk_index][] = $value;
}
if (++$chunk_index == $chunk_count) {
$chunk_index = 0;
}
}
return $chunks;
}
$rows = array_chunk_vertical($items, 4);
print "<table border=\"1\">\n";
foreach ($rows as $row) {
print "<tr>\n";
foreach ($row as $value) {
print "<td>" . $value . "</td>\n";
}
print "</tr>\n";
}
print "</table>\n";
This example uses an array called $items but changing it to your mysql data should be fairly easy. Changing $items block of code to something like the following should work:
PHP Code:
$query = "SELECT cd_artist FROM products WHERE cs_category = 'rock'";
$result = mysql_query($query);
$items = mysql_fetch_array($result);
This code only fetches the cd_artist column but you can add more columns there as required.
Alan
|
|
|