This php scriprt will (as many other scripts ) pick an image from a parsed config file.
In my "index.php" multiple images should show up (randomly picked).
That all works perfect...but I do not want an image to show up twice (or even more) at the same time.

How do I accomplish that?...I have no idea howto...

Thanks

Dick

Here are the files:

rotator.php

function showImage() {

$ini_file = 'images.ini';
# read the config file into an array or die trying
$images = @parse_ini_file($ini_file,true);
if (! $images) {
  die('cannot read ini file');
}



$img = array_rand($images);
# get the selected image's css id if one exists
$id = $images[$img]['id'] ?
  sprintf( ' id="%s" ', $images[$img]['id'] ) :
  '';
# get selected image's dimensions
$size = @getimagesize( $images[$img]['src'] );
# if an url was specified, output the opening A HREF tag
if ( $images[$img]['url'] ) {
  printf(
    '<a href="%s" title="%s" target="blank">',
    $images[$img]['url'],
    $images[$img]['title']
  );
}
# output the IMG tag
printf(
  '<img src="%s" alt="%s" %s %s%s/ border="0">',
  $images[$img]['src'],
  $images[$img]['alt'],
  $size[3],
  $id,
  $class
);
# if an url was specified, output the closing A HREF tag
if ( $images[$img]['url'] ) {
  echo('</a>');
}	
  }

images.ini

[klaassen] 
src   = img/klaassen.jpg 
alt   = Jan Klaassen 
url   = http://google.com/ 
title = klaassen website 

[Pietersen] 
src   = img/pietersen.jpg 
alt   = Jan Pietersen 
url   = http://msn.com/ 
title = Pietersen website 

[Jansen] 
src   = img/jansen.jpg 
alt   = Jan Jansen 
url   = http://php.com/ 
title = Jansen website 

[Mulder] 
src   = img/mulder.jpg 
alt   = Jan Mulder 
url   = http://www.hotscripts.com 
title = Mulder website 

[Dekker] 
src   = img/dekker.jpg 
alt   = Jan Dekker 
url   = http://www.voetbal.nl 
title = Dekker website 

[Gerritsen] 
src   = img/gerritsen.jpg 
alt   = Jan Gerritsen 
url   = http://www.ilse.nl 
title = Gerritsen website 

[Van Dijk] 
src   = img/dijk.jpg 
alt   = Jan van Dijk 
url   = http://www.maastricht.nl 
title = Van Dijk website 

index.php

<?php include('rotator.php'); ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<body> 

<p>refresh to see new images.</p> 

<?php showImage(); ?> 

<?php showImage(); ?> 


</body> 
<html>

    There is a thread you should look at [thread=10329337]Here[/thread].

    It is a function which stores the random number in an array and checks that it does not repeat it.

    I would recommend instead of typing showImage(); twice in your php code, have it accept a parameter, i.e.

    
    function showImage($numImages=1) {
    
    

    Instead of loading the array with random numbers, load the array with the info from the existing image.

    I'd write a quick one up for you but I must fly for a meeting.

    [edit] with the numImages=1 have something like (for $i=1, $i <= $numImages, $i++), etc etc to encapsulate the random image generation part. If you keep it all encased (much like the random number generator) you can see which images have been displayed, verses running the code multipul times, which it wouldn't necesarraly know. [/edit]

      Thanks a lot for your reply big.nerd 🙂
      I understand the idea of that function, but I really don't have a clue how to implement. (way to much noob :eek: )
      Could you explain this more, if you have the time?

      Thanks very much,

      Dick

        da_rithm,

        Here is the code (working):

        <?php
        function showImage($numpics = 2) { 
        
        $ini_file = 'images.ini'; 
        # read the config file into an array or die trying 
        $images = @parse_ini_file($ini_file,true); 
        if (! $images) { 
          die('cannot read ini file'); 
        } 
        
        $usedpics = array(); 
        for ($i = 0; $i < $numpics; $i++) {
        	$img = array_rand($images);
        	while(in_array($img,$usedpics)) {
        		$img = array_rand($images);
        	}
        	$usedpics[$i] = $img;
        
        	# get the selected image's css id if one exists 
        	$id = $images[$img]['id'] ? 
        	  sprintf( ' id="%s" ', $images[$img]['id'] ) : 
        	  ''; 
        	# get selected image's dimensions 
        	$size = @getimagesize( $images[$img]['src'] ); 
        	# if an url was specified, output the opening A HREF tag 
        	if ( $images[$img]['url'] ) { 
        	  printf( 
        		'<a href="%s" title="%s" target="blank">', 
        		$images[$img]['url'], 
        		$images[$img]['title'] 
        	  ); 
        	} 
        	# output the IMG tag 
        	printf( 
        	  '<img src="%s" alt="%s" %s %s%s/ border="0">', 
        	  $images[$img]['src'], 
        	  $images[$img]['alt'], 
        	  $size[3], 
        	  $id, 
        	  $class 
        	); 
        	# if an url was specified, output the closing A HREF tag 
        	if ( $images[$img]['url'] ) { 
        	  echo('</a>'); 
        	}
        }     
         } 
        
         showImage(5); // Show 5 Pictures
        
        ?>
        

        This was taken from the example thread I showed you; essentially this:

        $usedpics = array();  // Initialize an array to hold all of the pictures that have already been displayed.
            for ($i = 0; $i < $numpics; $i++) { // The numpics is the number of pics you wish to display (without repeating)
        		$img = array_rand($images); // Your own code
        		while(in_array($img,$usedpics)) { // Continue to go through the random array process until one is not found (a new img
        			$img = array_rand($images);
        		}
        		$usedpics[$i] = $img; // Save the "new" img so its not repeated
        
        // The rest of it is the existing from your code, with the exception of the added } to close off the for statement.
        

        Take care..

          Write a Reply...