Forum Moderators: coopster

Message Too Old, No Replies

else {

Reload page.

         

D_Blackwell

8:28 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have wrapped the content block so that it will not display if $selected_image does not exist, and this works fine. What I want to add is an else statement that will reload the page if the image is not found. How do I finish this off?

<?php if(isset($image_size)) {?>
<div id="image">
<img src="<?php echo $selected_image;?>" <?php echo $image_size[3];?> alt="<?php echo $caption;?>" title="" />
</div>
<div id="caption">
<?php echo $caption;?>
</div>
<?php }?>

londrum

8:35 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if you reload the page then it will just rerun the same function again - and the image still won't be there. so the page will load over and over.

D_Blackwell

10:05 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm loading a random image with an associated caption/description block. As it stands - if an image were for some reason unavailable, only a blank page with a black background will display. This is the only content on the page. If the page reloads, another random selection will be be made, so it should never expose the flaw. F5 does the trick, but I want it to happen automatically - not that I actually anticipate an image being unavailable; just to be safe, as there is large number in the array.

cameraman

1:03 am on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know what your code looks like so maybe it's not possible, but why not catch the file's nonexistence higher up so you can rerun the random section?

In direct answer to your question, if you want to do it server-side,
else {
header("Location: samepage.php\n");
exit;
}

or if you want to do it client-side, you could include some javascript (you could do it in the else so it doesn't get sent to the browser if everything's ok) that would attach to the onload event and change the location.href. Server-side would be slicker since you wouldn't see the page start to load and then 'hiccup'.

brevetoxin

1:17 am on Sep 19, 2007 (gmt 0)

10+ Year Member



I don't know what your code looks like so maybe it's not possible, but why not catch the file's nonexistence higher up so you can rerun the random section?

I agree. Wouldn't it be more efficient to start a loop of random selections? Then if the image exists, you exit the loop. If it doesn't exist, you return to the beginning.

Something like this pseudocode:

do
{
select random image
}
while(image does not exist)

D_Blackwell

1:55 am on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cameraman - For direct solution, the added else does the trick. (Don't expect any problems with missing images and such, but am trying play defense:)) Is the \n necessary? What is the reasoning there? The solution seems to work fine with or without.?

brevetoxin - I'm shot for tonight and will look at your suggestion tomorrow when fresher. My PHP skills are weak I admit, but I am making a concerted effort to immerse myself and soak up what I can.

This one is super simple, I'm just trying to cover the possibility of typ0 or missing files. There are quite a goodly number in rotation, so a mistake could slip through. (Mine, or somebody elses.)

<?php
$images = array(
array( 'file' => 'a',
'caption' => 'Caption/Description.'
),
array( 'file' => 'b',
'caption' => 'Caption/Description.'
),
array( 'file' => 'c',
'caption' => 'Caption/Description.'
)//ETC.
);
$i = rand(0, count($images)-1);
$selected_image = "images/{$images[$i]['file']}.jpg";
$caption = $images[$i]['caption'];

if (file_exists($selected_image) && is_readable($selected_image)) {
$image_size = getimagesize($selected_image);
}
?>

cameraman

7:07 am on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the \n necessary?

Y'know, I dono. I saw it somewhere when I was first starting to use php and didn't know what I was doing, so I've always done it that way (always an excellent reason to do something <grin>).

You're in prime shape for a 'no-fail' loop:

do {
$i = rand(0, count($images)-1);
$selected_image = "images/{$images[$i]['file']}.jpg";
$caption = $images[$i]['caption'];
}while(!(file_exists($selected_image) && is_readable($selected_image)));

$image_size = getimagesize($selected_image);

eelixduppy

1:08 pm on Sep 19, 2007 (gmt 0)



Look at the string documentation [us2.php.net] for escaped characters.

\n
is a linefeed.

D_Blackwell

3:03 pm on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cameraman - Looks to me like I can also drop the exit;

This seems to do the trick perfectly if there is a 'missing' file. (Is there is reason that it would be more prudent to retain exit;?) Interestingly, it tests perfectly in use on server, but not localhost - the page won't reload. Why is this?


else {
header("Location: samepage.php");
}

Need to get some paying work done now and hope to connect the dots with the do/while suggestions this evening.

eelixduppy

3:12 pm on Sep 19, 2007 (gmt 0)



>> but not localhost - the page won't reload. Why is this?
The page won't reload if you are redirecting to a page that doesn't exist? It will come up with a 404 error. If you want to be sure that the file exists before you redirect to it, you should use something like file_exists [us3.php.net] first.

Also, the exit is added there to absolutely make sure that no code gets executed below that line.

cameraman

6:00 pm on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You definitely want to keep the exit. Othewise it will work 'most' of the time but on occasion won't. I've seen that, so it's not just blind faith.

I believe technically you're supposed to supply header location with an absolute url (someone correct me if I'm wrong), although most of us have discovered that it generally works with a relative one. You might try an absolute url there and you might also try adding back in a \n or two (I've actually always used two, again, because that's just the way I saw it).

borntobeweb

7:15 pm on Sep 19, 2007 (gmt 0)

10+ Year Member



If your only goal is to make sure you select a good image file, i vote for the loop method mentioned by brevetoxin and cameraman. Getting the client's browser to reload the page seems pretty inefficient to me, and if your code breaks for some reason and you can't find any good images, you might even make the browser hang. Building on cameraman's code:

$ntries = 0;
do {
$i = rand(0, count($images)-1);
$selected_image = "images/{$images[$i]['file']}.jpg";
$image_size = @getimagesize($selected_image);
} while ($image_size === false && $ntries++ < count($images));
if($image_size === false) {
Give up after trying as many images as in the array
}

You don't really need to call file_exists() since getimagesize() does that for you. The "@" in front getimagesize() suppresses the warning if the file is not found. The check for $ntries ensures you don't get into an infinite loop.

eelixduppy

7:18 pm on Sep 19, 2007 (gmt 0)



>> You don't really need to call file_exists() since getimagesize() does that for you

hehe, yes it does. For some reason I misinterpreted thinking that the Location header was being changed to a file that doesn't exist.

D_Blackwell

10:02 pm on Sep 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code that I started with (See Post #1) was trying to handle potential errors (or at least display of messages) by wrapping the HTML with PHP. Everything was working fine, but I was looking to address the possibility that an image file would be missing, or misnamed. No error was generated, but no image either. My first thought was to add an else statement to that block, which would reload the page and seamlessly move on.

cameraman's suggestion for this method worked perfectly. Creating a test situation where several image files would not be found, the next available random image was immediately selected. No one would ever know. (I did drop the \n without incident.) (Though I did not figure out why it reloaded on the server, but not on localhost. I checked and double checked every which way, but it just wouldn't reload automatically. F5 would move it on just fine? The file path didn't seem to be the issue?)

Thanks to eelixduppy and cameraman for the tip on being prudent and keeping exit; That was the advice I expected, but it helps to be told again:))

