PHP: Create HTML Table Row from DB Quick and Easy

One of the things that I find myself doing is building tables to display data from a database query.

My preferred method of doing this is using a JQUERY Post call to a PHP script.

I won’t bore you with the PDO connection strings and database query and cut right to the chase:

/* do all your db connection stuff and query here */
/* The presumption here is that your $query will return the fields ID,Name and Some Interesting Data, IN THAT ORDER */

$output = '<table>';
$output .= '<tr><th>ID</th><th>Name</th><th>Some Interesting Data</th></tr>';
foreach($query as $row) {   

    $output .= '<tr><td>'.implode('</td><td>',$row).'</td></tr>';      //pretty slick eh?  No concatenating a bunch of cells


$output .= '</table>';

echo $output;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s