Simple Question on Syntax

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
KevenM
Forum Newbie
Posts: 3
Joined: Thu Sep 18, 2008 7:55 am

Simple Question on Syntax

Post by KevenM »

I have zero training with PHP, rather choosing to cut and paste various functions that I see in code when I need to modify an existing file.

In this case, I'm trying to set up a function where if a user did not upload a picture $img , then a placeholder image is put in place instead. This is the code I put in, but what's happening is that the placeholder image shows up regardless if the user uploaded a pic or not. What am I doing wrong?

Code: Select all

 
<div>
  <!-- IF <?php echo $img; ?> -->
    <?php echo $img; ?>
  <!-- ELSE --><img src="placeholderimageurlandfilename.gif">
  <!-- ENDIF -->
</div>
adroit
Forum Commoner
Posts: 37
Joined: Fri Aug 08, 2008 1:25 am
Location: India
Contact:

Re: Simple Question on Syntax

Post by adroit »

Hi Friend,

Use this function

function returnImage($imgName, $path)
{
$fullPath = $path.$imgName;
if (($imgName != "") && file_exists($fullPath))
return $fullPath;
else
return $path."placeholderimageurlandfilename.gif";
}

$img = returnImage("temp.jpg", "images/")

Assign $img to <img> tag src

Regards
KevenM
Forum Newbie
Posts: 3
Joined: Thu Sep 18, 2008 7:55 am

Re: Simple Question on Syntax

Post by KevenM »

I somewhat understand a little about what you wrote there, but I'm unsure which parts I'm supposed to substitute with my path and image name

Thanks again for the prompt reply
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Simple Question on Syntax

Post by Jade »

Code: Select all

 
<?php
function returnImage($imgName, $path)
{
$fullPath = $path.$imgName;
if (($imgName != "") && file_exists($fullPath))
return $fullPath;
else
return $path."placeholderimageurlandfilename.gif";
}
 
$img = returnImage("image_user_uploaded(if_any).jpg", "images/");
?>
 
<div>
<img src="<?php echo $img; ?>">
 </div>
 
KevenM
Forum Newbie
Posts: 3
Joined: Thu Sep 18, 2008 7:55 am

Re: Simple Question on Syntax

Post by KevenM »

Thanks for all the help everyone

I'm still having trouble, I suspect with this line:

Code: Select all

$img = returnImage("image_user_uploaded(if_any).jpg", "images/");
Specifically, if I put in temp.jpg or image_user_uploaded(if_any).jpg or whatever.jpg as the name changes with each listing. Every time someone uploads a file, it has the name format of 19_img.jpg where '19' changes to whatever number listing they happen to be. This number is dynamic.

For clarification, the user uploaded image goes to the folder /images/com_sobi2/clients/
I've also placed the nopic.gif image in this folder to simplify things.

So I'm this far right now:

Code: Select all

 
<?php
function returnImage($imgName, $path)
{
$fullPath = $path.$imgName;
if (($imgName != "") && file_exists($fullPath))
return $fullPath;
else
return $path."nopic.gif";
}
 
$img = returnImage("dynamicimagenameplaceholdershouldgoherebutIdontknowwhatthatis", "images/com_sobi2/clients/");
?>
 
<div>
<img src="<?php echo $img; ?>">
</div>
 
Thanks again,
Post Reply