Lissa Explains it All:  Web Design Forums  

Go Back   Lissa Explains it All: Web Design Forums > LEIA Archives > Web Site Help > General

Notices

 
 
Thread Tools Display Modes
  #1  
Old 02-09-2003, 04:51 PM
Pange's Avatar
Pange Pange is offline
Magitek Girl
 
Join Date: Apr 2001
Location: Canada!
Posts: 2,924
Pange is an unknown quantity at this point
PHP - Loading a jpg if a gif does not exist.

Hey! I'm fairly new to writing PHP, but I've handled a few small scripts on my website. I wrote this script, but it doesn't seem to work the way I want it to.

PHP Code:
<?php
$game 
basename($game);
 { 
$game="$game"; } 
$image basename($image);
 { 
$image="$image"; } 

if(
file_exists("$image.jpg")) { 
$image "$image.jpg"
 echo  
"<img src=\"$game/$image\">"; }
else  { 
$image "$image.gif"
 echo  
"<img src=\"$game/$image\">"; } 

?>
so basically, this script looks to see if the image.jpg exists. if it does, it shows the image on the page. if it doesn't, it will look for the same file name, but with a gif extension. I'm using dynamic links, so game = the directory where the image is stored, and image = the file in the directory to be loaded.

http://www.magitek.nu/ode2ff/layoutv...=main&game=ff9 tries to load this image: http://www.magitek.nu/ode2ff/layoutv...y/ff9/main.jpg

http://www.magitek.nu/ode2ff/layoutv...e=box&game=ff9 tries to load this image: http://www.magitek.nu/ode2ff/layoutv...ry/ff9/box.gif

for some reason, it is able to work for gif files, but any jpgs do not work. It's not a problem with that one jpg file, because I tried it here as well and it does not work...

if anyone has any information on this to help, I will be very greatful. This is driving me insane, lol. thanks! =)
  #2  
Old 02-10-2003, 05:57 PM
pb&j's Avatar
pb&j pb&j is offline
phantom
 
Join Date: Dec 2001
Location: lurking about
Posts: 9,998
pb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to all
Should the periods be escaped in the filenames?

<?php

$game = basename($game);
{ $game="$game"; }
$image = basename($image);
{ $image="$image"; }

if(file_exists("$image\.jpg")) {
$image = "$image\.jpg";
echo "<img src=\"$game/$image\">"; }
else {
$image = "$image\.gif";
echo "<img src=\"$game/$image\">"; }

?>
__________________
phantom of the forum! mwa-ha-ha!
  #3  
Old 02-11-2003, 11:43 AM
Ökii's Avatar
Ökii Ökii is offline
pee wee
 
Join Date: Sep 2001
Location: UK
Posts: 545
Ökii is an unknown quantity at this point
due to only needing one method per if/else, you could use a tertiary there

PHP Code:
$game basename($game);
$image basename($image);
echo 
'<img src="'.$game.'/'.$image.';
echo (file_exists($game.'
/'.$image.'.jpg')) ? '.jpg" />' : '.gif" />'
if you still get snags, try echoing out the values of $game and $image to test they are set and accessible without going through a global array.

also - the {$game = "$game";} and img=$img lines should be removed rapidly
  #4  
Old 02-12-2003, 12:47 AM
Pange's Avatar
Pange Pange is offline
Magitek Girl
 
Join Date: Apr 2001
Location: Canada!
Posts: 2,924
Pange is an unknown quantity at this point
thanks for the replies, you two!

I tried pb&j's code first, and when I escaped the periods, the image properties said the file was main\.jpg. Thanks for trying anyways though

Okii, thanks for yours! I tried it, but I get an error, but I know it's picking up the $game and $image variables by echoing them. This is the error message I get:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/.hassansybian/magitek/magitek.nu/ode2ff/layoutv2/gallery/gallery.php on line 72

PHP Code:
69 <?php
70 $game 
basename($game);
71 $image basename($image);
72 echo '<img src="'.$game.'/'.$image.';
73 echo (file_exists($game.'
/'.$image.'.jpg')) ? '.jpg" />' : '.gif" />';
74 ?>
So I have to add a "," or a ";" but I'm kind of confused about where to put the mark...you can definatly tell I'm a newbie with this. Do you have any advice?

Thanks again for your help! =)
  #5  
Old 02-12-2003, 05:10 AM
pb&j's Avatar
pb&j pb&j is offline
phantom
 
Join Date: Dec 2001
Location: lurking about
Posts: 9,998
pb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to all
Does this work?

$gameimage = basename($game)."/".basename($image);

if (file_exists($gameimage.".jpg")){
echo ('<img src=$gameimage.".jpg">');
}else{
echo ('<img src=$gameimage.".gif">');
}
__________________
phantom of the forum! mwa-ha-ha!
  #6  
Old 02-12-2003, 11:43 AM
Ökii's Avatar
Ökii Ökii is offline
pee wee
 
Join Date: Sep 2001
Location: UK
Posts: 545
Ökii is an unknown quantity at this point
oops

72 echo '<img src="'.$game.'/'.$image;

always tend to make the odd mistake when flytyping stuff for forums.
  #7  
Old 02-12-2003, 01:26 PM
Pange's Avatar
Pange Pange is offline
Magitek Girl
 
Join Date: Apr 2001
Location: Canada!
Posts: 2,924
Pange is an unknown quantity at this point
Oh, it works perfectly! Thank you both for your help, I couldn't have done it without you
  #8  
Old 02-12-2003, 02:49 PM
pb&j's Avatar
pb&j pb&j is offline
phantom
 
Join Date: Dec 2001
Location: lurking about
Posts: 9,998
pb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to all
for the reference, which one worked? or both?
this may be handy on a future project.
thanks.
  #9  
Old 02-12-2003, 10:52 PM
Pange's Avatar
Pange Pange is offline
Magitek Girl
 
Join Date: Apr 2001
Location: Canada!
Posts: 2,924
Pange is an unknown quantity at this point
It was actually Ökii's that worked =)
  #10  
Old 02-13-2003, 12:25 AM
pb&j's Avatar
pb&j pb&j is offline
phantom
 
Join Date: Dec 2001
Location: lurking about
Posts: 9,998
pb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to allpb&j is a name known to all
Cool, thanks.
__________________
phantom of the forum! mwa-ha-ha!
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:51 AM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.