Link to home
Start Free TrialLog in
Avatar of ggjones
ggjonesFlag for Afghanistan

asked on

How do I dynamically generate Table Cells based on variable content?


Hi there...

I need to dynamically populate up to 6 table cells in three rows of two columns each:

<table  border="0" cellpadding="0" cellspacing="0" style='width:1000px;'>
<tr>
<td style='width:50%;'></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>

Open in new window


The data comes from an array containing up to six arrays, each one containing a comma-delimited list.

$metaArray = array($array1, $array2, $array3, $array4, $array5, $array6);

Open in new window


However, the array may contain as little as 1 array, in which case the table should have a single cell only:

$metaArray = array($array3);

Open in new window


<table  border="0" cellpadding="0" cellspacing="0" style='width:1000px;'>
<tr>
<td style='width:100%;'></td>
</tr>
</table>

Open in new window


But if the array contains , say only 3 arrays, it should have three cells, thusly:

$metaArray = array($array1, $array3, $array6);

Open in new window


<table  border="0" cellpadding="0" cellspacing="0" style='width:1000px;'>
<tr>
<td style='width:50%;'></td>
<td></td>
</tr>
<tr>
<td colspan="2"style='width:100%;'></td>
</tr>
</table>

Open in new window


... and so on.

The order in which the cells are populated is important also - they need to follow their key order.

Could I get some help please, with the logic and syntax?

Many, many thanks,

GJ
Avatar of jrm213jrm213
jrm213jrm213
Flag of United States of America image

How are your rows determined? I get that you basically want 1 column per row, but how are you determining the number of rows?

possibly something like this but this is just a first guess, we probably need more information regarding exactly what you want...
<table>
<tbody>
<tr>
<?php
$cells = count($metaArray);
$width = 100/$cells;
for($i=0;$i<count($metaArray);$i++) //cells
{
    echo("<td width=\"$width\"></td>");
}
?>
</tr>
</tbody>
</table>

Open in new window


Avatar of ggjones

ASKER



... thanks for the reply jrm213jrm21

The number of rows is determined by the number of arrays (within the meta-array).

If one array, one row. If two arrays, 1 row, 2 columns.
If three arrays, 2 rows - 1 row, 2 columns. plus 1 row, 1 column

Or, in the case of an odd-number of cells - 3 for example -  2 rows, each having 2 columns, but the last cell being populated with with non-breaking-space-&nbsp;

Is that more clear??

regards,

GJ

SOLUTION
Avatar of jrm213jrm213
jrm213jrm213
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ggjones

ASKER


... thank you.

Both examples look good.

I'm having difficulty with the IMPLODE however.

Below is one example of the $metadata array.

I get this error:

Warning: implode() [function.implode]: Invalid arguments passed

Open in new window


when using:

implode($metaArray[$i],"")

Open in new window


What I need, i think, is the $key, rather than the index number:

implode($metaArray[$key],"")

Open in new window


what syntax do I use to pass the $key???

regards,

GJ


Array
(
    [products] => Array
        (
            [0] => Footwear
            [1] => Purses
            [2] => Boots
            [3] => Jewelry
            [4] => Shirts
            [5] => Hats
            [6] => Watches
            [7] => Apparel
            [8] => Jackets
            [9] => Hoodies
            [10] => T-shirt
        )

    [services] => Array
        (
            [0] => Gift Cards
            [1] => Hand Bags
        )

    [brands] => Array
        (
            [0] => Nike
            [1] => Converse
            [2] => Adidas
            [3] => Asics
            [4] => Dc
            [5] => Steve Madden
            [6] => Etnies
            [7] => Frye
            [8] => Rocket Dog
            [9] => Timberland
            [10] => Jessica Simpson
        )

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ggjones

ASKER

thanks jrm213jrm213, that does the trick very well.

My apologies for the confusion. I should have provided an array example to start with.

regards,

GJ