There might be a moral to this

Filed under: PHP, Programming — Tags: , — Eike @ January 18, 2008 12:21 am

I was considering to submit this to some site like http://thedailywtf.com , but then lousy PHP Code is too common to be newsworthy. Still….

I occasionally help a friend with an online app that was custom-made for her some four years ago… only “custom made” in that case means that the programmer followed some strange customs when he made it. The app is written mostly in procedurale PHP code – the programmer was at least dimly aware that there is such a thing as “functions” in PHP and you will actuall find some of them in the app, provided you do not look in the file “globalfunctions.inc”, which contains more procedural code (but is included in every file so at least it’s global) . Actually I even spotted an object – despite the fact that PHP 4 had been available for some time when the thing had been written it was built around a template class written in PHP3 (fasttemplate). Yet I feel that the programmer somehow misunderstood the purpose of templates, because he has a template for every single file in the app – obviously he couldn’t work out how to highlight a menu item depending on the page content, so he copied and pasted the whole HTML code for every single menu state. Actually there are two templates for every page, because this is a multilingual app (german-english) and his way to localize the application was to copy the german files and replace the copy text with the english equivalent. This is an example of not-so-great software engineering.

But the app was also designed to work with form data, and some parts of it required the user to enter a date by way of several select fields (year,month,day).

As everyone knows a month has 31 days. Some nitpicker might point to months like, for example, November or June or September or even April, and some people might by dimly aware that February might have 28 or 29 days. But hey, what’s day more or less, it’s not like people pay money for this (except they do. Bugger).

MySqls Datetime format requires two digits for the day of the month, but somehow the people who inventend arithmetics failed to comply with that requirement: numbers smaller than 10 do only have one figure. To build the options for the select field the programmers used a for loop (well, actually the code didn’t built the options, but should have) , only to correct the fundamental error in modern math he actually used two loops – from one to nine and 10 two 31:

	for($i=1;$i<=9;$i++){
		if($_SESSION['gtag_tag'] == "0$i"){
			$tpl->assign("{selectTag0$i}", "selected");
		}else{
			$tpl->assign("{selectTag0$i}", "");
		}
	}
	for($i=10;$i<=31;$i++){
		if($_SESSION['gtag_tag'] == $i){
			$tpl->assign("{selectTag$i}", "selected");
		}else{
			$tpl->assign("{selectTag$i}", "");
		}
	}

Repeat about 50 times throughout the code. If you can’t quite figure out what happens to the newly assigned {selectTag}s, here’s the template code for the select field:

<select name="select4">
              <option>---</option>
              <option {selectTag01}>01</option>
              <option {selectTag02}>02</option>
              <option {selectTag03}>03</option>
              <option {selectTag04}>04</option>
              <option {selectTag05}>05</option>
              <option {selectTag06}>06</option>
              <option {selectTag07}>07</option>
              <option {selectTag08}>08</option>
              <option {selectTag09}>09</option>
              <option {selectTag10}>10</option>
              <option {selectTag11}>11</option>
              <option {selectTag12}>12</option>
              <option {selectTag13}>13</option>
              <option {selectTag14}>14</option>
              <option {selectTag15}>15</option>
              <option {selectTag16}>16</option>
              <option {selectTag17}>17</option>
              <option {selectTag18}>18</option>
              <option {selectTag19}>19</option>
              <option {selectTag20}>20</option>
              <option {selectTag21}>21</option>
              <option {selectTag22}>22</option>
              <option {selectTag23}>23</option>
              <option {selectTag24}>24</option>
              <option {selectTag25}>25</option>
              <option {selectTag26}>26</option>
              <option {selectTag27}>27</option>
              <option {selectTag28}>28</option>
              <option {selectTag29}>29</option>
              <option {selectTag30}>30</option>
              <option {selectTag31}>31</option>
            </select>

The application was obviously written by someone who had learned PHP with Version 3 and then decided to go on with what he knew instead to upgrade his knowledge (because then he would have, for a start, discovered that PHP 4.0.1 introduced str_pad. Not that it would have been a big improvement). So I advise you to live and learn, lest some of your more embarassing errors might be discussed on a public website.

    2 Comments »

    1. Why you fucking moronic retard, simply use the fucking link that fucking says “content item module” in the fucking freaking sidebar.
      I hope you will learn something. Reading might be a start.

      Okay, cursing back is not funny no matter how coarse the comment was, but really… half of the site is about the content item module, I don’t know how anybody could miss it. And even then one could just, you know, ask.

      Comment by Eike — January 22, 2008 @ January 22, 2008 7:04 pm

    2. You are so good in writing any thing, but not a single ling to the download page of the module Content Item Module. When you publish on joomla site that you have made a module and it is good and perfect, and beside that it is free, you have to also make it easy for people to download it easily and quickly and not waste hours on your home page reading all this fucking bull shit, and finally after these hourse leaving your site with my cock in my hand. I hope you will learn some thing.

      Comment by Mizhad Al-Mulla — January 22, 2008 @ January 22, 2008 7:00 pm

    RSS feed for comments on this post. TrackBack URL

    Leave a comment