Output a php multi-dimensional array to a html table

Posted by Fireflight on Stack Overflow See other posts from Stack Overflow or by Fireflight
Published on 2010-03-17T18:58:27Z Indexed on 2010/03/17 19:01 UTC
Read the original article Hit count: 216

Filed under:
|
|
|
|

I have been banging my head against the wall with this one for nearly a week now, and am no closer than I was the first day.

I have a form that has 8 columns and a variable number of rows which I need to email to the client in a nicely formatted email. The form submits the needed fields as a multidimensional array. Rough example is below:

<input name="order[0][topdiameter]" type="text" id="topdiameter0" value="1" size="5" />
<input name="order[0][bottomdiameter]" type="text" id="bottomdiameter0" value="1" size="5" />
<input name="order[0][slantheight]" type="text" id="slantheight0" value="1" size="5" />
<select name="order[0][fittertype]" id="fittertype0">
    <option value="harp">Harp</option>
    <option value="euro">Euro</option>
    <option value="bulbclip">Regular</option>
</select>
<input name="order[0][washerdrop]" type="text" id="washerdrop0" value="1" size="5" />
<select name="order[0][fabrictype]" id="fabrictype">
    <option value="linen">Linen</option>
    <option value="pleated">Pleated</option>
</select>
<select name="order[0][colours]" id="colours0">
    <option value="beige">Beige</option>
    <option value="white">White</option>
    <option value="eggshell">Eggshell</option>
    <option value="parchment">Parchment</option>
</select>
<input name="order[0][quantity]" type="text" id="quantity0" value="1" size="5" />

This form is formatted in a table, and rows can be added to it dynamically. What I've been unable to do is get a properly formatted table out of the array.

This is what I'm using now (grabbed from the net).

<?php
if (isset($_POST["submit"])) {
$arr= $_POST['order']
echo '<table>';
foreach($arr as $arrs)
    {
    echo '<tr>';
    foreach($arrs as $item)
    {
        echo "<td>$item</td>";
    }
    echo '</tr>';
    }

echo '</table>;
};
?>

This works perfectly for a single row of data. If I try submitting 2 or more rows from the form then one of the columns disappears. I'd like the table to be formatted as:

| top | Bottom | Slant | Fitter | Washer | Fabric | Colours | Quantity |
------------------------------------------------------------------------
|value| value  | value | value  | value  | value  |  value  |  value   |

with additional rows as needed. But, I can't find any examples that will generate that type of table!

It seems like this should be something fairly straightforward, but I just can't locate an example that works the way I need it too.

© Stack Overflow or respective owner

Related posts about php

Related posts about multidimensional-array