I am a newbie and this is my first snippet on my first ever Webpage:
I like the idea of sottwell’s PageHitcounter but the module didn’t display the results properly: It came up with the message "Please wait while MODx loads the page..." and it stayed like that. I guess there is something with incompatibilities between versions (I am on Evo 1.02).
So I started to write a snippet, that dispays the content of the counter table on a unpublished webpage.
<?php
/*
This snippet queries the table Page_Hit_Counter and returns the result as a HTML formatted table.
The content of the Page_Hit_Counter- table is iserted be the plugin "CountPageHit" from S. Ottwell's Page Hit counter.
If the table does not exist, the snippet will creat it.
In addition, there is a "Clear Counts" button, created bei a HTML Form Submit Button.
Author : Robert Schaefer
Credits to S. Ottwell for her "CountPageHIt" plugin.
Date 5. 12. 2009
*/
$id = $modx->documentIdentifier;
$url = $modx->makeUrl($id);
/* Falls Tabelle nicht existiert, wird sie erzeugt */
$sql = "CREATE TABLE IF NOT EXISTS `".$table_prefix."page_hit_counter` (
`page_id` int(4) unsigned NOT NULL default '0',
`page_count` int(10) unsigned NOT NULL default '0',
UNIQUE KEY `page_id` (`page_id`) )";
$modx->db->query($sql);
/* Seite wird mit gesetztem ok Knopf neu geladen und die Datenbanktabelle gelöscht*/
if (isset($_POST['ok']) && $_POST['ok']=='OK') {
$_POST['ok'] = 'notok';
$table = $modx->getFullTableName("page_hit_counter");
$sql = "TRUNCATE TABLE $table";
$modx->db->query($sql);
$datum = date("d.M.Y");
echo '<p>All counters cleared on '.$datum.'</p>';
echo '<p>Please enter this date as default value into the Template Variable "Hit_Clear_Date" </p> ';
}
/* Erzeugt die Sicherheitsabfrage nach drücken des Clear Counter Button */
if (isset($_POST["knopf"]) && $_POST['knopf'] == ' Clear all Counters ') {
$o3= '<p>Are you sure you want to do this?</p>';
$o3 .= '<form action=" '.$url.' " method="POST" >';
$o3 .= ' <input type="submit" name="ok" value="OK" />';
$o3 .= ' <input type="submit" name="cancel" value="Cancel" />';
$o3 .='</form>';
echo $o3;
}
/* Fragt die table mit Hitcounts ab */
$res = $modx->db->select("page_id,page_count ",$table_prefix."modx_page_hit_counter",'1=1 ',"page_count DESC");
$num = $modx->db->getRecordCount($res);
$resultate = $modx->db->makeArray($res);
/* erzeugt die Tabelle mit den Anzeigen der Hit Counts
Die Tabelle wird als HTML Code zusammengestzt und dann per echo ausgegeben */
$o='<table class = "h_grid">';
for ($i = 0; $i < $num; $i++) {
$seiten_id = $resultate[$i][page_id];
$pg_title = $modx->db->select("pagetitle",$table_prefix."modx_site_content","id=$seiten_id");
$seiten_title = $modx->db->getValue($pg_title);
$seiten_hit = $resultate[$i][page_count];
$o .= '<tr><td>';
$o .= $seiten_title;
$o .= '</td><td>';
$o .= $seiten_hit;
$o .= '</td></tr>';
}
$o .='</table>';
echo $o;
/* Erzeugt den Formularknopf um die Datenbank Tabelle zu löschen */
$o2 = '<br/><br/>';
$o2 .= '<form action=" '.$url.' " method="POST" >';
$o2 .= '<input type="submit" value=" Clear all Counters " name="knopf" />';
$o2 .= '</form>';
return $o2;
?>
Currently I am working on another one, where I want to obfuscate E-mail Adresses via the database.
Regards Robert