Solved

get distinct list of mysql records filter by ip.date and page/query

Posted on 2014-01-10
12
573 Views
Last Modified: 2014-01-10
i have a table of
-- Table structure for table `visitor_tracking`
--

DROP TABLE IF EXISTS `visitor_tracking`;
CREATE TABLE IF NOT EXISTS `visitor_tracking` (
  `entry_id` int(11) NOT NULL auto_increment,
  `visitor_id` int(11) default NULL,
  `ip_address` varchar(15) NOT NULL,
  `page_name` text,
  `query_string` text,
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
  PRIMARY KEY  (`entry_id`),
  KEY `visitor_id` (`visitor_id`,`timestamp`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;

Open in new window


with data like
INSERT INTO `visitor_tracking` (`entry_id`, `visitor_id`, `ip_address`, `page_name`, `query_string`, `timestamp`) VALUES
(32, 1, '98.28.197.00', '/single_event_page.php', 'id=5', '2014-01-10 19:18:18'),
(33, 1, '98.28.197.00', '/single_event_page.php', 'id=2', '2014-01-10 19:31:49');

Open in new window

sorry for only 2 entries

i need to get the page views for unique hits so i assume i need to group ips and filter by page_name and query_string as the page name is with the query string.

how do i do i return records for a certain date and for the month?
im not understanding how to use distinct and group correctly in my mysqli select string.

thank you for any code or help you may provide.
Johnny
0
Comment
Question by:Johnny
  • 8
  • 4
12 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 39772376
Post your expected result.
0
 

Author Comment

by:Johnny
ID: 39772391
Sample of page tracking
ive been playing with this code
<?php
include("db_connect.php");
//retrieve the appropriate visitor data
$view = $_GET["view"];
//set a default value for $view
if($view!="all" && $view!="record" && $view!="pages")
  $view = "all";
if($view == "all")
{
    //show all recent visitors
    $sql = "SELECT visitor_id, GROUP_CONCAT(DISTINCT ip_address) as ip_address_list,
        COUNT(DISTINCT ip_address) as ip_total, COUNT(visitor_id) as page_count,
        MIN(timestamp) as start_time, MAX(timestamp) as end_time FROM visitor_tracking GROUP BY visitor_id";
    $result = mysql_query($sql);
    if($result==false){
        $view = "error";
        $error = "Could not retrieve values";   
    }
} elseif($view == "pages")
{
    //show all recent visitors
    $sql = "SELECT visitor_id, page_name, query_string, GROUP_CONCAT(DISTINCT ip_address) as ip_address_list,
        COUNT(DISTINCT ip_address) as ip_total, COUNT(DISTINCT ip_address) as page_count,
        timestamp FROM visitor_tracking GROUP BY query_string";
    $result = mysql_query($sql);
    if($result==false){
        $view = "error";
        $error = "Could not retrieve values";   
    }
}else {
    //show pages for a specific visitor
    $visitor_id = $_GET['id'];
    //rung $visitor_id through filter_var to check it's not an invalid
    //value, or a hack attempt
    if(!filter_var($visitor_id, FILTER_VALIDATE_INT, 0)){
        $error = "Invalid ID specified";   
        $view = "error";
    } else {
        $sql = "SELECT timestamp, page_name, query_string, ip_address FROM
          visitor_tracking WHERE visitor_id = '$visitor_id'";
        $result = mysql_query($sql);
    }    
}
function display_date($time){
    return date("F j, Y, g:i a", $time);   
}

?>
<html>
<head>
<title>IP Tracker Report Page</title>
<style>
html {font-family:tahoma,verdana,arial,sans serif;}
body {font-size:62.5%;}
table tr th{
    font-size:0.8em;
    background-color:#ddb;    
    padding:0.2em 0.6em 0.2em 0.6em;
}
table tr td{
    font-size:0.8em;
    background-color:#eec;
    margin:0.3em;
    padding:0.3em;
}
</style>
</head>
<body>
<h1>IP Tracker Report</h1>
<?php if($view=="all") {
    //display all of the results grouped by visitor
    if($row = mysql_fetch_array($result)){
    ?>
    <table>
      <tbody>
      <tr>
      <th>Id</th>
      <th>IP Address(es)</th>
      <th>Entry Time</th>
      <th>Exit Time</th>
      <th>Duration</th>
      <th>Pages visited</th>
      <th>Actions</th>
      </tr>
    <?php
      do{
        if($row["ip_total"] > 1)
            $ip_list = "Multiple addresses";
        else
            $ip_list = $row["ip_address_list"];
        $start_time = strtotime($row["start_time"]);
        $end_time = strtotime($row["end_time"]);
        $start = display_date($start_time);
        $end = display_date($end_time);
        $duration = $end_time - $start_time;
        if($duration >= 60) {
            $duration = number_format($duration/60, 1)." minutes";
        }
        else {
            $duration = $duration." seconds";   
        }
        echo "<tr>";
        echo "<td>{$row["visitor_id"]}</td>";
        echo "<td>$ip_list</td>";
        echo "<td>$start</td>";
        echo "<td>$end</td>";
        echo "<td>$duration</td>";
        echo "<td>{$row["page_count"]}</td>";
        echo "<td><a href='ip_report.php?view=record&id={$row["visitor_id"]}'>view</a></td>";
        echo "</tr>";
      } while ($row = mysql_fetch_array($result));
    ?>
      </tbody>
    </table>
    <?php } else { ?>
      <h3>No records in the table yet</h3>
    <?php } ?>
<?php } elseif($view=="record"){ ?>
  <h3>Showing records for Visitor <?php echo $visitor_id; ?></h3>
  <p><a href="ip_report.php">back</a></p>
  <?php
    //show all pages for a single visitor
    if($row = mysql_fetch_array($result)){
    ?>
    <table>
      <tbody>
      <tr>
      <th>Page viewed</th>
      <th>Query string</th>
      <th>Time of view</th>
      </tr>
    <?php
      do{
        if($row["ip_total"] > 1)
            $ip_list = "More than 1";
        else
            $ip_list = $row["ip_address_list"];
        $time = display_date(strtotime($row["timestamp"]));
        echo "<tr>";
        echo "<td>{$row["page_name"]}</td>";
        echo "<td>{$row["query_string"]}</td>";
        echo "<td>$time</td>";
        echo "</tr>";
      } while ($row = mysql_fetch_array($result));
    ?>
      </tbody>
    </table>
    <?php } else { ?>
      <h3>No records for this visitor</h3>  
    <?php
    }
}elseif($view=="pages"){ ?>
  <h3>Showing records for Pages <?php echo $visitor_id; ?></h3>
  <p><a href="ip_report.php">back</a></p>
  <?php
    //show all pages for a single visitor
    if($row = mysql_fetch_array($result)){
    ?>
    <table>
      <tbody>
      <tr>
      <th>Page viewed</th>
      <th>Query string</th>
      <th>Hits</th>
      </tr>
    <?php
      do{
        if($row["ip_total"] > 1)
            $ip_list = "More than 1";
        else
            $ip_list = $row["ip_address_list"];
        $time = display_date(strtotime($row["timestamp"]));
        echo "<tr>";
        echo "<td>{$row["page_name"]}</td>";
        echo "<td>{$row["query_string"]}</td>";
        echo "<td>{$row["page_count"]}</td>";
        echo "</tr>";
      } while ($row = mysql_fetch_array($result));
    ?>
      </tbody>
    </table>
    <?php } else { ?>
      <h3>No records for this page</h3>  
    <?php
    }
}


 elseif($view=="error") { ?>
    <h3>There was an error</h3>
    <?php echo $error;
}
?>

</body>
</html>

Open in new window


the pages for all hits seams to work, i need to filter a certain day and a complete month(date range) now and i think i have it.
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 39772480
Do you have duplicate entries for page_name,query_string?
What is the result of this query and what is the expected result?
select page_name,query_string,count(*)
  from visitor_tracking
 group by page_name,query_string;

Open in new window

0
 

Author Comment

by:Johnny
ID: 39772527
hits are recorded by ip. so yes there are multiple entries

im looking for unique hits for each page as a table. and to filter by a certain day and month

i guess now all i need is the date filter
0
 

Author Comment

by:Johnny
ID: 39772541
returned results for sql querry
returned from
$sql="select page_name,query_string,count(*)
  from visitor_tracking
 group by page_name,query_string;";

Open in new window


that groups them in alpha order too..still need hits count how do i print count field
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39772547
this condition selected records on Feb1st. change it as per your need.
WHERE DAYOFMONTH(`timestamp`) = 1 -- change it to wathever the day you want.
AND MONTH(`timestamp`) = 2

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Johnny
ID: 39772561
wow thats nifty for month and day ill have to look up year too

still how do i get count for hits please?
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39772574
You have year function too.
AND YEAR(`timestamp`)=2014

Open in new window

If you want altogether, you can simply apply
`timestamp`='2014-02-01'

Open in new window

Coming to the count(*), Is it not printing the count at all? Are you getting blank under this column?
0
 

Author Comment

by:Johnny
ID: 39772578
this works
 $sql="select page_name,query_string,count(DISTINCT ip_address) as page_count
  from visitor_tracking
 group by page_name,query_string;";

Open in new window

for unique hits
0
 

Author Comment

by:Johnny
ID: 39772580
fyi for anyone else that comes across this post

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
0
 

Author Closing Comment

by:Johnny
ID: 39772585
thank you
0
 

Author Comment

by:Johnny
ID: 39772587
and just so others know i needed to limit the query strings too so heres that code
    $words = array( 'id=5','id=73' );
$whereClause = '';
foreach( $words as $word) {
   $whereClause .= ' query_string LIKE "%' . $word . '%" OR';
}

// Remove last 'OR'
$whereClause = substr($whereClause, 0, -2);

    /*
    $sql = "SELECT visitor_id, page_name, query_string, GROUP_CONCAT(DISTINCT ip_address) as ip_address_list,
        COUNT(DISTINCT ip_address) as ip_total, COUNT(DISTINCT ip_address) as page_count,
        timestamp FROM visitor_tracking WHERE" . $whereClause ." GROUP BY query_string";
   */
   
    //echo $sql; die;

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now