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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    112
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Show Random Images PHP Code

    Hi folks!

    I'm a complete newb with PHP and was hoping to get some help.
    I'd like to write a script to insert into Wordpress that displays three random images from a set of many (50+) I have chosen.
    I then want each image to link to a different post.

    I found
    Code:
    <img src="http://path_to_images/headerimage_<?php echo(rand(1,3)); ?>.jpg"
    width="image_width" height="image_height" alt="image_alt_text" />
    online and it looks like it would display random images. The issue is that (a) it doesn't display three images (with no repeats) and (b) it doesn't allow me to link the images to posts.

    Would anyone here please help me? Thanks so much

    Ilan

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    How are the images stored? Are they all in one folder? If so you can read the directory to get the file names, skipping ones that don't end with an image extension (such as jpg, png, gif) if you have different extensions/files.

    If you have a list of some sort then read the list and randomly select from the list. You need a little more info if you expect code to be given for you, but the functions you would need to look at, pretty much regardless, are array_rand(); and unset(); (to remove it and avoid duplicates). It all depends on the setup. though, these are just some I can think of to kind of help.

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    112
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Hi Dubz,

    Thanks for replying.

    I can store the images whichever way is more convenient. I have not uploaded them to the server yet. I can put them in a post, in a folder on the website, etc.
    I can also name them however. The link part (a different link for each image) is important, so I would have to add that in somehow.

    I will set it up whatever way is easier to code!

    Thanks so much

    Ilan
    Last edited by Ilan; 05-17-2014 at 04:26 AM.

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Well the links would have to be different otherwise you'd get the same image or all of them.

    Put the images into one folder and read the docs for readdir() here to learn how to loop through the images. You'll want to put the files into an array and then use array_rand() to select as many as you want (at random), then loop through the results to generate the images. I was wrong about needing unset() by the way.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Below is to display, but will the links need to be assigned differently for each image?




    <?php

    // folder name where they are stored.
    $folder_name="photos";

    // put these types of images into an array.
    $images = array_merge(
    glob("$folder_name/*.jpg"),
    glob("$folder_name/*.png"),
    glob("$folder_name/*.gif")
    );

    // Randomize the array.
    shuffle($images);

    // Display the first 5 random images.
    echo"
    <img src='$images[0]' /><br />
    <img src='$images[1]' /><br />
    <img src='$images[2]' /><br />
    <img src='$images[3]' /><br />
    <img src='$images[4]' /><br />
    ";

    // If you would rather display all of them.
    //foreach($images as $img){
    //echo "<img src='$img' /><br />";
    //}

    ?>
    Last edited by mlseim; 05-17-2014 at 05:37 AM.

  • #6
    Regular Coder
    Join Date
    Aug 2012
    Posts
    112
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks so much both of you!

    I guess I wasn't clear though.

    Let's say I have five images: A, B, C, D, E.
    A is connected to post 1, B to post 2, C to post 3, etc.

    I would like to display three images (e.g. B, C, and E) and have them link to their respective posts.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    If you want to connect images and links then you'll need some way to store these links, in a file or in a database. If you do a database it would make selecting it randomly a whole lot easier, because you can choose a select amount by random and a whole lot more things, just with one query.

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Is this a constant dynamic thing that changes all the time, or a one-time deal?

    What if the post ID was part of the image filename? Perhaps the link could be parsed from the filename. I guess you would have to create the post before you upload the image, so you know the post ID.

    There's gotta be a plugin for this that already exists?

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by mlseim View Post
    What if the post ID was part of the image filename?
    That would only take one table away and would make it less dynamic. Then you run into the problem of two pictures being uploaded simultaneously and overwriting each other, because it's not as fast as a SQL insert, which auto increments an index if set to. Not to mention images following a pattern could be exposed to spam easier, whether its saving them or voting. Random names isn't as easy to do that with.


  •  

    Tags for this Thread

    Posting Permissions

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