Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatonation syntax

Status
Not open for further replies.

revone

Technical User
Mar 10, 2005
3
US
I have a database in which only some of the records have a photo associated with them. If the photo exists, I want to display it. If there is no photo, I don't want to display anything.

My images are located in the folder called "../images/recipes/320/". Each image is named according to the record ID, i.e., the image for Recipe_ID=1 is called "../images/recipes/320/1.JPG" and the image for Recipe_ID=2 is "../images/recipes/320/2.JPG" etc. I am having trouble concatonating the file name in php. The code always indicates that the file does not exist, even when it does.

Code:
if
(file_exists("../images/recipes/320/". $row_rs_recipe['Recipe_ID']."JPG"))
{echo("<img src=\"../images/recipes/320/". $row_rs_recipe['Recipe_ID']."JPG\">");} 
else 
{echo ("");}
 
you need to include the dot for ".jpg" inside the quotes...

Code:
if
(file_exists("../images/recipes/320/". $row_rs_recipe['Recipe_ID'].".JPG"))
{echo("<img src=\"../images/recipes/320/". $row_rs_recipe['Recipe_ID'].".JPG\">");}
else
{echo ("");}
 
Ok, I have several suggestions here:
1: Swap for is_file()
2: Implement clearstatcache(), as the results are cached!

Code:
$_GET['picroot'] = "../images/recipes/320/";
$_GET['current_pic'] = "{$_GET['picroot']}{$row_rs_recipe['Recipe_ID']}.JPG";

if (is_file($GET['current_pic']) {
    echo "<p title=\"Pictures Uploaded by user\" class=\"usr_pic\"><img src=\"{$_GET['current_pic']}\" /></p>";
  }
else {
  echo ("<p title=\"Pictures Uploaded by user\" class=\"usr_pic\"><strong>This user did not upload any files yet!</strong></p>");
}

ps. there might be some typo's there, but I guess you get the general idea.

I would also include a title field in the picture-table in your database, so you can parse something in title="" in the <img /> tags.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top