At any rate, moved on and tried 'do{}while' as practically everyone suggested, and quickly saw the appeal. Keeps everything in the include, and does away with the cumbersome PHP 'wrapper' that I started with in the HTML. Special thanks to cameraman for the solution, as I'd have probably botched the while statement and been back on bended knee.

This adds exactly the protection that I was looking to achieve, and in much cleaner fashion.

Thanks too, to borntobeweb. I'll have to look at the solution a little more closely. It's interesting, but think maybe not quite right for this. Am only looking for protection against the possibility of a file not being found occasionally (as inevitably will happen). In this situation, if none of the files exist there is big trouble already, and I'll still wind up with a big blank hole in the page (though no error message). It works just fine though.

This is where I'm at, and feeling like it's good to go. Tried my best, but haven't been able to force any problems or make it break it yet. (Hysterically simple for most everybody here, but I've got something better than I started with, and even understand most of it:))

INCLUDE
<?php
$images = array(
array( 'file' => 'a',
'caption' => 'Caption/Description.'
),
array( 'file' => 'b',
'caption' => 'Caption/Description.'
),
array( 'file' => 'c',
'caption' => 'Caption/Description.'
)
);
do {
$i = rand(0, count($images)-1);
$selected_image = "images/{$images[$i]['file']}.jpg";
$caption = $images[$i]['caption'];
}
while
(!(file_exists($selected_image) && is_readable($selected_image)));

$image_size = getimagesize($selected_image);
?>

HTML
<?php @include('includes/random.inc.php');?>

<div id="image">
<img src="<?php echo $selected_image;?>" <?php echo $image_size[3];?> alt="<?php echo $caption;?>" title="" />
</div>
<div id="caption">
<?php echo $caption;?>
</div>