random images from database help

Discussion in 'PHP' started by xceedbd, Jul 23, 2009.

  1. #1
    this code random images from folder:

    <?php
    function getRandomFromArray($ar) {
    mt_srand( (double)microtime() * 1000000 );
    $num = array_rand($ar);
    return $ar[$num];
    }

    function getImagesFromDir($path) {
    $images = array();
    if ( $img_dir = @opendir($path) ) {
    while ( false !== ($img_file = readdir($img_dir)) ) {
    // checks for gif, jpg, png
    if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
    $images[] = $img_file;
    }
    }
    closedir($img_dir);
    }
    return $images;
    }

    $root = '';
    // If images not in sub directory of current directory specify root
    //$root = $_SERVER['DOCUMENT_ROOT'];

    $path = 'images/';

    // Obtain list of images from directory
    $imgList = getImagesFromDir($root . $path);

    $img = getRandomFromArray($imgList);

    ?>


    Place the following where you wish the random image to appear:
    <img src="<?php echo $path . $img ?>" alt="" />

    Please help me change it, comes from database query
     
    xceedbd, Jul 23, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    If the image paths or even data is stored in a table, you can run a really simple:

    SELECT image_path FROM image_table ORDER BY RAND() LIMIT 1;
     
    jestep, Jul 23, 2009 IP
  3. xceedbd

    xceedbd Well-Known Member

    Messages:
    640
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #3
    sorry i want for Rotation or slideshow
     
    xceedbd, Jul 23, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Can you explain how the database is setup in relation to the images?
     
    jestep, Jul 23, 2009 IP
  5. xceedbd

    xceedbd Well-Known Member

    Messages:
    640
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #5

    I saved image to database by data type "mediumblob" . not saved web server.

    now i want slideshow of image.
     
    xceedbd, Jul 23, 2009 IP
  6. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You will need to provide the full table schema for anyone to help you.
     
    Chemo, Jul 23, 2009 IP
  7. xceedbd

    xceedbd Well-Known Member

    Messages:
    640
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #7
    Still i do not solve it, anyone plz help me.
     
    xceedbd, Jul 24, 2009 IP