Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 895675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:35:50+00:00 2026-05-15T14:35:50+00:00

My name is Shruti.I’m new to php. I have a program which displays images

  • 0

My name is Shruti.I’m new to php. I have a program which displays images randomly, there are about 200 images in my program.I want to display the random images with out repetition, can any one please help with this. here is my code.

Appreciate your help

Thank you.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:35:51+00:00Added an answer on May 15, 2026 at 2:35 pm

    I don’t know whats about the $img_id but you could consider to use shuffle().

    shuffle($file_array);
    

    But then you loose the connection to $img_id. You could (I am not sure if this is the best solution), build your array this way:

    $file_array = array(
            array("images/bag200.bmp", 1),
            array("images/bag178.bmp", 1),
            array("images/bag004.bmp", 0,
            ...
    );
    

    In the long run, it is probably better to store all the image paths in a CSV file or even a database. Believe me, you don’t want to maintain an array with > 200 entries manually 😉

    You can loop over images this way (they are in random order now):

    <?php foreach($file_array as $image): ?>
        <img src="<?php echo $image ?>" />
    <?php endforeach; ?>
    

    If you only want to display a subset of the images, randomly chosen, you can do this:

    $n = 20; // want to display 20 images
    $rand = array_rand(range(0,200), $n); // draw keys randomly
    
    shuffle($rand); //shuffle keys
    
    foreach($rand as $r) {
        echo '<img src="' . $file_array[$r] . '" />';
    }
    

    Some comments on your code:

    $ran = array_rand($file_array);
    

    $ran contains a key randomly chosen from the images.

    for ($i=0;$i<200;$i++) {
        //while (in_array($tst,$rand_array)){
            $tst = $file_array[$ran];
            $id = $img_id[$ran];//}
        $rand_array[] = $tst;
        $rand_id[] = $id;
    }
    

    You always pick the same entry from $file_array and $image_id because you never change $ran. That is way you get the same image 200 times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Name: <%= Html.TextBox(txtName, 20, new { @class = hello }) %> I want that
my name is aderson and in this moment i have something question about composition
My name is NipinVarma,I would want to know about some errors in my iphone
Name is not compliant? Layout? What? Do you have ideas what exactly could generate
[ { name: John Doe, location: { name: New York, New York, id: 12746342329
name says it all, but I want to be able to position an element
My name is Alex and I'm new to VB. I'm sorry but I am
file name myServices.php <?php $gender = 'MALE'; ?> in another file lets say file.php
Please name RoR authorization plugins with Rails 3 support, which you think is the
The name is pretty confusing probably. I have a requirement where a URL must

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.