Jump to content

Insert php database data into a javascript array?


icezapp

Recommended Posts

Hello =)

 

I'm trying to create a javascript array which should be filled with data from a mysql database.

so what i wannt to do is to loop out a new array element for each row in my database.

I should also say that i'm working in a php file.

 

I tried (and failed) to do it like this:

 

 

<?php include('databbaseconnect.php'); ?>
<script type="text/javascript">
var StoreDetails = [
['Please select an option','','','','',''],
<?php
mysql_select_db("alltomboule_se", $db);
$result = mysql_query("SELECT * FROM spelare");
while($row = mysql_fetch_array($result))
{
        echo "['".$row['namn'].$row['enamn']."','".$row['namn'].",'".$row['enamn']."','".$row['klubb']."','".$row['licensnr']."','".$row['id']."'],";
}
?>
['','','','','',''] // Note: no comma for last row
];

 

I wannt the array to look like this:

var StoreDetails = [
     ['Please select an option','','','','',''],
     ['Oscar Andersson','Oscar','Andersson','BON','16643','2'],
     ['Olle Johansson','Olle','Johansson','BFT','14443','3'],
     ['Ingvar Andersson','Ingvar','Andersson','BON','14333','4'],
     ['Kalle Kula','Kalle','Kula','BON','34576','5'],
     ['','','','','',''] 
];

 

Link to comment
Share on other sites

I'm not very familiar to json but I tested some.

With the json_encode() I get it in php format but I wannt it in javascript format.

 

so basicly i want to reciev all rows form my database and convert it into a javascript array with the format below.

 

var StoreDetails = [
     ['Please select an option','','','','',''],
     ['Oscar Andersson','Oscar','Andersson','BON','16643','2'],
     ['Olle Johansson','Olle','Johansson','BFT','14443','3'],
     ['Ingvar Andersson','Ingvar','Andersson','BON','14333','4'],
     ['Kalle Kula','Kalle','Kula','BON','34576','5'],
     ['','','','','',''] 
];

 

as i said i'm new to Json and pretty new to Javascript so i'm greatful for help

Link to comment
Share on other sites

  • 1 year later...

карточная игра ответ поле чудес nevosoft Игровые Автоматы Император букс казино еще игровые автоматы играть бесплатно онлайн за интерес чайковский Автоматы онлайн казино покер техасский правила!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.