Cheth.com


C
ode is a grossly misleading term. It implies that programs must be incomprehensibly constructed by secret guilds of cryptographers, that the goal of programming is maximum obfuscation through inscrutable cleverness, and that the programmer's tricks of the trade are much like the old magicians "code"—explanations shall never be revealed.

The truth is code must be readable. Over time, the major cost of code is not the development effort, it is the amount of time spent on maintenance. Consequently, creating code that isn't unduly "coded" is always the wiser choice.

The following example is a short, object-oriented report that analyzes new account activity for a Drupal powered Web site. The report accepts parameters from a pull-down select list, runs a SQL query, and hands off the report formatting chores to a standardized view.

<?php
/**
*----------------------------
* lookupRegistrationAttempts.php
*----------------------------
* Purpose: Look up Registration Attempts
*----------------------------
* 1.00 (cheth) 2009-Apr-16 initial implementation
* 1.01 (cheth) 2009-Apr-26 allow reporting for various time periods.
*----------------------------
*/

// ------self-identification----------
$program[] = "lookupRegistrationAttempts.php";
$version[] = "1.01";

//--------------------
// initialize classes
//--------------------
require_once('models/MySQLReportModel.php'); # MySQL reporting model
$rep = new MySQLReporter;

//----------------------------------
// prepare list of run-time options
//----------------------------------
$myParams[0][] = "1|last 24 hours"; // "$key|$value";
$myParams[0][] = "2|last two days";
$myParams[0][] = "7|last week";
$myParams[0][] = "31|last month";

//----------------------
// setup runtime params
//----------------------
$myParams[0]["id"] = "days";
$myParams[0]["prompt"] = "specify timeframe: ";
$myParams[0]["type"] = "select";

$rep->setParams($myParams);

//--------------------------
// get runtime param values
//--------------------------
$myValues = $rep->getParamValues();
$myQuestion_id = $myValues[0];
$myQuestion_title = "";

foreach ($myParams[0] as $myParam) {
      list($key, $val) = split("\|", $myParam);
      if ($key==$myQuestion_id) {
            $myQuestion_title = $val;
      };
};

//-------------------
// set report params
//-------------------
$rep->setProgram($program[0]);
$rep->setVersion($version[0]);
$rep->setURL($program[0]);
$rep->setTitle("Registration Attempts $myQuestion_title");
$rep->setLogout();

//---------------
// set timestamp
//---------------
$myTimestamp = time() - (86400 * $myQuestion_id);

//-------------------------------
// acquire New User info
//-------------------------------
$myQuery = <<< EOT
SELECT COUNT(*) AS counter, accesslog.hostname,
MIN(FROM_UNIXTIME(accesslog.timestamp - 10800)) AS earliest,
MAX(FROM_UNIXTIME(accesslog.timestamp - 10800)) AS latest
,max(accesslog.title) AS max_title, accesslog.uid
,users.name
, (select count(*) from accesslog s WHERE s.hostname = accesslog.hostname AND path = "user/register"
AND timestamp >= "$myTimestamp"
)
as registration_attempts
, (select count(*) from accesslog s WHERE s.hostname = accesslog.hostname AND path = "robots.txt"
AND timestamp >= "$myTimestamp"
)
as google_robots
FROM accesslog
LEFT OUTER JOIN users on users.uid = accesslog.uid
WHERE timestamp >= "$myTimestamp"
GROUP BY hostname HAVING registration_attempts > 0
ORDER BY registration_attempts DESC, counter DESC
EOT;

//-----------------
// generate report
//-----------------
echo ($rep->PrepareReport($myQuery));

exit;

?>




Copyright © 1996-2009 Cheth Rowe Consulting
Questions? Please use our contact form


just a counter

d   e   s   i   g   n   by cheth