Page 1 of 1
Display Results in a Table
Posted: Sat Dec 17, 2005 5:05 pm
by waradmin
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.
Posted: Sat Dec 17, 2005 5:16 pm
by onion2k
The proper 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>";
}
The rubbish way:
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>";
}
Posted: Sat Dec 17, 2005 6:20 pm
by waradmin
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>
Posted: Sun Dec 18, 2005 1:06 am
by Jenk
$user = ?
echo your query so you can verify it.
and don't use register globals, particularly with SQL!!!!112onetwelve
Posted: Sun Dec 18, 2005 2:16 am
by waradmin
$user is the persons profile your viewing. For example gallery.php?user=uberamd it would go into gallery and find where Uname is uberamd to get uberamd's images.
Posted: Sun Dec 18, 2005 2:25 am
by Jenk
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:
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++;
}
?>
Posted: Sun Dec 18, 2005 10:49 am
by waradmin
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>
Posted: Sun Dec 18, 2005 10:55 am
by onion2k
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:
Code: Select all
if ($counter++%3==0) { echo "<tr>"; }
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.
Posted: Sun Dec 18, 2005 11:12 am
by waradmin
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>";
}