Page 1 of 1

Simple Question on Syntax

Posted: Thu Sep 18, 2008 8:00 am
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>

Re: Simple Question on Syntax

Posted: Thu Sep 18, 2008 8:11 am
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

Re: Simple Question on Syntax

Posted: Thu Sep 18, 2008 10:32 am
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

Re: Simple Question on Syntax

Posted: Thu Sep 18, 2008 11:00 am
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>
 

Re: Simple Question on Syntax

Posted: Thu Sep 18, 2008 11:24 am
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,