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

  • SEARCH
  • Home
  • 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 462619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:05:34+00:00 2026-05-12T23:05:34+00:00

I have some code that works, but is clunky and am wondering if there

  • 0

I have some code that works, but is clunky and am wondering if there is a better way to do it, first the code, and then i will explain:

// repeat query 10 times
for ($i = 1; $i <= 10; $i++) {
   $query = @mysql_query("SELECT fileName FROM images");
    while($row = mysql_fetch_assoc($query)){
        extract($row);
        echo "<img src='images/images/$fileName'/>";
    }
}

I would like to repeat a set of images whose file names are stored in a mySql table. The code above repeats the query 10 times. I would rather query once, then repeat the list. Is there a way to do this, either on the mySql side or in the php loop?

Thanks.

  • 1 1 Answer
  • 3 Views
  • 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-12T23:05:34+00:00Added an answer on May 12, 2026 at 11:05 pm

    Cache all the rows from the query in an array. Loop through the cache array 10 times and print.

    $a= array();
    $query= mysql_query("SELECT fileName FROM images");
    while($row = mysql_fetch_assoc($query))
     $a[]= $row;
    
    for ($i = 1; $i <= 10; $i++) {
     foreach ($a as $row)
       echo "<img src='images/images/{$row['fileName']}'/>";
    }
    

    Of course you’ll need to sanitize the output of $row['fileName']

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

Sidebar

Related Questions

I have some code that works fine in FF but not in IE. I
I have some login code that works well for Firefox and other browsers, but
i have some javascript roll over code that works fine in firefox but when
I have some very basic semaphore code that works great on Linux, but cannot
I have some code that works like the advised use of TransactionScope, but has
so I have some code that works fine with small text files but crashes
I have some code that works like the below. I was wondering if its
I have some file writing code that works as expected, but prints an error
I have written some code that works pretty well, but I have a strange
I have some code that works fine but it's become too slow: HTML: I

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.