Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-16-2012, 03:51 PM   PM User | #1
hackerzlab
New Coder

 
Join Date: Feb 2006
Posts: 91
Thanks: 19
Thanked 0 Times in 0 Posts
hackerzlab is an unknown quantity at this point
Non repeating random Image from a folder

The code below random shows image from a folder and it works just the way it is but then when i try to show more than 1 image, it repeats itself.

PHP Code:
<?php

$folder 
'images/';

$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';

$img null;

if (isset(
$_GET['img'])) {
    
$imageInfo pathinfo($_GET['img']);

    if (isset(
$extList[strtolower($imageInfo['extension'])]) && file_exists($folder.$imageInfo['basename']))
        
$img $folder.$imageInfo['basename'];
} else {
    
$fileList = array();
    
$handle opendir($folder);

    while (
false !== ($file readdir($handle))) {
        
$file_info pathinfo($file);
        if (isset( 
$extListstrtolower$file_info['extension'])])) {
            
$fileList[] = $file;
        }
    }
    
closedir($handle);

    if (
count($fileList) > 0) {
        
$imageNumber time() % count($fileList);
        
$img $folder.$fileList[$imageNumber];
    }
}

if (
$img!=null) {
    
$imageInfo pathinfo($img);
    
$contentType 'Content-type: '.$extList$imageInfo['extension'] ];
    
header($contentType);
    
readfile($img);
} else {
    if (
function_exists('imagecreate')) {
        
header ("Content-type: image/png");
        
$im = @imagecreate (100100) or die ("Cannot initialize new GD image stream");
        
$background_color imagecolorallocate ($im255255255);
        
$text_color imagecolorallocate ($im0,0,0);
        
imagestring ($im255,  "IMAGE ERROR"$text_color);
        
imagepng ($im);
        
imagedestroy($im);
    }
}

?>
The code to show the image:
PHP Code:
<img src="rotate.php"> and <img src="rotate.php"
I'm trying to show about 2/3 images on the same place but all the images are the same. Is there anyway to prevent it from happening and show different images?

thank you.
hackerzlab is offline   Reply With Quote
Old 02-16-2012, 04:44 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
How do you know which 3 images to show?
Can you describe how your images are stored, and how you pick 3 of them ... even though you might have 50 of them?
mlseim is offline   Reply With Quote
Old 02-16-2012, 04:54 PM   PM User | #3
hackerzlab
New Coder

 
Join Date: Feb 2006
Posts: 91
Thanks: 19
Thanked 0 Times in 0 Posts
hackerzlab is an unknown quantity at this point
the code works and it shows random image from a specified folder but when i try to show more than 1 image by using
PHP Code:
<img src="rotate.php"
multiple times, it still shows only one image multiple times.

i don't care what image it is as long as they are not repeated. thank you.
hackerzlab is offline   Reply With Quote
Old 02-16-2012, 05:39 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
So, it's based on the user ... ?

Somewhere, you have to keep track of which image(s) were already displayed.
Using SESSION or COOKIE ..

Do they need to see a unique image each time they visit, or with each page refresh?
And do they need to be displayed in order?
And what happens when the user comes back a different day, does the list start over again?

Or maybe the 3 images rotate automatically (using javascripting)?

It's really hard to answer your original post ... not being sure what the specifications are.
mlseim is offline   Reply With Quote
Old 02-17-2012, 11:13 AM   PM User | #5
hackerzlab
New Coder

 
Join Date: Feb 2006
Posts: 91
Thanks: 19
Thanked 0 Times in 0 Posts
hackerzlab is an unknown quantity at this point
Thank You for your interest.

i really don't care as long as the images are differently displayed. Currently all the images are the same.

for example:
PHP Code:
<img src="rotate.php">  <img src="rotate.php">  <img src="rotate.php"
shows image1.jpg three times.

I want
PHP Code:
<img src="rotate.php">  <img src="rotate.php">  <img src="rotate.php"
to show image1.jpg image2.jpg image3.jpg. It can be in any order as long as the images are different. It can be on refresh or unique as long as its different. I hope you got my point.
hackerzlab is offline   Reply With Quote
Old 02-17-2012, 01:04 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Are they all JPG type images?

And would it be OK to use scripting like this?

<?php $count=3; include("rotate.php");?>

So you would include the PHP script and tell it how many to print?

Would that be OK, or does it have to be an <img src ... > tag?
mlseim is offline   Reply With Quote
Old 02-17-2012, 04:44 PM   PM User | #7
hackerzlab
New Coder

 
Join Date: Feb 2006
Posts: 91
Thanks: 19
Thanked 0 Times in 0 Posts
hackerzlab is an unknown quantity at this point
it has to be in <img src .."> tag.

Thank You.
hackerzlab is offline   Reply With Quote
Old 02-17-2012, 07:14 PM   PM User | #8
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
doh!

I'm replying to this in two different threads?

I wonder if you're having a caching issue (which is why they are the same).
When you do a page refresh, do they change then?
mlseim is offline   Reply With Quote
Old 02-17-2012, 07:23 PM   PM User | #9
hackerzlab
New Coder

 
Join Date: Feb 2006
Posts: 91
Thanks: 19
Thanked 0 Times in 0 Posts
hackerzlab is an unknown quantity at this point
yeah. i thought if one of them could be sorted out, i'll use it since both the codes are for the same purpose.

when i refresh, they change. but all the images remain the same which is image1.jpg image1.jpg

i want it to be as image1.jpg image2.jpg image 3.jpg (they can be in any order as long as they are different)
hackerzlab is offline   Reply With Quote
Old 02-18-2012, 05:20 AM   PM User | #10
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
So it's a cache issue then.
Refreshing changes it, so you know it's working.

Try this ...

Duplicate the script 3 times ...
Call them "rotate1.php", "rotate2.php", "rotate3.php"
The same script, but different names ... so it might force it to not cache.

Now put the three of them in your page and see what happens.
mlseim is offline   Reply With Quote
Old 02-18-2012, 06:04 AM   PM User | #11
hackerzlab
New Coder

 
Join Date: Feb 2006
Posts: 91
Thanks: 19
Thanked 0 Times in 0 Posts
hackerzlab is an unknown quantity at this point
no its not a cache issue and of course, i posted a working code! i just needed help modifying it.

thanx for your time. never mind.
hackerzlab is offline   Reply With Quote
Old 02-18-2012, 05:33 PM   PM User | #12
Chris Hick
Regular Coder

 
Join Date: Oct 2010
Location: Florence, MS
Posts: 476
Thanks: 10
Thanked 33 Times in 32 Posts
Chris Hick is an unknown quantity at this point
Is the reason kind of obvious? All three images are going to be the same, because of the GET. If the GET isn't set it sets it according to the else which in this case you might be doing but it still sets all three images the same in the img tag because the page is calling the script at the same exact time.
__________________
Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
I always recommend the HEAD First series of books for learning a new coding language. ^_^
Chris Hick is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:46 PM.


Advertisement
Log in to turn off these ads.