0

I have a folder full of images (200), I want a php script to select 1 image every 24 hours and display it on the page (using true randomness, example not display the same image more than once until all images have been displayed)

I would like to use a mysql db for this script to store the id, image path, and the image link (example: id: 1, us.png, http://www.website.com/us.html)

Note: The link will be dynamic in nature, such as http://www.website.com/cms/united-states/

Also note: I need help with the entire script, as in the creation of the db, and the script itself as I am just now starting out...I learn best visually, I need to see the script function before I can fully grasp the concept. I appreciate all who contribute to helping learn php+mysql!

Thank you in advance! Brian

1

1 Answer 1

0

here is code

$folder = "images";
    $results_img_arr = array();
    if (is_dir($folder))
    {
            if ($handle = opendir($folder))
            {
                    while(($file = readdir($handle)) !== FALSE)
                    {
                        if(!in_array($file,array(".","..")))
                            $results_img_arr[] = $folder."/".$file;
                   }
             closedir($handle);
            }
    }
    $ran_img_key  = array_rand($results_img_arr);
    $dbconn = mysqli_connect("127.0.0.1", "root", "", "images");
    $img_path = $results_img_arr[$ran_img_key];
    $select_img_exist = mysqli_query($dbconn,"SELECT imagespath FROM imgfiles WHERE  imagespath = '$img_path'");
    $img_rowcount=mysqli_num_rows($select_img_exist);
        if($img_rowcount == 0 )
        {
         $insert_img = "INSERT INTO imgfiles (imagespath,views) VALUES ('$img_path',1)";
        mysqli_query($dbconn, $insert_img);
        echo "<img src='".$img_path."'>";
        }
        else{
            echo "all images are displayed";
        }
Sign up to request clarification or add additional context in comments.

4 Comments

Hello, I forgot the most important part of the code that I was initially looking for which is to display random at midnight each day, I have a piece of code from another script I used to use, but I don't know how to incorporate it in this code: $today=getdate(); srand($today['mday']+$today['month']+$today['year']); $r=rand(0,$i-1); echo '<a href="' . $links[$imgs[$r]] . '">' . "<img src=\"flags/{$imgs[$r]}\" alt=\"Photo\" /></a>"; } That basically would display an image at 12 midnight everyday. Can you plug that into your response code to make it work?
Also, I believe because I am using php 7.028 your script gives me this error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /var/www/html/rand.php on line 21 I have resolved it by putting a @ like this: $img_rowcount=@mysqli_num_rows($select_img_exist); Is that ok, or should something else be changed? I get no errors after adding the @ however... Thank you again! Brian
...And how I can put a different link to each image being displayed? I will create the row in my database table for the link? Also, I believe because I am using php 7.028 your script gives me this error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /var/www/html/rand.php on line 21 I have resolved it by putting a @ like this: $img_rowcount=@mysqli_num_rows($select_img_exist); Is that ok, or should something else be changed? I get no errors after adding the @ however... Thank you again! Brian
Also, I notice that the db part is not having an effect on the script, your code only seems to be working from the folder and the db. I want to pull the image url from the db table and display it, currently it not working that way. If I comment out the db query, the image sitll display's when I comment out the SQL Query: // $select_img_exist = mysqli_query($dbconn,"SELECT image FROM test WHERE imagespath = '$img_path'"); The image still displays, it should NOT display if code is working, but it's not. Can you fix?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.