Jump to content

[SOLVED] PHP MySQL Diplay results in table with 4 columns.


nita

Recommended Posts

Hi.

 

I would like to display results from database in a table split in 4 columns and rows depending on number of the items to display. (in this case - dvd covers) [1 row x 4 images(columns) ]

 

Code submitted is displaying them all in 1 row,

 

<?
include "connectdb.php";

echo "
<table>
<tr>";

$result = mysql_query("SELECT * FROM movies WHERE cat='Adventure'") or die(mysql_error());
while($row = mysql_fetch_array($result)) 
{
echo "
	<td>
	<img src='http://www.nita-on-line.com/knmc/covers/";
	echo $row['cover'];
	echo ".jpg' border='0'>";
	echo "</td>";
}
echo "</tr>
</table>
";

?>

 

how can i do that ???

thank you for your advice in advance !

 

nita

Example

 

<?php
include 'db.php';

define ("NUMCOLS",4);

$res = mysql_query("SELECT col1, col2 FROM mytable");

$count = 0;
echo "<TABLE border=1>";
while (list($col1, $col2) = mysql_fetch_row($res)) {

    if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row

    echo "<TD>$col1<br>$col2</TD>\n";
    $count++;

    if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}

# end row if not already ended

if ($count % NUMCOLS != 0) {
   while ($count++ % NUMCOLS) echo "<td> </td>";
   echo "</TR>\n";
}
echo "</TABLE>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.