Spartan Coding

I thought it would probably be rude to post this on Coding Horror, as it is rediculously long… but here is my attempt at Spartan coding.

So, the first function… changes a users information in the database:

	function update_User($user,$id) {
 
 
		if (isset($user['deleteprofile']))
		{
			//array(3, 'live', 'Rick'
			$this->db->where('user_id', $id);
			$this->db->delete('users');
 
			$this->db->where('id', $id);
			$this->db->delete('ez_users');
 
			$this->db->where('user_id', $id);
			$this->db->delete('ez_auth');
 
			$this->db->where('user_id', $id);
			$this->db->delete('ez_access_keys');
 
			$this->db->where('user_id', $id);
			$this->db->delete('faves');
 
 
			return true;
		}
 
 
		$user['first_name']= isset ($user['first_name'])? $user['first_name']: "";
		$user['last_name']= isset ($user['last_name'])? $user['last_name']: "";
		$user['academic_status']= isset ($user['academic_status'])? $user['academic_status']: "";
		$user['college']= isset ($user['college'])? $user['college']: "";
		$user['department']= isset ($user['department'])? $user['department']: "";
 
		$data = array(
               'first_name' => $user['first_name'],
               'last_name' => $user['last_name'],
                'academic_status' => $user['academic_status'],
               'college' => $user['college'],
				'department' => $user['department'],
		 		'user_id' => $id
            );
 
		$this->db->where('user_id', $id);
 
		if (!$this->db->update('users', $data)) {
			return false;
		}
 
 
		if (!isset($user['access']))
			return true;
 
		$data = array(
               'access' => $user['access'],
				'user_id' => $id
            );
	//	print_r($data);
		$this->db->where('user_id', $id);
 
		if (!$this->db->update('ez_access_keys', $data)) {
 
			//incorrect parameters. maybe one of your form field names is different from the database fields
			//in the 'users' table?
			//common mistake: using a name for the submit button. don't do that!
			//also: you have to have a hidden field in the form called $user_id with
			//the value of the ID of the user you are playing with.
			return false;
		}
 
		return true;
	}

It became this:

function updateProfile($data, $id){
		$this->db->where('user_id', $id);
		if(!$this->db->update('users', $data)){
			return false;
		}
		return true;
	}

This is possible because arrays can be passed to the function. All of the functionality that was needed in the function is still there, it is perfectly readable, and it does the same tests… I guess it could become this:

function updateProfile($data, $id){
		$this->db->where('user_id', $id);
		if($this->db->update('users', $data)){
			return true;
		}
		return false;
	}

Which is probably better as it will return false automagically if there is a problem…. in fact, I think I will be changing it to that. Yes it does hide a lot of the functionality, but… it allows the functionality to be huge… I can edit the whole table or just one column with this function, unlike the other function where the author apparently wanted to create a function for each of the options that were needed…. sigh… The unfortunate thing is that I have not been able to get rid of this code completely yet because I have not yet looked to find out where else it is used… And yes, I know it would be more complex if it were not for the fact that I was using the code igniter framework, but… not as complex as the one that preceeded it.

Wow, the code for the syntax highlighter needs to be fixed…

<pre lang="php">

works, but

<pre lang = "php">

does not…

Komodo Edit on Ubuntu (Linux)

I use Komodo Edit all the time on Windows (XP and Vista) but up till now I have not used it in any Linux environment. I decided it was time to take the plunge and try things out. First I had to download the tar file from the activestate website (here), that was easy, the one thing that I was wary about was this:

AS package (libcpp6)

I had never heard of that, but as it turns out it is an [A]ctive [S]tate package. So, not a problem. When I got it there was an install .html. It had all of the instructions for all of the operating systems. As it turns out that was a bit overkill considering this was the Linux version not a Windows one or a mac one…

The only thing that I needed from the help file was the information that you had to install these packages (I use apt-get to do it, you might use something else):


scim scim-gtk2-immodule scim-qtimm scim-tables-additional

You might also need to install these ones if they are not already in your Linux of choice:

glibc libjpeg libstdc++

You will also need to have perl installed.

Finally once those packages are all installed you can run the install file

./install.sh

And then once that is done (you get to chose the path that it installs to (I left it as default because I am the only user of this machine), add komodo to your path by entering:

export PATH="/home/[username]/Komodo-Edit-4/bin:$PATH”

in the terminal (well, that is the quick way, you would probably have to type that in every time you wanted to use it… a better way is to add that line to the end of your ~/.bashrc file).

You could also create a symbolic link to it:

ln -s "/home/username/Komodo-Edit-4/bin/komodo" /usr/local/bin/komodo

Finally, you can just type in komodo to run it, or create a link in your menu (or on the desktop) to run it.

Once I get some money, I will probably be buying the full version (Komodo IDE) which has more features in it. Until then, I am more than happy with Komodo Edit 4.

If you have any problems installing it, please refer to the install file that they include as I have skipped some of the stuff that did not have any relevance to me.

The shortest, truest most unpopular (but free) dieting book ever:

Here it is, my first book, guaranteed to never be popular, no one will spend a cent on it but it’s the shortest, truest guide to dieting and healthy living ever, guaranteed to increase life span, increase libido, eliminate most nagging health problems and prevent serious disease of all kinds:

1) Eat less, mostly plants and other fresh foods in variety.
2) Move around more.
3) Repeat as necessary.

Anyone who tells you there is more to it than that is full of crap and trying to sell you something.

John C.

From here.

The worlds most secure login screen ever!

Go ahead... try and stump this one

[sarcasm]Not only is their customer support top notch, their login screen is super secure. [/sarcasm] It just begs to be attacked.

http://acer.custhelp.com/cgi-bin/acer.cfg/php/enduser/acct_login.php?p_userid=[emailaddress]&p_next_page=myq_upd.php&p_iid=[6 numbers]&p_created=[10 numbers]

Ya… its all done as a query string…

Dynamic Images with PHP

You may or may not have noticed that my headerchanges slightly everytime you load the page. If you want to check it out, go ahead I’ll wait.

Ok, so now that you have checked it out, I will show you how it is done using php (I’m sure that some of you are curious).

Here is the code:

Most of it is self explanatory, but some of the interesting lines are 9, where we create the image with the set width and height and store it in a variable called $im, then on 11 we fill the image with an rgb value (0,0,0) in this case. Line 14 then creates a variable using teh ImageColorAllocate function, which was previously used to fill the background. This variable is later used to colour the text that is added to the image. We also have to tell the browser what kind of image it is (line 16), this unfortunately means that it is more difficult to add to a webpage (without it spitting out header already sent errors). To get around those errors, you display the image using:

<img src="imagegenerator.php" alt="My image" width="760" height="150" />

Finally we built the rest of the image on line 25 and display it on 27 using the ImagePNG function (because we told the browser it was a png on line 16). Nice and simple.