Jump to content

PHP Arrays and Javascript Arrays


Hilly_2004

Recommended Posts

Simple really. I'm trying to go from a PHP array to a JavaScript array in the following format:

 

var myData = new Array(['Bowled', 23], ['Caught', 44], ['LBW', 11]);

 

My question is...how?

 

The data to populate the array will be retrieved from a table in the database

 

e.g.

 

Wicket_ID

How_Out

Number_Of_Times

1

Bowled

23

2

Caught

44

3

LBW

11

 

How do I retrieve this data from the database, make it into an array, and then pass that array across into it's JavaScript equivilant?

 

Any help whatsoever would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/144975-php-arrays-and-javascript-arrays/
Share on other sites

<?php 

$db = mysql_connect("localhost", "username", "passsword");
mysql_select_db("databasename");

$strSql = "select * from table";
$rs = mysql_query($strSql);
$arrStats = array();
while($row = mysql_fetch_array($rs)) {
   $arrStats[] = array($row['How_Out'], $row['Number_Of_Times']);
}
?>
<html>
<head>
</head>
<script language='javascript'>
var myData = new Array();
<?php 
for($i=0;$i< count($arrStats);$i++) {
   echo "myData[".$i."] = new Array('".$arrStats[$i][0]."','".$arrStats[$i][1]."');\n";
}
?>

</script>

 

something like this????

 

PS: code is not tested.

<?php 

$db = mysql_connect("localhost", "username", "passsword");
mysql_select_db("databasename");

$strSql = "select * from table";
$rs = mysql_query($strSql);
$arrStats = array();
while($row = mysql_fetch_array($rs)) {
   $arrStats[] = array($row['How_Out'], $row['Number_Of_Times']);
}
?>
<html>
<head>
</head>
<script language='javascript'>
var myData = new Array();
<?php 
for($i=0;$i< count($arrStats);$i++) {
   echo "myData[".$i."] = new Array('".$arrStats[$i][0]."','".$arrStats[$i][1]."');\n";
}
?>

</script>

 

something like this????

 

PS: code is not tested.

 

I've just tested that and unfortunately the source code shows it as:

 

var myData = new Array();

myData[0] = new Array('Bowled','24');

myData[1] = new Array('Caught','11');

myData[2] = new Array('L.B.W.','5');

 

...it needs to be in the exact same format as:

 

var myData = new Array(['Bowled', 23], ['Caught', 44], ['LBW', 11]);

<?php

 

$db = mysql_connect("localhost", "username", "passsword");

mysql_select_db("databasename");

 

$strSql = "select * from table";

$rs = mysql_query($strSql);

$arrStats = array();

while($row = mysql_fetch_array($rs)) {

  $arrStats[] = array($row['How_Out'], $row['Number_Of_Times']);

}

?>

<html>

<head>

</head>

<script language='javascript'>

<?php

$str = "var myData = new Array(";

for($i=0;$i< count($arrStats);$i++) {

  $str .= "['".$arrStats[$i][0]."',".$arrStats[$i][1]."]";

  if($i+1 != count($arrStats))

  $str .= ",";

}

$str = ");";

echo $str;

?>

 

</script>

does this work 4 u ?

sorry missed out concatenation on last part..

 

<?php 

$db = mysql_connect("localhost", "username", "passsword");
mysql_select_db("databasename");

$strSql = "select * from table";
$rs = mysql_query($strSql);
$arrStats = array();
while($row = mysql_fetch_array($rs)) {
   $arrStats[] = array($row['How_Out'], $row['Number_Of_Times']);
}
?>
<html>
<head>
</head>
<script language='javascript'>
<?php 
$str = "var myData = new Array(";
for($i=0;$i< count($arrStats);$i++) {
   $str .= "['".$arrStats[$i][0]."',".$arrStats[$i][1]."]";
   if($i+1 != count($arrStats))
   $str .= ",";
}
$str .= ");";
echo $str;
?>

 

check if this works !

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.