Output multidimensional php array to html table

I have a form that contains 8 columns and a variable number of rows that I need to send an email to a client in a well formatted email. The form represents the required fields as a multidimensional array. Example 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 was not able to do was get the formatted table from the array.

This is what I am currently using (grabbed from the network).

 <?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 fine for a single row of data. If I try to send 2 or more lines from the form, one of the columns will disappear. I would like the table to be formatted as:

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

with extra lines as needed. But I can not find examples that will generate this type of table!

It seems like this should be something pretty simple, but I just can't find an example that works the way I need it.

+4
source share
3 answers

In your HTML, try something like this:

 <table> <tr> <th>Bottom</th> <th>Slant</th> <th>Fitter</th> </tr> <?php foreach ($_POST['order'] as $order): ?> <tr> <td><?php echo $order['bottomdiameter'] ?></td> <td><?php echo $order['slantheight'] ?></td> <td><?php echo $order['fittertype'] ?></td> </tr> <?php endforeach; ?> </table> 

Obviously, I do not include all your attributes in it, but I hope you get this idea.

+5
source

How about this?

 $keys = array_keys($_POST['order'][0]); echo "<table><tr><th>".implode("</th><th>", $keys)."</th></tr>"; foreach ($_POST['order'] as $order) { if (!is_array($order)) continue; echo "<tr><td>".implode("</td><td>", $order )."</td></tr>"; } echo "</table> 
+7
source

The table class that I wrote a while ago

 <?php class Table { protected $opentable = "\n<table cellspacing=\"0\" cellpadding=\"0\">\n"; protected $closetable = "</table>\n"; protected $openrow = "\t<tr>\n"; protected $closerow = "\t</tr>\n"; function __construct($data) { $this->string = $this->opentable; foreach ($data as $row) { $this->string .= $this->buildrow($row); } $this->string .= $this->closetable; } function addfield($field, $style = "null") { if ($style == "null") { $html = "\t\t<td>" . $field . "</td>\n"; } else { $html = "\t\t<td class=\"" . $style . "\">" . $field . "</td>\n"; } return $html; } function buildrow($row) { $html .= $this->openrow; foreach ($row as $field) { $html .= $this->addfield($field); } $html .= $this->closerow; return $html; } function draw() { echo $this->string; } } ?> 

Used as follows:

 <body> <?php $multiDimArray = []; # Turn the form array into a matrix for ($i = 0; $i < count($_POST['order']); $i++) { $multiDimArray[] = []; foreach ($_POST['order'][$i] as $key=>$value) { if ($i == 0) { $multiDimArray[$i][] = $key; } $multiDimArray[$i][] = $value; } } $table = new Table($multiDimArray); # Create and draw the table $table->draw(); ?> </body> 
+6
source

Source: https://habr.com/ru/post/1304391/


All Articles