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