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
https://forums.phpfreaks.com/topic/120845-if-statement-help-please/
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?!

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??

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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.