Forum Moderators: coopster
<?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 }?>
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'.
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)
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);
}
?>
Is the \n necessary?
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);
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.
Also, the exit is added there to absolutely make sure that no code gets executed below that line.
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).
$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.
hehe, yes it does. For some reason I misinterpreted thinking that the Location header was being changed to a file that doesn't exist.
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>