Page 1 of 1
show database to html table tag
Posted: Mon May 26, 2008 10:58 pm
by joola
Hi, I`m new here. Straight to my problem. I have designed a complicated table using html tag <table..></table> ect. If I create that table using php script is not easy and take long time. my question is, how to adding/showed database/table to frontend table and the table using html tag??
Thanks alot for all help...
Re: show database to html table tag
Posted: Tue May 27, 2008 7:31 am
by allspiritseve
What you first want to do is separate your business logic from your presentation logic... in your case, the code that fetches data from the database is your business logic. Your presentation logic is the code that displays a table. So you might have:
index.php (business logic)
Code: Select all
$stmt = $db->prepare ('SELECT * FROM posts WHERE post_id = :post_id');
$stmt->bindValue (':post_id', $post_id);
$stmt->execute();
$post = $stmt->fetch (PDO::FETCH_ASSOC);
include ('template.tpl');
template.tpl (presentation logic)
Code: Select all
<table><? foreach ($post as $key => $value): ?>
<th><?=$key; ?></th><td><?=$value; ?></td>
<? endforeach; ?></table>
Basically, <?=$key; ?> is shorthand for <?php echo $key; ?>. The latter is preferred, but I'm sure somebody could give you a better explanation for that than I could.
In any event, it's better to never have HTML in your business logic. In your presentation logic, you should almost always start with HTML, and then add in PHP like I did above. I assume you have been echoing out HTML... it's tricky business and hard to mantain.