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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:02:42+00:00 2026-05-23T22:02:42+00:00

I am working on an e-commerce website. There is an array products in javascript

  • 0

I am working on an e-commerce website. There is an array “products” in javascript defined by the following code

<script type="text/javascript">
    var products = 
    [
         {"id":"1","title":"Apple iPhone 4"},
         {"id":"2","title":"BlackBerry 9780 Bold"}
    ];
    /*some other javascript code*/
</script>

I want this array to be updated dynamically according to the number of rows returned by querying the database.
For example, suppose I query the database and 5 rows are returned. I want this array to be updated with those 5 rows. Please help me getting this done. I am using PHP, MySQL, Apache on Windows machine.

  • 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-05-23T22:02:43+00:00Added an answer on May 23, 2026 at 10:02 pm

    You want the push function: http://www.w3schools.com/jsref/jsref_push.asp

    var products = 
    [
         {"id":"1","title":"Apple iPhone 4"},
         {"id":"2","title":"BlackBerry 9780 Bold"}
    ];
    
    // Let's add a new phone:
    products.push({"id":"3","title":"HTC Evo"});
    
    /*
        products now equals: 
        [
             {"id":"1","title":"Apple iPhone 4"},
             {"id":"2","title":"BlackBerry 9780 Bold"},
             {"id":"3","title":"HTC Evo"}
        ];
    */
    

    To account for the possibility you may have an existing array and you’d like to update it via ajax, you can do this dynamically with PHP:

    <script type="text/javascript">
    <?php foreach($phones as $phone): ?>
        products.push({"id":"<?=$phone['id']?>","title":"<?=$phone['name']?>"});
    <?php endforeach; ?>
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on adding some analytics code into our e-commerce website. What I'm doing
I'm working on e-commerce website using VB.NET. User login to buy some of products
Working on some legacy hibernate code. How do I do the following with hbm.xml(hibernate
Background: I'm working on an e-commerce website. It was my original intention to add
i am working on an e-commerce website using php. i have tried post/get and
I have one simple question. Now I'm working on a e-commerce script and of
I'm learning C# and working on my first ASP.Net e-commerce website. I have the
I am currently working on integrating a PHP/MySQL-based e-commerce website I manage with the
I'm currently working on developing an e-commerce website and I'm going to use C#,
I'm working on a little something that will be part of an e-commerce website

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.