image display in loop

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
mrpaulfrank
Forum Commoner
Posts: 33
Joined: Sun Jun 23, 2002 3:39 am

image display in loop

Post by mrpaulfrank »

so far, this is what i have:

<?php require_once('Connections/connection.php'); ?>
<?php
mysql_select_db($database_db, $db);
$query_Recordset1 = "SELECT * FROM test";
$Recordset1 = mysql_query($query_Recordset1, $db) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_select_db($database_db, $db);

$genre = "comedy";
$query_Recordset1 = "SELECT * FROM test WHERE genre = '$genre'";
$Recordset1 = mysql_query($query_Recordset1, $db) or die(mysql_error());
while($row_Recordset1 = mysql_fetch_assoc($Recordset1))
{
echo "<A href=backs/$back><IMG SRC=images/$image></A>"."<br>\n".
"Title: ".$row_Recordset1['title']."<br>\n".
"Format: ".$row_Recordset1['format']."<br>\n".
"Price: ".$row_Recordset1['make']."<br><br>\n\n";
}
$image = $row_Recordset1['image']
?>

<html>
<head>

i want the image to be displayed along with the other text information for each unique result in the loop. what am i doing wrong?
g-force2k2
Forum Newbie
Posts: 12
Joined: Thu Apr 18, 2002 8:15 pm

not positive ;\

Post by g-force2k2 »

mrpaulfrank wrote:echo "<A href=backs/$back><IMG SRC=images/$image></A>"."<br>\n".
"Title: ".$row_Recordset1['title']."<br>\n".
"Format: ".$row_Recordset1['format']."<br>\n".
"Price: ".$row_Recordset1['make']."<br><br>\n\n";
}

?>
im not a pro at php but first i'd put this line:

$image = $row_Recordset1['image']

before the:

echo "<A href=backs/$back><IMG SRC=images/$image></A>"."<br>\n".

just because of the fact that $image hasn't been defined yet ;\

second if you want it displayed as a certain image... just change this:

echo "<A href=backs/$back><IMG SRC=images/$image></A>"."<br>\n".

to this:

echo "<A href=backs/$back><IMG SRC=images/$image.gif></A>"."<br>\n".

here you add a simple .gif extension or you can use whateva you like ;) but thats so then when inputting images you don't need an extension...

but i could be wrong anyways ;\

g-force2k
mrpaulfrank
Forum Commoner
Posts: 33
Joined: Sun Jun 23, 2002 3:39 am

got it

Post by mrpaulfrank »

thanks for the help...i ended up figuring it out. it took a little intuition. what was happening before and with your suggestion was a static definition of $image. if it were before the loop...if would define it with the first one found in the db. this is what i had to end up doing (thank you beginning php4 by wrox):

while($row_Recordset1 = mysql_fetch_assoc($Recordset1))
{
$image = $row_Recordset1['image'];
if($genre = "comedy")
{
echo "<A href=backs/$image><IMG SRC=images/$image></A>"."<br>\n".
"Title: ".$row_Recordset1['title']."<br>\n".
"Format: ".$row_Recordset1['format']."<br>\n".
"Price: ".$row_Recordset1['make']."<br><br>\n\n";
}
}

this way...every time the loop is executed it redefines the $image before the html tag uses it for displaying the image. the book didnt pop out and say this, actually it says literally nothing about how to display images using php (from files) if there even is a way without html tags, but i looked harder into the loops section and found something.
Post Reply