Display Results in a Table

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
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Display Results in a Table

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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>";
}
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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>
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

$user = ?

echo your query so you can verify it.

and don't use register globals, particularly with SQL!!!!112onetwelve
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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++;
}
?>
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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>"; 
    }
Post Reply