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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.