jacko_162 Posted August 22, 2008 Share Posted August 22, 2008 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 Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 22, 2008 Share Posted August 22, 2008 if(file_exists($pathToFile)) { // display the image } Quote Link to comment Share on other sites More sharing options...
webent Posted August 22, 2008 Share Posted August 22, 2008 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 } Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted August 22, 2008 Author Share Posted August 22, 2008 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?! Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 22, 2008 Share Posted August 22, 2008 Bad syntax! Should be: $filename = "/images/products/".$ID."one.jpg"; Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted August 22, 2008 Author Share Posted August 22, 2008 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?? Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 22, 2008 Share Posted August 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted August 22, 2008 Author Share Posted August 22, 2008 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> Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 22, 2008 Share Posted August 22, 2008 Correct. I prefer to use mixed quotes so as not to escape the character i.e $id = 1; $string = "<a href='index.php?pageId=".$id."'></a>"; print $string; Quote Link to comment Share on other sites More sharing options...
jacko_162 Posted August 22, 2008 Author Share Posted August 22, 2008 so like this; <a rel='lightbox' href='images/products/".$ID."one.jpg'><img src='images/products/".$ID."one.jpg' width='50' height='50' border='0'></a> Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted August 22, 2008 Share Posted August 22, 2008 Correct. So: $ID = 123; echo "<a rel='lightbox' href='images/products/".$ID."one.jpg'><img src='images/products/".$ID."one.jpg' width='50' height='50' border='0'>"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.