Quickie Visitors Country WP Plugin

This plugin is dependent on WP/GeoTrack by Johannes Lietz, this is NOT a stand-alone plugin, it won’t work if WP/GeoTrack is not installed anyway, so it’s safe.

And don’t look for it in WP Plugins repo, you won’t find it there :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
Plugin Name: Top Visitors Flag
Plugin URI: http://www.marvinmarcelo.com/
Description: This plugin displays visitors on sidebar as widget dependent on <a href="http://dadabase.de/weblog/archives/2006/05/23/wordpress-geotrack-plugin">WP/GEO-Track plugin</a>.
Author: Marvin Marcelo
Author URI: http://www.marvinmarcelo.com/
Version: 1.0.0
*/
function tvf_get_topvisits()
{
  global $wpdb, $table_prefix;
  $sql = "select count(*) as `rows` , `country` , `flagname` from `".$table_prefix."geotrack` group by `country` order by `rows` desc limit 0 , 12";
  $flagstats = $wpdb->get_results($sql);
 
  $html = "<li id='top-country-flags'><h2>Top Visiting Countries</h2><div>\n"; /*  id string */
  foreach ($flagstats as $cf)
  {
    $cf->rows = number_format($cf->rows, 0, "", ",");
    $html .= "<img src=\"". GEOTRACK_URL . "flags/" . $cf-/>flagname . ".png\" alt=\"Flag of $cf->country with $cf->rows visits\" title=\"$cf->country with $cf->rows visits\" />\n";
  }
  $html .= "</div></li>";
  echo $html;
}
 
function init_tvf_init(){
	register_sidebar_widget("Top Visitors Flags", "tvf_get_topvisits");
}
 
if (defined('GT_NOT_INSTALLED'))
{
  echo "<!-- You need \"http://dadabase.de/weblog/archives/2006/05/23/wordpress-geotrack-plugin\" installed first-->";
}
else
{
  add_action("plugins_loaded", "init_tvf_init");
}

Also, you have to forgive the function, constants, and variables names, we all struggle for a better and sensible one :) Happy coding!

PHP code block – bringing order to chaos

I had to come up with this post because I’ve been working on an old CMS about 3 years old (or even more) and I had been wondering why the calendar links cuts off from 2001 to 2003.

Task: List a year/month links from the present to the oldest published image sets.

Sounds easy, and I believe it is.

But when I checked on the existing script, this is what I got.

 $month_array = array("1"=>'January',"2"=>'February',"3"=>'March',"4"=>'April',"5"=>'May',"6"=>'June',"7"=>'July',"8"=>'August',"9"=>'September',"10"=>'October',"11"=>'November',"12"=>'December');
	$smarty->assign('y2day',$y2day); $y=0;
	for ($q=12; $q >= 1; $q--){
			$month = $month_array[$q];
			if( ($_GET[year] < '2000') && ($m2day ==$q ) ) $w_string = $week_string; else  $w_string ='';
			if( ($_GET[year] == '2007') && ($_GET[month] == $q) ) $w_string = $week_string;
 
		  if($m2day >= $q){
			$tmp_cal_month_now_array = array(
					'month' => $month,
					'string' => $w_string,
					'link' => $q
					); $date_month_now_array[$y++] =  $tmp_cal_month_now_array;	 }
		} //for
 
	 $smarty->assign('calendar_now_month',$date_month_now_array);
 
	$y=0;	$c_yeartoday = date("Y") -1; //echo $c_yeartoday;
	for($q=0; $q < = 5; $q++){
			$tmp =$c_yeartoday - $q; //echo $tmp;
 
		 $tmp_cal_date_array = array(
					  'year' => $tmp
 
				 );  $date_year_array[$y++] =  $tmp_cal_date_array;
	 } //for
 
	$smarty->assign('calendar_year',$date_year_array);
 
	    $y=0;
		for($q=12; $q >= 1; $q--)
		{
			$month = $month_array[$q];
			if( $_GET[month] == $q ) $w_string = $week_string; else $w_string='';
			$tmp_cal_month_array = array(
					'month' => $month,
					'string' => $w_string,
					'link' => $q
					); $date_month_array[$y++] =  $tmp_cal_month_array;
 
		}//for
	 $smarty->assign('calendar_month',$date_month_array);
 
	 $y=0;
		for($q=12; $q >= 10; $q--)
		{
			$month = $month_array[$q];
			if( ($_GET[year] == '2000') && ($_GET[month] == $q) ) $w_string = $week_string; else $w_string='';
			$tmp_cal_month_old_array = array(
					'month' => $month,
					'string' => $w_string,
					'link' => $q
					); $date_month_old_array[$y++] =  $tmp_cal_month_old_array;
 
		}//for
	 $smarty->assign('calendar_month_old',$date_month_old_array);

