dhimo Posted September 7, 2007 Share Posted September 7, 2007 I was wonderingHow to count entries or hits made each hour of the day?I want to loop all hrs of the day like00 01 02 03 04 05 06 ... 12 13 ... 20 21 22 23and then get the nr of hits for each oneany idea is appriciated Link to comment Share on other sites More sharing options...
user4fun Posted September 7, 2007 Share Posted September 7, 2007 ??????????//The only thing i know about is to install a pragram that does that, i know of one good one BUT i dont like to promte things like that on forums. A google search might bring up a few good resultsDarn it, i just promoted google, ooops Link to comment Share on other sites More sharing options...
justsomeguy Posted September 7, 2007 Share Posted September 7, 2007 You'll need to have something on each page that records the hit in either a log file or a database. A database would make it a lot easier to process the log entries. You would want to store the page they accessed and the date and time when they access it. So you would need a piece of PHP code on every page that you want to track to add an entry to the database whenever someone loads the page, then you can write a piece of code to search through the database and get all the info.Keep in mind that keeping track of things like this start to take up a lot of disk space very quickly. If you don't clean out the old log entries regularly you are going to run into out-of-space messages eventually.Most hosts also come with pre-installed stats packages that provide this type of information already. Link to comment Share on other sites More sharing options...
dhimo Posted September 7, 2007 Author Share Posted September 7, 2007 I just need an sql tip on how to run the sql query to make that type of selection. I have gd class chart and I just want to show on chart. Its only for learning purposes Link to comment Share on other sites More sharing options...
justsomeguy Posted September 7, 2007 Share Posted September 7, 2007 The SQL statement is entirely dependent on how you set up the database in the first place. If it is a date field, you can use the SQL date functions to return results in a certain range. If it is an integer field with a Unix timestamp, you can use the date functions in PHP to figure out which range to get. Link to comment Share on other sites More sharing options...
Synook Posted September 7, 2007 Share Posted September 7, 2007 This is the UPDATE query somewhat like what JustSomeGuy may be hinting to mysql_query("UPDATE hitcount_table SET hits = hits + 1 WHERE hour = '" . date("H") . "'"); For this you only need a single table (hitcount_table) with two columns: hour INT(2) which has all the hours preset (from 0 to 23), and the hits INT(6) DEFAULT 0 column, which will record the hits for each hour.I'm not too familiar with GD, but to select the results, you would just run $result = mysql_query("SELECT * FROM hitcount_table");while ($hour = mysql_fetch_assoc($result)) {$hour['hour']; //This is the hour$hour['hits']; //This is the number of hits//You could use these values to add a chart element} Link to comment Share on other sites More sharing options...
user4fun Posted September 7, 2007 Share Posted September 7, 2007 hour problemthe hour selected by php is the users hours, if you are expecting uses from china, this code will be a waste, because you might start thinkg users are coming on at 9:00a.m but they actually come on at midnight. Link to comment Share on other sites More sharing options...
Synook Posted September 8, 2007 Share Posted September 8, 2007 That is true... hmm... you are right, that will only record the server time that the client comes at, not the user time, so would be unhelpful in some respects, although you still will be able to see at what server times the people come on.There is no way of detecting the client's time purely through PHP, you will need to use AJAX to get JavaScript to send the client's time through to your PHP code, like in the example below. All database tables remain the same, and the method of selection will still be as above.This goes in the header of the document you want to count <script type="text/javascript">function count_hit() { var ua = navigator.userAgent.toLowerCase(); if (!window.ActiveXObject) request = new XMLHttpRequest(); else if (ua.indexOf('msie 5') == -1) request = new ActiveXObject("Msxml2.XMLHTTP"); else request = new ActiveXObject("Microsoft.XMLHTTP"); today = new Date(); thishour = today.getHours(); request.open("GET", "count_hit.php?hour=" + thishour, true); request.send(null);}</script> And set the body onload to run the script <body onload="count_hit()"> Then, have a page called count_hit.php that will update the database. This is all it needs to have: mysql_connect("host", "username", "password"); //Replace with actual MySQL detailsmysql_select_db("hits"); //Replace with actual database namemysql_query("UPDATE hitcount_table SET hits = hits + 1 WHERE hour = '" . $_GET['hour'] . "'"); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.