Jump to content

Print out mysql data for hit counter


brandontran

Recommended Posts

I am trying to display hits to website pages in a specific format. When a visitor views the page, the sessions are recorded in the database similar to this.

 

Example snippet from my mysql database table:

id - page - date

1, page1, 2009-10-07

2, page3, 2009-10-07

3, page2, 2009-10-07

4, page9, 2009-10-08

5, page5, 2009-10-08

 

I would like to print this table in which the dates are unique and the rows are counted for those unique dates.

 

So end result would be an array such as this:

Array

(

    [2009-10-07] => 3

    [2009-10-08] => 2

)

 

Here is what I came up with so far however it isn't totaling hits for the dates. Which is why I'm asking for some help. I have messed with this a great deal with only this progress. Any help is greatly appreciated.

 

<?php

require_once("config.php");

 

@mysql_connect(localhost,$db_user,$db_pass) or die("Unable to connect to database");

@mysql_select_db($db_name) or die( "Unable to select database");

 

$query = "SELECT date FROM views Order by id DESC LIMIT 0,20 ";

$result = mysql_query($query);

 

$data = array();

 

while($item=mysql_fetch_row($result))

    {

$date = $item[0];

$total = count($date);

$data[$date] = $total;

    }

 

print_r($data);

 

?>

Link to comment
https://forums.phpfreaks.com/topic/180919-print-out-mysql-data-for-hit-counter/
Share on other sites

I solved it:  This was the catch line: print_r(array_count_values($data));

 

 

@mysql_connect(localhost,$db_user,$db_pass) or die("Unable to connect to database");
@mysql_select_db($db_name) or die( "Unable to select database"); 


$query = "SELECT date FROM views ORDER BY date ASC LIMIT 0,20";
$result = mysql_query($query);

while($row=mysql_fetch_array($result))
    {
$data[] = $row['date'];
    }

print_r(array_count_values($data));

 

Which prints out:

 

Array
(
    [2009-11-04] => 3
    [2009-11-05] => 1
    [2009-11-06] => 1
    [2009-11-09] => 15
)

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.