Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

asked on

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

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
Avatar of Sharath S
Sharath S
Flag of United States of America image

Post your expected result.
Avatar of Johnny

ASKER

User generated image
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.
ASKER CERTIFIED SOLUTION
Avatar of Sharath S
Sharath S
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Johnny

ASKER

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
Avatar of Johnny

ASKER

User generated image
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
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

Avatar of Johnny

ASKER

wow thats nifty for month and day ill have to look up year too

still how do i get count for hits please?
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?
Avatar of Johnny

ASKER

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
Avatar of Johnny

ASKER

fyi for anyone else that comes across this post

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

ASKER

thank you
Avatar of Johnny

ASKER

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