JClark Posted November 16, 2013 Share Posted November 16, 2013 Hi all, So, I need a little help with something I'm trying to script. I'm the script : http://phpmygraph.abisvmm.nl/ to try display some data I have in a MySQL database. What I want the output to be is the below : $data = array( 'time' => players, 'time' => players, 'time' => players, ); I'm trying to create an array from 2 rows of data, which I've tried to code some below. (not complete to how I want it). As the example above, essentially I need 'time' to be the variable $row['time'] and 'players' to be the variable $row['players']. Is there some way to create the array within the white() function, or alternatively run the while function within the array? $date = date("Y-m-d"); $id = $_GET['id']; // @ will stop error if id is empty $dataarray = array(); $mysql_query = "SELECT * FROM stats WHERE `serverid`='{$id}' ORDER BY time LIMIT 7"; $mysql_result = mysql_query($mysql_query) or die(mysql_error()); while($row = mysql_fetch_array($mysql_result)){ $arrayplayers = $row['players']; $arraytime = $row['time']; $array = "'$arraytime'=>$arrayplayers"; Help much appreciated. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/283972-array-mysql-row-data/ Share on other sites More sharing options...
Ch0cu3r Posted November 16, 2013 Share Posted November 16, 2013 (edited) If you are wanting to group all players that have the same time into an array. Then you can do. $data = array(); while($row = mysql_fetch_array($mysql_result)) { // create a new array if(!isset($data[ $row['time'] ])) $data[ $row['time'] ] = array(); // add player to array $data[ $row['time'] ][] = $row['players']; } // output the array to see what is stored printf('<pre>%s</pre>', print_r($data, 1)); Edited November 16, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283972-array-mysql-row-data/#findComment-1458593 Share on other sites More sharing options...
JClark Posted November 16, 2013 Author Share Posted November 16, 2013 (edited) Thanks for the quick reply. I've just given the code a test. The array that returns is : Array ( [10:00] => Array ( [0] => 5 ) [11:00] => Array ( [0] => 8 ) [12:00] => Array ( [0] => 12 ) ) I need to get the array to appear in the same format as below: array( 'time' => players, 'time' => players, 'time' => players, ); Thanks for the help in advanced, it's much appreciated! Edited November 16, 2013 by JClark Quote Link to comment https://forums.phpfreaks.com/topic/283972-array-mysql-row-data/#findComment-1458595 Share on other sites More sharing options...
Ch0cu3r Posted November 16, 2013 Share Posted November 16, 2013 Try it and see if the data is stored in the format you want. If it is not you'll need to give us some example data from your database and then an example of how you want to store that data into an array. Quote Link to comment https://forums.phpfreaks.com/topic/283972-array-mysql-row-data/#findComment-1458597 Share on other sites More sharing options...
JClark Posted November 16, 2013 Author Share Posted November 16, 2013 Thanks for the response. I edited my post just as you replied! Quote Link to comment https://forums.phpfreaks.com/topic/283972-array-mysql-row-data/#findComment-1458598 Share on other sites More sharing options...
Ch0cu3r Posted November 16, 2013 Share Posted November 16, 2013 If there is only one player to a time then you change the while loop to just $data = array(); while($row = mysql_fetch_array($mysql_result)) { $data[ $row['time'] ] = $row['players']; } Which will give you this array for $data Array ( [10:00] => 5 [11:00] => 8 [12:00] => 12 ) Quote Link to comment https://forums.phpfreaks.com/topic/283972-array-mysql-row-data/#findComment-1458600 Share on other sites More sharing options...
JClark Posted November 16, 2013 Author Share Posted November 16, 2013 Fantastic! That's exactly what I needed to know. Everything is working perfectly. Essentially a cron job runs every hour storing stats to the time, and with the array the script passes the data to a php graph generator and thus shows a graph of the data I require, Many thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/283972-array-mysql-row-data/#findComment-1458601 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.