Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2002
    Location
    Virginia
    Posts
    787

    php random image from database

    hello, I want to call a random image on a daily interval to display on my home page from a specific image database on the same website, i figured out how to call if from a images.txt file but not the existing database

    any ideas, help?


    thank you
    UrlRedo.com - short URL service

  2. #2
    Join Date
    Jun 2005
    Location
    Columbus, Ohio
    Posts
    171
    Just create a separate entry for every image, and, using a cron, have a new image every day be marked with a "1" for use. Then just call the image with a "1" for that column.

  3. #3
    Join Date
    Sep 2004
    Location
    Vancouver, BC Canada
    Posts
    122
    Do you use a unique identifier in the database?

    I would pull amount of items from the database with something similar to:
    Code:
    <?php
    
    $strqry = "SELECT COUNT(*) FROM myImages";
    
    $query = mysql_query($strqry) or die("MySQL Error: <br /> {$strqry}<br />". mysql_error());
    
    $count = mysql_fetch_array($strqry);
    
    $count = $count['COUNT(*)'];
    
    $random = rand(1, $count); 
    
    // Get row based on random Id pulled from database
    $strqry = "SELECT * FROM myImages WHERE id = '{$random}'";
    
    $query = mysql_query($strqry) or die("MySQL Error: <br /> {$strqry} <br /> ". mysql_error());
    
    $image = mysql_fetch_array($query, MYSQL_ASSOC);
    
    // Display image based on filename from database
    echo "<img src="/your/path/here/to/image/{$image['filename']}\" />\";
    
    ?>
    Of course your database scheme might be different but it should give you somewhat of an idea of how to pull a random image from the database.
    SharkBait
    Web Dev: php, MySQL, HTML, XHTML, JS, CSS
    www.DyanmicShark.com - Coming Soon!
    My Blog:www.tyleringram.com

  4. #4
    Join Date
    Oct 2002
    Location
    Virginia
    Posts
    787
    thanks for the replies, i found what i was looking for (i think) at one of the invision modding forums since its pulling from a forum database

    Code:
    $result = mysql_query("SELECT directory, masked_file_name FROM XXXX_XXXX_images WHERE masked_file_name LIKE '%.jpg' OR masked_file_name LIKE '%.gif' ORDER BY RAND() LIMIT 1",$db);
    if ($myrow = mysql_fetch_array($result)) {
      do {
    but now i need to figure how to make it pull ONE random picture per day and not each time the page refreshes, i actually got some max_connection errors as users are hitting refresh over and over to see the next pic

    any ideas?
    UrlRedo.com - short URL service

Posting Permissions

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