View Single Post
Old 03-06-2008, 04:18 PM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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($items4);

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
Attached Thumbnails
best-way-loop-table-displaytable.jpg  
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following 2 Users Say Thank You to Alan @ CIT For This Useful Post:
SOCK (03-06-2008), StevenF (03-06-2008)