Jump to content

if statement help please


jacko_162

Recommended Posts

i need help with an if statement.

 

at the moment i display 6 images like so;

 

<a rel="lightbox" href="images/products/<? echo $ID ?>one.jpg"><img src="images/products/<? echo $ID ?>one.jpg" width="50" height="50" border="0"></a>
<a rel="lightbox" href="images/products/<? echo $ID ?>two.jpg"><img src="images/products/<? echo $ID ?>two.jpg" width="50" height="50" border="0"></a>
<a rel="lightbox" href="images/products/<? echo $ID ?>three.jpg"><img src="images/products/<? echo $ID ?>three.jpg" width="50" height="50" border="0"></a>
<a rel="lightbox" href="images/products/<? echo $ID ?>four.jpg"><img src="images/products/<? echo $ID ?>four.jpg" width="50" height="50" border="0"></a>
<a rel="lightbox" href="images/products/<? echo $ID ?>five.jpg"><img src="images/products/<? echo $ID ?>five.jpg" width="50" height="50" border="0"></a>
<a rel="lightbox" href="images/products/<? echo $ID ?>six.jpg"><img src="images/products/<? echo $ID ?>six.jpg" width="50" height="50" border="0"></a>

 

i was wondering if i could change each one to an if statement as not every page i display has six images, and i get the box with a red "X" in when there isnt an image..

 

and adding an else statement also so if the image doesnt exists it wont display anything.

 

help will be appreciated,

 

Regards,

Jacko :)

Link to comment
Share on other sites

I had an issue with images existing but having null values and in that case file_exists() wasn't adequate, so I did this instead...

    
$myimage = getimagesize($pathToFile);
if ($myimage != "") {
    // display the image
}

Link to comment
Share on other sites

i tried to use the file_exists php and typed the following;

 

$filename = '/images/products/<? echo $ID ?>one.jpg';

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

 

but it doesnt seem to pull the <? echo &ID ?>

 

it is in the loop for the Get $ID though?!

Link to comment
Share on other sites

thank you, thats now fixed :)

 

but i tried to echo the image and <a> tags and now i get error;

 

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /homepages/28/d208931384/htdocs/site/product.php on line 131

 

<?php
$filename = "images/products/".$ID."one.jpg";

if (file_exists($filename)) {
    echo "<a rel="lightbox" href="images/products/<? echo $ID ?>one.jpg"><img src="images/products/<? echo $ID ?>one.jpg" width="50" height="50" border="0"></a>";
} else {
    echo "";
}
?>

 

any ideas what i have done wrong??

Link to comment
Share on other sites

Your syntax is really bad. Are you new to PHP?

You do not include script tags <? ?> within a script.

You also need to escape quotes or it will see it as the end of a string.

 

echo "<a rel=\"lightbox\" href=\"images/products/".$ID."one.jpg\"><img 

 

Clean up the rest of your code.

Link to comment
Share on other sites

yes i am new to PHP :)

 

still learning.

 

so i need to convert this;

 

<a rel="lightbox" href="/images/products/".$ID."one.jpg"><img src="/images/products/".$ID."one.jpg" width="50" height="50" border="0"></a>

 

to this;

 

<a rel=\"lightbox\" href=\"images/products/".$ID."one.jpg\"><img src=\"images/products/".$ID."one.jpg\" width=\"50\" height=\"50\" border=\"0\"></a>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.