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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:23:03+00:00 2026-06-18T09:23:03+00:00

I really can’t find a way to print the content of the array on

  • 0

I really can’t find a way to print the content of the array on a page. Can anyone help?
I have taken the data from a database and looped it into an javascript array and now would like to just display it on the page for now.

Here is the code:

<html>

<head>

<script>


var items = {


<?php

   $con = mysql_connect("***","****","*****");
   if (!$con)
   {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("learning_game", $con);

  $result = mysql_query("SELECT * FROM data");


  $first=true;
  while($row = mysql_fetch_array($result))
{
if (!$first){
echo ",";
}
  $first = false;
echo "{name='" . $row['word'] . "',image='" . $row['image'] ."'}";

 }


 mysql_close($con);


   ?>



   };

   </script>

   </head>

<body>




</body>

   </html>
  • 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-18T09:23:05+00:00Added an answer on June 18, 2026 at 9:23 am
    <html>
      <head>
      </head>
      <body>
        <div></div>
        <script>
          var items = [
          <?php
          $con = mysql_connect("***","****","*****");
          if (!$con) {
            die('Could not connect: ' . mysql_error());
          }
          mysql_select_db("learning_game", $con);
          $result = mysql_query("SELECT * FROM data");
          $first = true;
          while ($row = mysql_fetch_array($result)) {
            if (!$first) {
              echo ",";
            }
            $first = false;
            echo "{name:'" . $row['word'] . "',image:'" . $row['image'] ."'}";
          }
          mysql_close($con);
          ?>
          ];
          for (var i = 0; i < items.length; i += 1) {
            document.getElementsByTagName('div')[0].innerHTML += items[i].name + ":" + items[i].image + "<br />\n";
          }
        </script>
      </body>
    </html>
    

    Please note, that the items declaration is now with [ ] signs, so it becomes an array, not an object.

    In the line where you echo out the name and image the equals signs became colons (:).

    As well as the full script had to be moved to the bottom, because you can only write into elements which are already rendered (closed). So I have added a new div into the mix, and write into that.

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

Sidebar

Related Questions

Can anyone help with this I really can't see what is wrong. I have
Sorry everyone, I really can't find any better title for my question. I have
I really can't find this answer... I have multiline NSString called myString in XCode,
I really can't find how i can retrieve the value from a currency field
I have 2 major problems with the Flex (4.6) SoapDecoder, I really can't get
Well, I don't know if I'm completely wrong, but I really can't find a
I really can't find a nice enum JDBC mapping example. Is enum actually supported
I have a problem that I really can't understand. I'm a newbie C programmer,
I hope this is not a dumb question but I really can't find anything
I know it's a simple question but I really can't find anything on Google.

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.