robertdd robertdd - 1 month ago 6
PHP Question

How to print data in a table from mysql

I want to extract the last eight entries from my database and print them into a two columns table like this:

|1|2|
|3|4|
|5|6|
|7|8|


Is that possible?

This is my code:

$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$db->connect();

$sql = "SELECT ID, movieno
FROM movies
ORDER BY ID DESC
LIMIT 8 ";

$rows = $db->query($sql);

print '<table width="307" border="0" cellspacing="5" cellpadding="4">';
while ($record = $db->fetch_array($rows)) {
$vidaidi = $record['movieno'];
print <<<END
<tr>
<td>
<a href="http://www.dadadada.com/watch?v=$vidaidi" target="_blank">

<img src="http://img.dadadada.com/vi/$vidaidi/1.jpg" width="123" height="80"></a>
</td>
</tr>
END;
}
print '</table>';

Answer

Yes it's possible.

<table border=1><tr>
<?
$count = 0;
$max = 4;
while(your loop){
 $count++;
echo '<td>'.$count.' record stuff </td>';

if($count >= $max){
  //reset counter
   $count = 0;
  //end and restart
   echo '</tr><tr>';
 }

}
?>
</tr></table>
Comments