database - How to format a specific string of data in PHP? -


so, have .txt file full of data of uses of program on site. format goes follows:

~input|methodused|user|userinfo|month|day|year|hour|minute\n 

every time uses program, adds line text file. i'm working on statistics page. i'm wondering 3 things:

how manage string of entries (using file_get_contents) to:

get uses in specific day (say 08|17|17)

in day, common user and/or input

and overall, common user and/or input

i assume difference in code between finding common user , common input hardly any. can me accomplish any, or 3 of these tasks in php? i'm aware of php's explode() function , assume necessary complete task. know way of handling data not best, not want change it. have months of data stored way. if failed add information necessary complete task, please let me know.

like mario suggesting, put data in database, , make simple stats looking for. i'll show example of doing sqlite3 database.

first, you'd want import data. use file_get_contents, because had test this, imported string, , because didn't know data looks like, made up:

$db = new sqlite3( __dir__ . '/logs.db'); $db->exec('     create table log (         input text,         methodused text,         user text,         userinfo text,         datetime text     ) ');  $str = "~a|3|bob|aaa|12|04|2000|23|05\n ~b|6|bill|bbb|08|18|2017|14|18\n ~c|4|tina|ccc|09|28|2016|16|33\n ~d|7|doug|ddd|11|07|2017|08|24\n ~e|1|fred|eee|01|22|2015|00|16\n";  // array of lines $lines = explode( php_eol, $str );  // loop through lines foreach( $lines $line ) {     if( ! empty( $line ) )     {         list(             $input,             $methodused,             $user,             $userinfo,             $month,             $day,             $year,             $hour,             $minute         ) = explode( '|', $line );          $datetime = date('y-m-d h:i:s', strtotime($year . '/' . $month . '/' . $day . ' ' . $hour . ':' . $minute . ':00' ) );          $db->exec("             insert log (input, methodused, user, userinfo, datetime)              values (\"$input\",\"$methodused\",\"$user\",\"$userinfo\",\"$datetime\")         ");     } } 

now there's database data in it, it's easy queries give stats want. connection again:

$db = new sqlite3( __dir__ . '/logs.db'); 

this selects users in each row

$results = $db->query('select user log'); 

this selects all rows datetime in last 6 months

$results = $db->query('select * log datetime > datetime("now", "-6 months")'); 

this selects all rows datetime on specific day

$results = $db->query('select * log date(datetime) = "2017-08-18"'); 

get common user on specific day

$results = $db->query('select user, count(*) cnt log date(datetime) = "2017-08-18" group user order cnt desc limit 1'); 

get common user of records

$results = $db->query('select *, count(*) cnt log group user order cnt limit 1'); 

finally, display results of of results above (choose 1 @ time):

while( $row = $results->fetcharray(sqlite3_assoc) ) {     echo '<pre>';     print_r( $row );     echo '</pre>'; } 

i'm sure you'd want make own modifications, or perhaps use mysql database instead (which i'd do), ought started. did test code, should meet needs, if considering type of thing.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -