Avatar of ggjones
ggjones
Flag 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
PHP

Avatar of undefined
Last Comment
ggjones

8/22/2022 - Mon
jrm213jrm213

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


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
jrm213jrm213

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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