Continue reading

Variable, variables and lots of variables

List of variable names, oddly named and other stuff I’m glad I bumped into :D . Found them in project about 5 or 6 yrs old that needs modification and I believe optimization too, yes really. So why glad? Because I realized the way I named my variables isn’t that bad at all.

/**
 * This one is on top of a movie player page. Maybe 'he' wasn't so sure 
 * that the parameters passed on to this page is for a 'video'. Just making
 * sure for the nth time I guess.
 */
$videoistrue=0;
/**
 * I think 'he' hate lines. And this one is good, page title is saved 
 * in a session variable, so if another programmer writes a new page
 * and forgot to assign $_SESSION['pagetitle'] a new value for an FAQ
 * page, say faq.php, it'd show up <title>Show Series</title>
 */
session_start();$_SESSION['pagetitle'] = "Show Series";
/**
 * ... and it went on and on
 * $_SESSION['pagetitle'] = "Calendar";
 * session_start();$_SESSION['pagetitle'] = "Browse Model";
 *
 * Hmmmm, $ii as iteration variable, struggling with names? I often 
 * use $i, $n or $x, sometime $c, hehehe! I've seen $i for many. And 
 * have you noticed the class name 'smalleditnoline', can it be any 
 * longer? Yes! and if he had enough time, it may have look like 
 * 'smalleditnolinewitharialbold' or 'smalleditnolineFFFbgcolor333fgcolor'
 * crazy! This is CSS anyway, will surely post one for that, hahaha!
 */
for ($ii=1;$ii< =$totalpages;$ii++)
{
  if ($ii==$page) {
    echo "<font class='smallediton'>[$ii]";
  }
  else {
    echo "<a href='?sort=" . $_GET['sort'] . "&page=$ii' class='smalleditnoline'>[$ii]</a>";
  }
}
/**
 * more variables with default values, note that this is set just
 * before a loop starts
 */
$thisCat = "";
$firstrun = true;
$firstrow = 0;
$rowcount = 0;
/**
 * "NO QUOTES" for key names in square brackets :( 
 * if only my IDE could complain
 */
$GLOBALS[catadded] = false;
$GLOBALS[keywordadded] = false;
$GLOBALS[modeladded] = false;
/**
 * Consistent! Maybe 'He' doesn't want anybody to easily understand 
 * the script that's why no new lines after semi-colon at least.
 */
if ($seriesRow[seriesNo] < 10) $seriesNoTxt = "0".$seriesRow[seriesNo]; else $seriesNoTxt = $seriesRow[seriesNo];

Ok, enough of grumbling here. I have released my frustration for today, going back to work. But if I am to start a project or even a simple script, I follow formatting conventions, consistent quotes and escapes, logical variable names and useful comments with links and samples. It doesn't only make your code readable, it helps a lot for the next programmer to just add-in the stuff he needs to and keeping his sanity.

Breaking the silence

So, I haven’t really got time posting stuff here. Its because I’ve been trying to finish my new site after my day job. It is called swerteka.com, and I’m gonna give it as free ad posting service.

But it’s still a long way to go, and it will be loaded by the CMS script I’ve been working on.

Back to work

Over the next few days, I’ll be a “ning” developer :D , actually I started playing around the code about a month ago. But taken away off some other projects (feature integration) on PhpFox.

And as always, I started from the index.php going thru all the include files.

1
2
3
4
<?php
define('NF_APP_BASE',dirname(__FILE__));
require_once NF_APP_BASE . '/lib/index.php';
?>

Then look for anything that says “request” which is basically the point of interaction, how it receives it and handles it. After that, I look for the database object, its methods. Until the haunt kept on going.