Hilly_2004 Posted February 12, 2009 Share Posted February 12, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/144975-php-arrays-and-javascript-arrays/ Share on other sites More sharing options...
premiso Posted February 12, 2009 Share Posted February 12, 2009 You can use json_encode as far as how to use that I do not know. But I know it is common practice, especially with AJAX scripts. Quote Link to comment https://forums.phpfreaks.com/topic/144975-php-arrays-and-javascript-arrays/#findComment-760821 Share on other sites More sharing options...
samshel Posted February 12, 2009 Share Posted February 12, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/144975-php-arrays-and-javascript-arrays/#findComment-760828 Share on other sites More sharing options...
Hilly_2004 Posted February 12, 2009 Author Share Posted February 12, 2009 <?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]); Quote Link to comment https://forums.phpfreaks.com/topic/144975-php-arrays-and-javascript-arrays/#findComment-760841 Share on other sites More sharing options...
samshel Posted February 12, 2009 Share Posted February 12, 2009 <?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 ? Quote Link to comment https://forums.phpfreaks.com/topic/144975-php-arrays-and-javascript-arrays/#findComment-760844 Share on other sites More sharing options...
Hilly_2004 Posted February 12, 2009 Author Share Posted February 12, 2009 Afraid not... When you echo the str variable, it just prints out: ");" Quote Link to comment https://forums.phpfreaks.com/topic/144975-php-arrays-and-javascript-arrays/#findComment-760851 Share on other sites More sharing options...
samshel Posted February 12, 2009 Share Posted February 12, 2009 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/144975-php-arrays-and-javascript-arrays/#findComment-760853 Share on other sites More sharing options...
Hilly_2004 Posted February 12, 2009 Author Share Posted February 12, 2009 Wahey! Get in, works like a charm. Have a look at your handy work: http://www.kingjamescc.co.uk/barchart.php Quote Link to comment https://forums.phpfreaks.com/topic/144975-php-arrays-and-javascript-arrays/#findComment-760857 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.