Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: random image

  1. #1
    Regular Coder googleit's Avatar
    Join Date
    Jul 2006
    Location
    birmingham, uk
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random image

    here is some code whic i use on my website for banners
    PHP Code:
    <?php

    /*
     * Name your images 1.jpg, 2.jpg etc.
     *
     * Add this line to your page where you want the images to 
     * appear: <?php include "randomimage.php"; ?>
     */ 

    // Change this to the total number of images in the folder
    $total "11";

    // Change to the type of files to use eg. .jpg or .gif
    $file_type ".jpg";

    // Change to the location of the folder containing the images
    $image_folder "images/random";

    // You do not need to edit below this line

    $start "1";

    $random mt_rand($start$total);

    $image_name $random $file_type;

    echo 
    "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";

    ?>

  • #2
    New Coder
    Join Date
    Dec 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    error in inserting url path in db.

    Quote Originally Posted by googleit View Post
    here is some code whic i use on my website for banners
    PHP Code:
    <?php

    /*
     * Name your images 1.jpg, 2.jpg etc.
     *
     * Add this line to your page where you want the images to 
     * appear: <?php include "randomimage.php"; ?>
     */ 

    // Change this to the total number of images in the folder
    $total "11";

    // Change to the type of files to use eg. .jpg or .gif
    $file_type ".jpg";

    // Change to the location of the folder containing the images
    $image_folder "images/random";

    // You do not need to edit below this line

    $start "1";

    $random mt_rand($start$total);

    $image_name $random $file_type;

    echo 
    "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";

    ?>
    Very helpful, but on my own programming I created some unknown error. Hope some one can help.

    here is my error I'm getting

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '1'' at line 1
    PHP Code:
    <meta name="generator" content="Namo WebEditor(Trial)">
    <?PHP
    // Get db connections
    include ("config.php");
    // Make a MySQL Connection

    $link2 mysql_connect$dbhost$dbuser$dbpass)or die("Could not connect: ".mysql_error());


    mysql_select_db($dbname) or die(mysql_error());



    $query "INSERT INTO Image (ImagePath)
    VALUES('$ImagePath')"
    ;
    mysql_query($query) or die(mysql_error());

    $ImagePath "";  // Clear variable so I know the image if from the db

        
    $result mysql_query("SELECT ImagePath FROM Image where $id = '1'") or die(mysql_error()); 

    // Make sure you actually get a result:
        
    if (mysql_num_rows($result) != 1){
        
    $error "Bad Login";


    echo 
    '<font color="black"><a href="imagedb.php" target="_self">ImagePath</font></a> not found, re-enter';


            } else {

    echo 
    "<img src=\"$ImagePath\" />"

    $ImagePath "";// Clear variable so I know  the old variable will not be reinserted in the db again.

    echo '<font color="black">Click here to insert a new <a href="imagedb.php" target="_self">ImagePath</font></a> in the db.';

    }


    mysql_close();


    ?>

  • #3
    New Coder
    Join Date
    Apr 2008
    Location
    Blagoevgrad, Bulgaria
    Posts
    47
    Thanks
    0
    Thanked 8 Times in 8 Posts
    Thank you, veru helpful.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Here is a much simpler way. Just upload images (accepted types can be defined within the curly braces) to the specified folder and it will pick one automatically, regardless of name or the number of images:
    PHP Code:
    $folder 'images/random';
    $images glob$folder '/*.{jpg,gif,png}'GLOB_BRACE );
    $image  $imagesarray_rand$images ) ];
    echo 
    '<img src=" . $folder . '/' . $image . " alt="' $image '" />'
    Last edited by kbluhm; 01-08-2009 at 04:56 AM.

  • #5
    Regular Coder
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    118
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Here is another method

    PHP Code:
    $banners = array('/img/banner.jpg''/img/banner2.jpg''/img/banner3.jpg');
    shuffle($banners);
    echo 
    ' <img src="'.$banners[0].'" width="123" height="123" alt="banner" /> '

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    oops... missing a few single quotes in there, as well as having included a bit of flawed logic, for all you copy 'n' pasters
    PHP Code:
    $folder 'images/random';
    $images glob$folder '/*.{jpg,gif,png}'GLOB_BRACE );
    $image  basename$imagesarray_rand$images ) ] );
    echo 
    '<img src="' $folder '/' $image '" alt="' $image '" />'

  • Users who have thanked kbluhm for this post:

    Jedi Knight (02-02-2009)

  • #7
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts
    Thanks kbluhm - unfortunately I don't see any 'Thank you' button, so you'll just get this

  • #8
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Thanks CyberPirate!

    Quote Originally Posted by CyberPirate View Post
    Here is another method

    PHP Code:
    $banners = array('/img/banner.jpg''/img/banner2.jpg''/img/banner3.jpg');
    shuffle($banners);
    echo 
    ' <img src="'.$banners[0].'" width="123" height="123" alt="banner" /> '
    thanks for this method! it help me alot


  •  

    Posting Permissions

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