function array_to_table($array, $recursive = false, $null = ' ') { // Sanity check if (empty($array) || !is_array($array)) { return false; } if (!isset($array[0]) || !is_array($array[0])) { $array = array($array); } // Start the table $table = "<table>\n"; // The header $table .= "\t<tr>"; //Add an abitrary serial number S/N $table .= '<th>S/N</th>'; // Take the keys from the first row as the headings foreach (array_keys($array[0]) as $heading) { $table .= '<th>' . $heading . '</th>'; } $table .= "</tr>\n"; // The body $i = 0; foreach ($array as $row) { $table .= "\t<tr>"; foreach ($row as $cell) { $table .= '<td>' . ++$i . '</td>'; $table .= '<td>'; // Cast objects if (is_object($cell)) { $cell = (array) $cell; } if ($recursive === true && is_array($cell) && !empty($cell)) { // Recursive mode $table .= "\n" . array2table($cell, true, true) . "\n"; } else { $tooltip = "gettype(): " . strtoupper(gettype($cell)); $table .= '<span title="' . $tooltip . '">' . (strlen($cell) > 0 ? htmlspecialchars((string) $cell) : $null) . '</span>'; } $table .= '</td>'; } $table .= "</tr>\n"; } $table .= '</table>'; return $table; }
/** * Translate a result array into a HTML table * * @author Aidan Lister <*****@*****.**> * @version 1.3.2 * @link http://aidanlister.com/repos/v/function.array2table.php * @param array $array The result (numericaly keyed, associative inner) array. * @param bool $recursive Recursively generate tables for multi-dimensional arrays * @param string $null String to output for blank cells */ function array2table($array, $recursive = false, $null = ' ') { // Sanity check if (empty($array) || !is_array($array)) { return false; } if (!isset($array[0]) || !is_array($array[0])) { $array = array($array); } // Start the table $table = "<table>\n"; // The header $table .= "\t<tr>"; // Take the keys from the first row as the headings foreach (array_keys($array[0]) as $heading) { $table .= '<th>' . $heading . '</th>'; } $table .= "</tr>\n"; // The body foreach ($array as $row) { $table .= "\t<tr>"; foreach ($row as $cell) { $table .= '<td>'; // Cast objects if (is_object($cell)) { $cell = (array) $cell; } if ($recursive === true && is_array($cell) && !empty($cell)) { // Recursive mode $table .= "\n" . array2table($cell, true, true) . "\n"; } else { $table .= strlen($cell) > 0 ? htmlspecialchars((string) $cell) : $null; } $table .= '</td>'; } $table .= "</tr>\n"; } $table .= '</table>'; return $table; }
if ($result = $mysqli->store_result()) { $ary_results[$result_set_id] = $result; // Increment the result set $result_set_id++; } } while ($mysqli->more_results() && $mysqli->next_result()); // Get the data from the result set. if ($ary_results) { while ($row = $ary_results[0]->fetch_assoc()) { $array[] = $row; } } if (count($array) < 1) { $tabledata = "No Records"; } else { $tabledata = array2table($array); // Will output a table of 600px width } //Verify status and prepare output if ($mysqli->errno == 0) { // Success $dbStatus = "ok"; $dbData = array("user_id" => $mysqli->insert_id, "client_id" => $client_id); } else { // Error $dbStatus = "err"; $dbData = array("errno" => $mysqli->errno, "error" => $mysqli->error, "client_id" => $client_id); } $additionalCSS .= <<<EOD <link href= <link href="../../css/bootstrap.min.css" rel="stylesheet" media="screen"/> <link href="../../css/litesprite.css" rel="stylesheet" media="screen"/>
<tr> <td style="width:230px;"><div id="left"> <div class="colhead">Common Tasks</div> <ul style="margin: 10px 0 0"> <li><a href="volumerequest.php">Request Storage Volume</a></li> <li><a href="storagenodelist.php">Storage Node Status</a></li> <li><a href="removevolume.php">Delete Volume</a></li> </ul> </div></td> <td><div id="middle"> <h1>Database:</h1> <?php if (!isset($volumeid)) { echo "Machine:<br />\n"; array2table($machine, "100%", FALSE, TRUE); echo "Mapping:<br />\n"; array2table($mapping, "100%"); echo "Volume:<br />\n"; array2table($volume, "100%", TRUE); } else { echo "Mapping: Volume ID {$volumeid}<br />\n"; array2table($idresult, "100%"); } ?> </div></td> </tr> </table> </div> </body> </html>
/** * Convert a PHP array to HTML table * * @param array $array the associative array to be converted * @param boolean $transpose whether to show keys as rows instead of columns. * This parameter should be used only for a single dimensional associative array. * If used for a multidimensional array, the sub array will be imploded as text. * @param boolean $recursive whether to recursively generate tables for multi-dimensional arrays * @param boolean $typeHint whether to show the data type as a hint * @param string $null the content to display for blank cells * @param array $tableOptions the HTML attributes for the table * @param array $keyOptions the HTML attributes for the array key * @param array $valueOptions the HTML attributes for the array value * * @return string|boolean */ public static function array2table($array, $transpose = false, $recursive = false, $typeHint = true, $tableOptions = ['class' => 'table table-bordered table-striped'], $keyOptions = [], $valueOptions = ['style' => 'cursor: default; border-bottom: 1px #aaa dashed;'], $null = '<span class="not-set">(not set)</span>') { // Sanity check if (empty($array) || !is_array($array)) { return false; } // Start the table $table = Html::beginTag('table', $tableOptions) . "\n"; // The header $table .= "\t<tr>"; if ($transpose) { foreach ($array as $key => $value) { if ($typeHint) { $valueOptions['title'] = self::getType(strtoupper($value)); } if (is_array($value)) { $value = '<pre>' . print_r($value, true) . '</pre>'; } else { $value = Html::tag('span', $value, $valueOptions); } $table .= "\t\t<th>" . Html::tag('span', $key, $keyOptions) . "</th>" . "<td>" . $value . "</td>\n\t</tr>\n"; } $table .= "</table>"; return $table; } if (!isset($array[0]) || !is_array($array[0])) { $array = array($array); } // Take the keys from the first row as the headings foreach (array_keys($array[0]) as $heading) { $table .= '<th>' . Html::tag('span', $heading, $keyOptions) . '</th>'; } $table .= "</tr>\n"; // The body foreach ($array as $row) { $table .= "\t<tr>"; foreach ($row as $cell) { $table .= '<td>'; // Cast objects if (is_object($cell)) { $cell = (array) $cell; } if ($recursive === true && is_array($cell) && !empty($cell)) { // Recursive mode $table .= "\n" . array2table($cell, true, true) . "\n"; } else { if (!is_null($cell) && is_bool($cell)) { $val = $cell ? 'true' : 'false'; $type = 'boolean'; } else { $chk = strlen($cell) > 0; $type = $chk ? self::getType($cell) : 'NULL'; $val = $chk ? htmlspecialchars((string) $cell) : $null; } if ($typeHint) { $valueOptions['title'] = $type; } $table .= Html::tag('span', $val, $valueOptions); } $table .= '</td>'; } $table .= "</tr>\n"; } $table .= '</table>'; return $table; }
<html> <head> <meta charset="utf-8"> <title>eBASE | Developer Application</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> </head> <body> <p id="titre">eBASE Developer Application Test</p> <table> <tr><th>ID</th><th>First Name</th><th>Last Name</th><th>Email Address</th><th>Get Email!</th></tr> <?php array2table($people); ?> </table> <script> $(document).ready(function(){ $("button").click(function(){ var fristName = $(this).closest("tr").children("td:eq(1)").text(); var lastName = $(this).closest("tr").children("td:eq(2)").text(); var email = $(this).closest("tr").children("td:eq(3)").text(); alert("Name: " + fristName + " " + lastName + "\nEmail: " + email); }); });