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…

flattr this!

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.

flattr this!

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:

 

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.

flattr this!

JQuery and WordPress

Update: New Post – A better way to use jQuery in WordPress Themes

I was making a new template for my site. I was originally going to make it all curvy and web 2.0… but I figured, since I am not the best at the visual design of things I went with this. I like it.

While I was making it, I was looking at using jQuery to do the curvy web 2.0 things. Now, if you have not used jQuery before, you should check it out, it is simple and there are a million plugins for it. There is one for corners (which I was going to use), there is a lightbox one, so on and so forth. It is also really small, the version included with the latest wordpress install is 1.2.3 which is about 30 KB. The latest version is 1.2.6.

Since I was not going to make it all curvy I decided to add an easter egg to my blog using jQuery.

At first I kept getting the error:

Error: $ is not a function
Source File: http://localhost/blog/
Line: 22

That is apparently because WordPress uses a lot of different javascript libraries and apparently they have done a bit of rewritting. instead of using the $(“”) to start your jQuery javascript, you use jQuery(“”). This took a bit of searching and testing to find out, but in the end I got that figured out. It was then on to the code to make the easter egg work. Here it is:

jQuery(document).ready(function(){
 jQuery("h4").click(function(){
  jQuery(this).fadeOut("fast", function() {
      jQuery(this).html("π").fadeIn("fast");
      });
  });
});

Step by step:
1 -> Load this when the page loads
2 -> Whenever a h4 is clicked (there is only one here so its fine to do it this way)
3 -> Fade out what we clicked on fast
4 -> fade in pi

Nice and easy (and it looks cool too). This could be put to much more practical uses like translations or possibly a slide show of images (fadeIn pause fadeOut, fadeIn next image). For now it will just be a small easter egg that I added to my site.

Oh, and apparently the google syntax highliter has decided not to work in this version of wordpress so, I am now using wp-syntax instead.

Oh ya… and you have to have these lines in your code before you call any of the jQuery functions.

<script src="./wp-includes/js/jquery/jquery.js" type="text/javascript"></script>
<script src="./wp-includes/js/jquery/jquery.corner.js" type="text/javascript"><!--mce:1--></script>

(PI is an inside-ish joke by the way)

Edit: You do not need the jquery.corner.js part… unless you want fancy corners.. you would also have to download it too… if not you will get an error.

Ok, I think there is a bug somewhere, if you click through using the page links, and do a search the pi thing will work. If you rely on the cache (go to the same page by clicking on the title link of a post/page) it does not work at all. (At least in FF3)

flattr this!

Google Chart Helper 0.1

Jobba the Site is coming along great and is getting closer and closer to being done.

As I am sure you know, Google recently released a chart api, but it is a bit of a pain to generate all of the numbers and put them in the image uri so that Google can generate the charts for you. That is why I created the google_chart_helper for Code Igniter. So far it is pretty beta, and only does one chart type, the pie chart, but I figured I would put it up here to see if anyone has a better way of doing it (or some cool tips that I don’t know about).

Here is how it works:

$chart = new google_chart();
$chart_data = array(2,3,4);
$chart_labels = array(two, three, four);
$width = 500;
$threedee = false;
$colourful = true;
$chart->pie_chart($chart_data, $chart_labels,
$width, $threedee, $colourful);

and the result will be something like:

This was generated a while ago, and I saved it, but generally, Google will create a new chart for you every time you ask for one.

Here is the php file:

my_google_chart_helper

Have fun!

Edit
Here is the file as a txt so you can look at it (or what ever)

flattr this!