show database to html table tag

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
joola
Forum Newbie
Posts: 8
Joined: Mon May 26, 2008 10:51 pm

show database to html table tag

Post 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...
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: show database to html table tag

Post 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.
Post Reply