<?php
/* -----------------------------------------------------------------------------
:: Snippet: PageTrackerProcess 1.0.0
----------------------------------------------------------------------------- */
// get necessary information
$page = $_POST['page'];
$referrer = $_POST['referrer'];
$ip = $_SERVER['REMOTE_ADDR'];
// Variable setup
$views = 0;
$ips = '';
$referrers = '';
$sql = $modx->query( "SELECT * FROM `devdef_stats` WHERE page = '" . $page . "'" );
$resultArray = $modx->db->makeArray( $sql );
foreach($resultArray as $item) {
if($resultArray) {
// Get this information
$views = $item['views'];
$ips = $item['ip'].' ';
if( $item['referrer'] )
$referrers = $item['referrer'].' ';
// Set a flag to state that old information was found
$flag = true;
}
}
// Create arrays for all referrers and ip addresses
$ref_num = array();
$ip_num = array();
// Find each referrer
$values = split( ' ', $referrers );
// Set a regular expression string to parse the referrer
$regex = '%(\d+)\((.*?)\)%';
// Loop through each referrer
foreach( $values as $value ) {
// Find the number of referrals and the URL of the referrer
preg_match( $regex, $value, $matches );
// If the two exist
if( $matches[1] && $matches[2] ) {
// Set the corresponding value in the array ( referrer link -> number of referrals )
$ref_num[$matches[2]] = intval( $matches[1] );
}
}
// If there is a referrer on this visit
if( $referrer ) {
// Add it to the array
$ref_num[$referrer]++;
}
// Get the IPs
$values = split( ' ', $ips );
// Repeat the same process as above
foreach( $values as $value ) {
// Find the information
preg_match( $regex, $value, $matches );
// Make sure it exists
if( $matches[1] && $matches[2] ) {
// Add it to the array
$ip_num[$matches[2]] = intval( $matches[1] );
}
}
// Update the array with the current IP.
$ip_num[$ip]++;
// Reset the $ips string
$ips = '';
// Loop through all the information
foreach( $ip_num as $key => $val ) {
# Append it to the string (separated by a space)
$ips .= $val.'('.$key.') ';
}
// Trim the String
$ips = trim( $ips );
// Reset the $referrers string
$referrers = '';
// Loop through all the information
foreach( $ref_num as $key => $val ) {
# Append it
$referrers .= $val.'('.$key.') ';
}
// Trim the string
$referrers = trim( $referrers );
// Update the number of views
$views++;
// If we did obtain information from the database
// (the database already contains some information about this page)
if( $flag ) {
// Update it
$fields = array(
'ip' => $ips,
'views' => $views,
'referrer' => $referrers,
'page' => $page
);
$modx->db->update($fields, 'devdef_stats', 'page="' . $page . '"');
} else {
$dbTable = array();
$dbTable['ip'] = $ips;
$dbTable['views'] = $views;
$dbTable['referrer'] = $referrers;
$dbTable['page'] = $page;
$dbQuery = $modx->db->insert($dbTable, 'devdef_stats' );
};$modx->db->makeArray( $sql );
$modx->db->update($fields, 'devdef_stats', 'page="' . $page . '"');
$dbQuery = $modx->db->insert($dbTable, 'devdef_stats' );
This question has been answered by opengeek. See the first response.
So I've been following a tutorial posted I can't for the life of me figure out the database queries, according to the Revo Wiki the Evo database ($modx->db->query and similar) classes, have all depreciated and use xPDO XML definitions.
Do I try to set up xPDO definitions? ...or use traditional PHP?
Could someone give me a hand understanding the changes that need to me made? I've read up on Reverse Engineering xPDO classes from tables, but I feel far too confused to even make an attempt.
Greatly appreciated.