JClark Posted November 18, 2013 Share Posted November 18, 2013 Hi there, Just a simple question for all you coders. I'm trying to make a PHP Array from MySQL results. The two rows are 'players' and 'time'. The players row has plain numerical data and time, time postings (11:00 and so on). The output's of the two arrays I'm after are in the formats below : (example) 1,2,10,5,32,5,0 (example time) "11:00", "11:15", "11:30", The code I'm currently attemping with is : while($row = mysql_fetch_array($mysql_result)) { $dataplayers[ $row['players'] ] = array(); $datatime[ $row['time'] ] = array(); } As it stands, I seem to be getting no output at all. Thanks for your help in advanced! Quote Link to comment Share on other sites More sharing options...
requinix Posted November 18, 2013 Share Posted November 18, 2013 Not sure if I understand, but maybe $dataplayers = $datatime = array(); while($row = mysql_fetch_array($mysql_result)) { $dataplayers[] = $row['players']; $datatime[] = $row['time']; }Also, stop using the mysql extension and its mysql_* functions and move to PDO or mysqli. Why? Because it's old, insecure, slow, and no longer supported with PHP. Quote Link to comment Share on other sites More sharing options...
JClark Posted November 18, 2013 Author Share Posted November 18, 2013 Not sure if I understand, but maybe $dataplayers = $datatime = array(); while($row = mysql_fetch_array($mysql_result)) { $dataplayers[] = $row['players']; $datatime[] = $row['time']; }Also, stop using the mysql extension and its mysql_* functions and move to PDO or mysqli. Why? Because it's old, insecure, slow, and no longer supported with PHP. Thanks for the reply. The code works fine, but when I print the array to test the output I'm getting the below : Array ( [0] => 17 [1] => 14 [2] => 11 [3] => 11 [4] => 7 [5] => 5 ) Array ( [0] => 21:15 [1] => 21:00 [2] => 20:45 [3] => 20:30 [4] => 20:15 [5] => 20:00 ) I need to try get it to array in the format below (if it's possible). I know it's possible manually. Array(17,14,11,11,7,5) Array("21:15", "21:00", "20:45", "20:30", "20:15", "20:00") I'll try to explain briefly what it's for. The data is being pulled from the database to be used with PChart to display a line graph. The below example is how PChart requests the data to be displayed : $myData->addPoints(array(-39,17,32,41,-48,47,29,45),"Series1"); This is what I was planning to do if I can get the array working... $myData->addPoints($datatime,"Series1"); Again thanks for your time. Realise I'm maybe doing things a little backwards here, Apologies for my scripting. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 18, 2013 Share Posted November 18, 2013 so use $myData->addPoints($datatimes,"Series1"); echo '<pre>'; print_r( Array(17,14,11,11,7,5) ); echo '<pre>'; produces Array ( [0] => 17 [1] => 14 [2] => 11 [3] => 11 [4] => 7 [5] => 5 ) exactly the same as Requinix's array Quote Link to comment Share on other sites More sharing options...
JClark Posted November 18, 2013 Author Share Posted November 18, 2013 Fantastic, that has done the trick. Apologies for my lack of understanding on arrays! Quote Link to comment 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.