Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
no duplicate image in random script problem
Old 04-13-2010, 04:49 PM no duplicate image in random script problem
Novice Talker

Posts: 14
Trades: 0
Hi!

I have a randomscript which both makes a random image from a folder and creates a random class. Sometimes it duplicates the images and my question is:

How do I make the images to just be displayed once?

the script is:
PHP Code:
<?php
/* The default folder with images */
//$settings['img_folder'] = 'plugins/project_menu/images/thumbs/';

/* File types (extensions) to display */
$settings['img_ext'] = array('.jpg','.gif','.png');

/*
   How to display the images?
   0 = print just the image path (for includes), like: images/test.jpg
   1 = redirect to the image, when using: <img src="randim.php" />
*/
$settings['display_type'] = 0;

/* Allow on-the-fly settings override? 0 = NO, 1 = YES */
$settings['allow_otf'] = 1;


/*******************************************************************************
*  DO NOT EDIT BELOW...
*
*  ...or at least make a backup before you do!
*******************************************************************************/

/* Override type? */
if ($settings['allow_otf'] && isset($_GET['type']))
{
    
$type intval($_GET['type']);
}
else
{
    
$type $settings['display_type'];
}

/* Override images folder? */
if ($settings['allow_otf'] && isset($_GET['folder']))
{
    
$folder htmlspecialchars(trim($_GET['folder']));
    if (!
is_dir($folder))
    {
        
$folder $settings['img_folder'];
    }
}
else
{
    
$folder $settings['img_folder'];
}

/* Make sure images fodler ends with an '/' */
if (substr($folder,-1) != '/')
{
    
$folder.='/';
}

/* Get a list of all the image files */
$flist = array();
foreach(
$settings['img_ext'] as $ext)
{
    
$tmp glob($folder.'*'.$ext);
    if (
is_array($tmp))
    {
        
$flist array_merge($flist,$tmp);
    }
}

/* If we have any images choose a random one, otherwise select the "noimg.gif" image */
if (count($flist))
{
    
$src $flist[array_rand($flist)];
}
else
{
    
$src 'noimg.gif';
}

      
/* Output the image according to the selected type */
if ($type)
{
    
header('Location:'.$src);
    exit();
}
else
{
//    echo $src;
}
?>
and for calling the images (the long scripts is because I got 25 pictures that is displayed):
PHP Code:
<body>

<?php
// This is for making a random class
class UniqueRand{
  var 
$alreadyExists = array();

  function 
uRand($min NULL$max NULL){
    
$break='false';
    while(
$break=='false'){
      
$rand=mt_rand($min,$max);

      if(
array_search($rand,$this->alreadyExists)===false){
        
$this->alreadyExists[]=$rand;
        
$break='stop';
      }else{
  
//      echo " $rand already!  ";
    //    print_r($this->alreadyExists);
      
}
    }
    return 
$rand;
  }
}
$rand=new UniqueRand(); 


?>
<?php
/* this is for making a random image from the folder 
plugins/project_menu/images/thumbs/ hyperlinked to 
plugins/project_menu/images/pictures/ */

$_GET['type']=0

$_GET['folder']='plugins/project_menu/images/pictures/';


// new random image1;
include 'randim.php';
//now you have large image in $src;
// replace folder name assuming filename is the same
$small str_replace('plugins/project_menu/images/pictures/','plugins/project_menu/images/thumbs/',$src);
 
?>
<ol>
<li><div class="scrollbox"> 
<div class="<?="randompacering"$rand->uRand(1,25).""?>"><a class="fancy" rel="fancy" href="<?=$src;?>"><img border='0' height='40px' src="<?=$small;?>"></a></div></p>
</li>

<?php
// new random image2;
include 'randim.php';
$small str_replace('plugins/project_menu/images/pictures/','plugins/project_menu/images/thumbs/',$src);
 
?>
<li>
<div class="<?="randompacering"$rand->uRand(1,25).""?>"><a class="fancy" rel="fancy" href="<?=$src;?>"><img border='0' height='40px' src="<?=$small;?>"></a></div></p>
</li>

</ol>
</body>
</html>
any suggestions?

thanks a lot!

Perik
perik is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-14-2010, 06:38 AM Re: no duplicate image in random script problem
Andy Pugh's Avatar
Extreme Talker

Posts: 203
Name: Andy
Location: N.Ireland
Trades: 0
You could set a session, basically when an image is loaded, insert it into a session, then check before you output the next image if it's already been called.
__________________

Please login or register to view this content. Registration is FREE
Andy Pugh is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to no duplicate image in random script problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.10329 seconds with 12 queries