In an php/html document could I create a loop to create html table rows as long as there is a rows to be read from a MySQL database table?
I could then populate the html table with the data but I first need to know if I can make a loop that will only run as longs as there are
more database rows to be read. Thanks.
lewashby
56
Junior Poster
Recommended Answers
Jump to PostYes - use concatenation. Example:
$output = ""; while($data = $stmt->fetch()) { $output .= "<tr><td>{$data['field1']}</td><td>{$data['otherfield']}</td></tr>"; }
Then:
<table> <thead> <tr> <th>Field 1 Heading</th> <th>OtherField Heading</th> </tr> </thead> <tbody> <?=$output?> </tbody> </table>
Jump to PostDiafol answer is (of course) accurate and enough. I hope he don't mind I wrote one more example using a very simple view component HTML generator class for tables.
<?php $database = "test"; $username = "testuser"; $password = "testpassword"; $db = new PDO("mysql:host=localhost;dbname=".$database , $username, $password); $statement …
All 5 Replies
diafol
jkon
602
Posting Whiz in Training
Featured Poster
diafol
commented:
Hey, I like it! :)
+15
diafol
jkon
602
Posting Whiz in Training
Featured Poster
diafol
commented:
Nice ;)
+0
lewashby
56
Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.