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 8303451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:32:48+00:00 2026-06-08T17:32:48+00:00

i am new to Javascript and i have created the code below it works

  • 0

i am new to Javascript and i have created the code below it works fine no problem at all however i want to know what is i want to pull the image dynamically using php and javascript from mysql database how can i refactor my code bellow. thanks in advance for your contribution.

var myimage = document.getelementById("mainImage");
var imageArray =["images/overlook.jpg","images/garden.jpg","images/park.jpg"];

var imageIndex =0;

function changeimage(){
      myimage.setAttribute("src",imageArray[imageIndex]);
      imageIndex++;
      if(imageIndex >= imageArray.length){
      imageIndex = 0;
}

setInterval(changeimage, 5000);
  • 1 1 Answer
  • 0 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-06-08T17:32:49+00:00Added an answer on June 8, 2026 at 5:32 pm

    One of several options.

    1. Query the database for the column with the URL of the images.

      $query = mysql_query("SELECT url FROM images");
      
    2. Then something like this to get an array out of it:

      $images = array();
      while($row = mysql_fetch_array($query)){
          $images[] = $row['url'];
      }
      
    3. Then generate this string (that you use in the Javascript provided):

      var imageArray = ["images/overlook.jpg","images/garden.jpg","images/park.
      

      using the array you retrieved from the database. You could use json_encode in PHP for this if you don’t want to mess around with error prone string building.

      $imagesAsJsonArray = json_encode($images);  
      
    4. Echo it. Done.

    Not the most elegant of solutions. But it gives you something to play with. Check out a few PHP tutorials online and you’ll soon get the hang of it.

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

Sidebar

Related Questions

I have created an object literal using the code below. Everything works fine. However,
I have some code that dynamically creates a new button through JavaScript that when
I'm kinda new to javascript running on Apache but I have the following problem.
I'm testing a new TeeChart HTML5/javascript and I have a problem with zoom. When
I am new to Javascript and am having some difficulty. I want to have
I'm kind of new to Javascript. Any help or suggestion regarding below problem is
I have the below code to create a class in javascript.... Now i will
I'm pretty new to javascript and jquery, and have run into a problem that
I'm using the prototype javascript libraries. My code is below. I've created a class
I'm quite new to javascript but have undertaken a task to get better aquainted

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.