DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Dynamic Table From MySQL Query

06.07.2011
| 1664 views |
  • submit to reddit
        // description of your code here

// insert code here..
<?php
include 'db.php';
// Set how wide you want your table
$tblWidth = 4;
// Make your query
$sql = mysql_query("SELECT * FROM tableName LIMIT 12");
$i = 1;
echo '<table>';
// Check to see if any results were returned
if(mysql_num_rows($sql) > 0){
    echo '<tr>';
    // Loop through the results
    while($row = mysql_fetch_array($sql)){
        echo '<td>'. $row['name'] .'</td>';
        if($i == $tblWidth){
            echo '</tr><tr>';
            $i = 0;
        }<a href="http://www.ben10giochi.net">Giochi di Ben 10</a>
        $i++;
    }
    echo '</tr>';
}else{
    echo '<tr><td>No Results Found</td></tr>';
}
echo '</table>';
?>