How do you get this to work? - well, for more than simple <strong> and <i> tags - I'm trying to get a HTML table in as an array, if this is possible!

Any advice is appreciated!

8 Years
Discussion Span
Last Post by Airshow


Do you mean that you want to build an HTML table, storing chunks in a php array as you go?

If so, then consider this pseudo code:

var htmlTable = array();//initialise array
for(.......){//loop to create rows one at a time.
	htmlTable[] = make_new_row_here;//append new array element containing (string) HTML table row.
//Then to dump to the output stream ...
echo htmlTable[].implode("\n");//Join the pieces with a line break as a separator (to make the HTML output stream more readable in case you need to inspect it).

Of course there are many variations on the theme, for example you might want to build your table with a discrete statement per row rather than using a loop but the principle is the same.

Personally I use this pattern all the time in both php and javascript. It is certainly recommended in javascript and as far as I am aware it's efficient and fast in php too.


Edited by Airshow: n/a

Votes + Comments
Yes, me like

Also if you want columns in the array then a 2 dimensional array will be required. For example

for ($row=0;$row<21;$row++) {
    for ($column=0;$column<6;$column++) {
        $htmltable[$row][$column]=$row.'x'.$column; //cell value;
print_r($htmltable); //dump array

I haven't tested the above script but gives you an idea of how to generate an array with a table like structure.


print_r($htmltable); //dump array

Remember that print_r() "displays information about a variable in a way that's readable by humans" (from php manual).

print_r() is therefore good for debug-inspection of the HTML but not the right thing for commiting to the output stream where a rendered table is required.

That said, we don't know exactly what Whitestream6 wants, so good to make aware of print_r(). :icon_smile:



I was trying to set a HTML table with content already in place as an array - but if that's not possible, then not to worry, I will see what other HTML in arrays I can learn.


Do you want the array to output to a table?

$big_array = array(array('first_one','second_one', 'third_one'),array('first_two','second_two', 'third_two'),array('first_three','second_three', 'third_three'));

$output = "<table><thead><tr><th>first</th><th>second</th><th>third</th></tr></thead><tbody>";
$i =0;
while($i < count($big_array[$i])){
   $output .= "<tr><td>$big_array[$i][0]</td><td>$big_array[$i][1]</td><td>$big_array[$i][2]</td></tr>"
   $i = $i + 1;
$output .="</body></table>"
//you could beautify html source output with \n and \t - but I'll leave it out for now.
echo $output;

You should see output:

[B]first             second                third[/B]
first_one         second_one         third_one
first_two         second_two         third_two
first_three      second_three       third_three

Edited by diafol: n/a


Hi Ardav. That is indeed another viable interpretation.

Whitestream6, read my signature.



Hi Airshow, "long time..." and all that. Yep agree, a full description of the problem would have been useful.

Spotted a mistake in my code:

$output .="</body></table>"

Should be:

$output .="</tbody></table>"

Bodies all over the place! HTML killing field!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.