Display Results in a Table
Moderator: General Moderators
Display Results in a Table
I have a table for storing pictures. The fields are id (auto inc), Uname, pic_title, pic_location
How would i make the results echo into a table where it displays 3 pictures wide (so 3 <td>) then goes to the next row to display the next three?
Thanks.
How would i make the results echo into a table where it displays 3 pictures wide (so 3 <td>) then goes to the next row to display the next three?
Thanks.
The proper way:
The rubbish way:
Code: Select all
while ($record = mysql_fetch_object($result)) {
echo "<div style=\"float: left; width: 33%; text-align: center;\"><img src=\"".$record->pic_location."\"></div>";
}Code: Select all
while ($record = mysql_fetch_object($result)) {
if ($counter++%3==0) { echo "<tr>"; }
echo "<td align=\"center\"><img src=\"".$record->pic_location."\"></td>";
if ($counter%3==0) { echo "</tr>"; }
}
if ($counter<3) {
echo "</tr>";
}Here is what i got and im getting no results:
Code: Select all
<td><? include("config.php");
#connect to MySQL
$conn=@mysql_connect("$DBHOST", "$DBUSER", "$DBPASS")
or die("Err:Conn");
#select the specified database
$rs = @mysql_select_db("$DBNAME", $conn)
or die("Err:Db");
#create the query
$sql="SELECT * FROM gallery where Uname='$user'";
#execute the query
$rs=mysql_query($sql,$conn);
#write the data
while( $row = mysql_fetch_array($rs) )
{ ?><?php while ($record = mysql_fetch_object($result)) {
if ($counter++%3==0) { echo "<tr>"; }
echo "<td align=\"center\"><img src=\"".$record->pic_location."\"></td>";
if ($counter%3==0) { echo "</tr>"; }
}
if ($counter<3) {
echo "</tr>";
} ?><?php } ?></td>so what would happen if someone entered the below into the address bar?
gallery.php?user=%27+OR+%27%27+%3D+%27
Anyway.. that is a different (yet very important) topic.
change your php to:
gallery.php?user=%27+OR+%27%27+%3D+%27
Anyway.. that is a different (yet very important) topic.
change your php to:
Code: Select all
<?php
include("config.php");
//connect to MySQL
$conn = mysql_connect("$DBHOST", "$DBUSER", "$DBPASS") or die("Err:Conn");
//select the specified database
$rs = mysql_select_db("$DBNAME", $conn) or die("Err:Db");
//create the query
$sql = "SELECT * FROM `gallery` WHERE `Uname` = '". mysql_real_escape_string($_GET['user']) . "'";
//execute the query
$rs=mysql_query($sql, $conn);
//write the data
$counter = 0;
while ($row = mysql_fetch_array($rs)) {
if (($counter % 3) == 0) {
echo "<tr>";
}
echo "<td align=\"center\"><img src=\"{$record['pic_location']}\"></td>";
if (($counter % 3) == 0) {
echo "</tr>";
}
$counter++;
}
?>That works, but it doesnt display them properly. It displays the second image below the first image and the third image to the very far right. (see http://www.open-blog.org/myopenspace2/g ... er=uberamd )The code i have is:
Code: Select all
<table width="100%" border="0" cellpadding="0" cellspacing="0"><?php
include("config.php");
//connect to MySQL
$conn = mysql_connect("$DBHOST", "$DBUSER", "$DBPASS") or die("Err:Conn");
//select the specified database
$rs = mysql_select_db("$DBNAME", $conn) or die("Err:Db");
//create the query
$sql = "SELECT * FROM `gallery` WHERE `Uname` = '". mysql_real_escape_string($_GET['user']) . "'";
//execute the query
$rs=mysql_query($sql, $conn);
//write the data
$counter = 0;
while ($row = mysql_fetch_array($rs)) {
if (($counter % 3) == 0) {
echo "<tr>";
}
echo "<td align=\"center\"><img src=\"pubimages/thumbs/thumb_{$row['pic_location']}\"></td>";
if (($counter % 3) == 0) {
echo "</tr>";
}
$counter++;
}
?></table>Your counter increment is in the wrong place. If you look at the code I first posted you'll see that the first modulus check has the increment built into it:
If you think about it for a moment I'm sure you'll figure out why.
By the way .. your 'open server' link in your sig is returning an error.
Code: Select all
if ($counter++%3==0) { echo "<tr>"; }By the way .. your 'open server' link in your sig is returning an error.
Yeah i believe that does make sense to me after i looked at it closely. But what needs to be done to the bottom one:
Code: Select all
if (($counter % 3) == 0) {
echo "</tr>";
}