开发者

Get the last 6 rows of MySQL and define the variables for each?

So, I am going to be running this script every 4 hours to populate my database.

My question is is it possible to get the last 6 rows and define the variables for each?

So I can use them to create a last 24 hour graph..

like:

$value1,$time1,$ap1

$value2,$time2,$ap2

$value3,$time3,$ap3

$value4,$time4,$ap4

$value5,$time5,$ap5

$value6,$time6,$ap6

<?php
$c = curl_init();
curl_setopt_array($c, array(
    CURLOPT_URL => 'http://www.bungie.net/stats/reach/online.aspx',
    CURLOPT_RETURNTRANSFER => true,
    ));
$r = curl_exec($c);
curl_close($c);

preg_match_all('|([\w\s]+)</a> </h4>\s*([0-9,]+) Players|s', $r, $m);
$teams = array_combine($m[1], $m[2]);
foreach ($m[2] as &$v) $v = str_replace(',','',$v);
//echo '<pre>'.print_r($teams,1).'</pre>';

//array with values
$a = $m[2];
//define idividual variables
$noble_map_pack = $a[0];
$rumble_pit = $a[1];
$living_dead = $a[2];
$team_slayer = $a[3];
$mlg = $a[4];
$team_swat = $a[5];
$team_snipers = $a[6];
$team_objective = $a[7开发者_如何转开发];
$multi_team = $a[8];
$big_team_battle = $a[9];
$invasion = $a[10];
$firefight = $a[11];
$score_attack = $a[12];
$coop_campaign = $a[13];
$team_arena = $a[14];
$doubles_arena = $a[15];
$ffa_arena = $a[16];

//define time variables
date_default_timezone_set('America/New_York');
$time = date("g:i");
$ap = date("A");
$date = date("YmdHis");

//include_once "mysql_con.php";
$d_host = "localhost";
$d_user = "";
$d_pass = "";
$d_name = "database";

@mysql_connect("$d_host","$d_user","$d_pass") or die ("Could not connect!");
@mysql_select_db("$d_name") or die ("No database!");

mysql_query("INSERT INTO `noblemappack`
(`value`,`time`,`ap`,`date`)
VALUES ('".$noble_map_pack."','".$time."','".$ap."','".$date."')")

?>


Query (assuming time is a datetime column that can be sorted):

SELECT * FROM `noblemappack` ORDER BY `time` DESC LIMIT 6

If you don't have a datetime field you can sort by, add one. "The last 6" is pretty vague otherwise.
If you just put the result in an array, you're all set. You shouldn't keep values in individual variables, keeping them as sets of data in an array is much easier to work with.

array(
    array('value' => 'foo', 'time' => '13:45', 'date' => '2011-02-03'),
    array('value' => 'bar', 'time' => '22:41', 'date' => '2011-02-04'),
    ...
);

Same for your other variables:

list($data['noble_map_pack'], $data['rumble_pit'], ...) = $a;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