Controlling Rows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Controlling Rows

Post by psurrena »

Hello,

I would like to format the results pulled from my query. Right now it just lists the images since all I've included is the <br /> tag. My question is, how would I specify three images across and then start a new line?

Thanks,
-Pete

My code is as follows:

Code: Select all

<?php
include '../library/config.php';
include '../library/opendb.php';

$query  = "SELECT id, name FROM upload2";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
    echo "Database is empty <br>";
} 
else
{
    while(list($id, $name) = mysql_fetch_array($result))
    {
?>
    <img src="../upload/<?=$name;?>" /> <br />
    
<?php        
    }
}
include '../library/closedb.php'; 
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Look at the first one in the useful posts thread

viewtopic.php?t=29816
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

Thanks! The first post worked great. For other people, here is how my code looks:

Code: Select all

<?php
$usr = "*******";
$pwd = "*******";
$db = "*******";
$host = "*******";

# connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }

$output = '<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>';

# setup SQL statement
$SQL = " SELECT * FROM gallery ";
$SQL = $SQL .

# execute SQL statement
$retid = mysql_db_query($db, $SQL, $cid) or die(mysql_error());

# display results
$output .= "<P><DT><B>$category</B><BR>\n\n";
$output .= "<table>\n\n";
$howmany = mysql_num_rows($retid);
$rowmax = 2;
for($x = 0; $row = mysql_fetch_array($retid); $x++)
{
if($x % $rowmax == 0) 
$output .= "<tr>\n";

$img_name = $row["name"];

$output .= "<td><img src =\"../upload/$img_name\">$img_name</td>";
if($x % $rowmax == $rowmax - 1)
$output .= "\r</tr>\n\n";
}

if($left = (($howmany + $rowmax - 1) % $rowmax))
$output .= '<td colspan="' . $left . '">&nbsp' . ";</td>\n</tr>\n\n";

$output .= "</table>\n\n";

$output .= "</DT></P>";

$output .= '</BODY>
</HTML>';

echo $output;
?>
Post Reply