Closed Thread
Results 1 to 6 of 6

Thread: Randomness... on PHP

  1. #1
    Member Kitsune Devil is an unknown quantity at this point
    Join Date
    Nov 2003
    Location
    New York, NY
    Posts
    58

    Randomness... on PHP

    Hello,
    Suppose I want a PHP code which allows me to show a random image (with a link that corresponds with the image) on my page. I have the images stored in a folder... now what do I do? I'm quite new at this...
    ~Kitsune
    Will take requests for small buttons/banners
    >> ___mirage [ x ] rapture

  2. #2
    anti-liberal keith is an unknown quantity at this point keith's Avatar
    Join Date
    Oct 2000
    Location
    Buttsville
    Posts
    2,375
    first thing's first... you'll need some type of database that stores the image names with a corresponding link.
    w3rd

  3. #3
    Senior Member bloodyveins is an unknown quantity at this point bloodyveins's Avatar
    Join Date
    Mar 2003
    Location
    squid.conf
    Posts
    230
    the solution could be either listing all files in that directory (using php functions: opendir, readdir, is_file) and then generating seed to choose random image or listing all files manually in a flat file and then choosing a random image from it.

    i think, there is no need of database. this process can be done automatically.

  4. #4
    Member Kitsune Devil is an unknown quantity at this point
    Join Date
    Nov 2003
    Location
    New York, NY
    Posts
    58
    Ah, I see. Thanks!!
    Will take requests for small buttons/banners
    >> ___mirage [ x ] rapture

  5. #5
    anti-liberal keith is an unknown quantity at this point keith's Avatar
    Join Date
    Oct 2000
    Location
    Buttsville
    Posts
    2,375
    Originally posted by bloodyveins
    the solution could be either listing all files in that directory (using php functions: opendir, readdir, is_file) and then generating seed to choose random image or listing all files manually in a flat file and then choosing a random image from it.

    i think, there is no need of database. this process can be done automatically.
    well, that's what i was getting at. as far as i'm concerned, even a single file storing info is a type of database.

    you could even store them in the actual php file, which is about as rudementary of a database as you could get:
    PHP Code:
    <?php

    $rand 
    rand(1,5);

    switch (
    $rand) {
    case 
    1:
      echo 
    "<a href=\"http://www.domain.com\"><img src=\"domain.jpg\"></a>";
      break;
    case 
    2:
      echo 
    "<a href=\"http://www.something.com\"><img src=\"something.jpg\"></a>";
      break;
    case 
    3:
      echo 
    "<a href=\"http://www.another.com\"><img src=\"another.jpg\"></a>";
      break;
    case 
    4:
      echo 
    "<a href=\"http://whatever.com\"><img src=\"whatever.jpg\"></a>";
      break;
    case 
    5:
      echo 
    "<a href=\"http://slappystick.com\"><img src=\"slappystick.jpg\"></a>";
      break;
    }

    ?>
    w3rd

  6. #6
    Senior Member bloodyveins is an unknown quantity at this point bloodyveins's Avatar
    Join Date
    Mar 2003
    Location
    squid.conf
    Posts
    230
    err... i think i misdelivered my idea keith.
    what i meant by listing all files manually in a flat file and then choosing a random image from it. should be listing all images files in a flat file through such mechanism and then choosing a random image from it.
    The function of flat file here is as "cache". The existence of flat file will reduce time wasting while reading directory and generating seed. This process could be called automatic, since you don't write all filenames by yourself. You may also involve cron job to update the image file list in the file.

    Here is an example code of how it works:

    PHP Code:

    //Set up variables
    $imgDir "myImage/"//relative to current file
    $imgListFile "imglist.txt";

    function 
    fileExtension ($givenFile) {
        
    $arrayFile explode(".",$givenFile);
        
    $ext count($arrayFile) - 1;
        return 
    $arrayFile[$ext];
    }

    function 
    file_list($dir) {
        global 
    $imgListFile;
        if (
    is_dir($dir)) {
            
    $fd = @opendir($dir);
            while ((
    $part = @readdir($fd)) == true) {            
                if (
    $part != "." && $part != "..") {
                    if(
    is_file($dir.$part)) {
                        if (
    fileExtension($part) == "jpg" || fileExtension($part) == "jpeg" || fileExtension($part) == "gif" || fileExtension($part) == "png") {
                            
    $file_array[] = $part;
                        }
                    }
                }
                
    clearstatcache();
            }
            if (
    $fd == true) {
                
    closedir($fd);
            }
            if (
    is_array($file_array)) {
                
    natsort($file_array);
                
    $fp fopen($imgListFile,'wb');
                if(
    is_writable($imgListFile) && flock($fp,LOCK_EX)) {
                    for(
    $i=0;$i<count($file_array); $i++)
                        
    $content .= $file_array[$i].";";
                    
    $content preg_replace("/;$/","\n",$content);
                    
    fwrite($fp,$content);
                    
    flock($fp,LOCK_UN);
                    
    fclose($fp);
                }    
                return 
    $file_array;
            } 
            else {
                return 
    false;
            }
        }
        else {
            return 
    false;
        }
    }

    function 
    imgFromFile ($pathToFile) {
        
    $fp fopen($pathToFile,'rb');
        if(!
    $fp
            return 
    0;
        
    $fileImg fread($fp,filesize($pathToFile));
        
    fclose($fp);
        
    $file_array explode(";",$fileImg);
        return 
    $file_array;
    }

    function 
    chooseImg($method 1) {
        if(
    $method == 1) { //use file_list
            
    global $imgDir;
            
    $fileList file_list($imgDir);

        }
        else { 
    //use imgFromFile
            
    global $imgListFile;
            
    $fileList imgFromFile($imgListFile);
        } 
        
    $totElement count($fileList) - 1;
        
    $chosen rand(1,$totElement);
        return 
    $fileList[$chosen];
    }

    //Usage:

    $img chooseImg(1);
    print 
    "Image chosen from <b>listing image file</b>: ".$img;

    print 
    "<br />";

    $img chooseImg(2);
    print 
    "Image chosen from <b>$imgListFile</b>: ".$img

    This code does run. When I made it, I'd tested it with dummy data. This is a special one for you!! Wish this help..

Closed Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts