Jump to content

Building table by column rather than by row


stephdumais

Recommended Posts

Hello,

I'm currently looking for a way to populate some html tables of varying lenghts and widths, pulling the data from arrays.

I want to achieve this with a function in order to make this as dynamic as possible.

 

example:

 

function build_table($data){ ... }

 

$data would be an array containing a varying number of sub-arrays, the first array would populate the first row (column headings) and each subsequent array would be the data for each column rather than each row.

 

Thus the table would be build as such:

 

row 1 = array1[1],array2[1],array3[1]...

row 2 = array1[2],array2[2],array3[2]...

row 3 = array1[3],array2[3],array3[3]...

 

In other words, I would like each specific key value from each array to be merged into new arrays that form the rows.

 

Let me know if this is clear enough...if so, does anyone have any ideas?

 

Link to comment
Share on other sites

Here's a simpler way to explain what I'm trying to achieve:

 

Here's my particular setup:

 

$data = array(
            array('1','2','3','4'),
            array('5','6','7','8'),
            array('9','10','11','12'),
       );

 

Each sub-array in my $data should be a column, while each element in the sub-arrays should become a row, resulting in a table like this:

 

1 | 5 | 9

2 | 6 | 10

3 | 7 | 11

4 | 8 | 12

 

Now I'm not sure how to loop this... Any ideas?

Link to comment
Share on other sites

try

<?php 
$data = array(
            array('1','2','3','4'),
            array('5','6','7','8'),
            array('9','10','11','12'),
       );

       
// Simplest way

echo "<table><tr>";

foreach ($data as $col) {
    echo '<td>' . join ('<br>', $col) . '<td>';
}

echo "</tr></table>";       
?>

Link to comment
Share on other sites

<?php 
$data = array(
            array('1','2','3','4'),
            array('5','6','7','8'),
            array('9','10','11','12'),
       );

       


echo "<table border='1'>";

for ($r = 0; $r < 4; $r++) {
    echo '<tr>';
    for ($c = 0; $c < 3; $c++) {
        echo '<td>', $data[$c][$r], '</td>';
    }
    echo '</tr>';
}
echo "</table>";       
?>

Link to comment
Share on other sites

just as an idea...

 

<?
$query=mysql_query("SELECT * FROM `accounts`");
while($row=mysql_fetch_assoc($query)){
$arr[]=$row;
}

echo '<table>';
$k=0;
for($i=0; $i<=count($arr)/3; $i++){
if($k==0) echo '<tr>';

echo '<td>';
print_r($arr[$i]);
echo '</td>';
echo '<td>';
print_r($arr[$i+3]);
echo '</td>';
echo '<td>';
print_r($arr[$i+6]);
echo '</td>';
$k++;
echo '</tr>';
}
echo '</table>';
?>

 

math's prolly off tho...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.