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...
show database to html table tag
Moderator: General Moderators
- 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
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)
template.tpl (presentation logic)
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.
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');
Code: Select all
<table><? foreach ($post as $key => $value): ?>
<th><?=$key; ?></th><td><?=$value; ?></td>
<? endforeach; ?></table>
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.