Jump to content

Array MySQL row data


JClark

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/283972-array-mysql-row-data/
Share on other sites

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));

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!

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
)

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

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.