note 122424 deleted from function.implode by peehaa
| From: | peehaa@php.net | Date: | Sun, 28 Apr 2019 10:49:44 +0000 |
| Subject: | note 122424 deleted from function.implode by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210821@lists.php.net to get a copy of this message | ||
Note Submitter: jack dot r dot davis dot jr at gmail dot com
----
A handy use for implode():
<?php
function tablerow ($array)
{
$rv = "<tr><td>" . implode("</td><td>" , $array) .
"</td></tr>";
return($rv);
}
function tablehead($array)
{
$rv = "<tr><th>" . implode("</th><th>" , $array) .
"</th></tr>";
return($rv);
}
$db = mysqli_connect("localhost","user","password",
"dbname");
$sql = "select id, name, city from users";
$query = mysqli_query($db, $sql);
while ($r = mysqli_fetch_array($query, MYSQLI_ASSOC))
{
if (!$keys)
$keys = array_keys($r);
$rows[] = tablerow($r);
}
$headers = tablehead($keys);
$rows = implode("", $rows);
echo "<table>" . $headers . $rows. "</table>";
?>