PHP: Filter MYSQL array
I'm developing an visits counter and I'd like to increase a number depending on the browser and the operating system.
For example:
Firefox Mac OS X
Safari Mac OS X
Firefox Mac OS X
Firefox Linux
Then the website would display
Safari on Mac OS X 1 visit
Firefox on Mac OS X 2 visits
Firefox on Linux 1 visit
There are a lot of browsers and operating systems combinations, then I'd like to use an algorithm that will calculate that easily. The data is retrieved from a MYSQL database. Does anyone knows a good way to do开发者_Go百科 it?
SELECT browser, os, COUNT(*) as c FROM table GROUP BY browser, os;
The function get_browser will return a rather large array with all the information it can know about the user.
You should put this array in a database, probably with a column for each piece of information that get_browser() returns:
$browser = get_browser()
print_r($browser);
foreach ($browser as $key => $information) { // You may want to use array_walk for this, wich is harder to read in a simple example.
$browser_clean[$key] = mysql_real_escape_string($information); //DO NOT FORGET TO SANITIZE, ppl can spoof the browserstrings and open up SQL injection holes!
}
//You probably want some slightly cleaner and better way to talk to the Database then boiling up some ugly SQL-string, though.
mysql_query('INSERT INTO browserstats (parent, platform, browser, ...restofyourfields) VALUES ('. $browser_clean['parent'] .','. $browser_clean['platform'] .','. $browser_clean['browser'] .'...restofthefields)')
;
精彩评论