Adding hints

Sometimes it is not totally obvious what you are looking for the user to enter into a text box.

Take this one here:

It is not obvious what we are looking for in this form, By email might is probably an email address. In person is probably an address. Online? That could also be an email address, a skype name, a msn messenger account name, a website…. you get the idea. Other, well, that could be anything, but what is that second box for? To be fair, the form has values in it telling what should be done, I removed those for the sake of the example. That is indeed one way of doing it, but that means that someone has to select all of the text and delete it when they get to that box. I don’t know about you, but I find it a little annoying when I have to do that… So, I started looking for a solution, and I found one that uses jQuery (my javascript library of choice). I could have written it myself of course, its not that hard really:

if(form.input is empty) {
   form.intpu.value = form.input.text
}
if(form.input is clicked){
   form.input.value = ""
}
if(form is unclicked and value = ""){
   form.input.value = form.input.text
}
if(when form is submitted and form.input.value == form.input.text){
   form.input.value == ""
}

I decided to modify that slightly, so that if the form was filled with the value found in text it would be lighter than normal, so:

if(form.input is empty) {
   form.intput.value = form.input.text
   form.css("color", "#999")
}
if(form.input is clicked){
   form.input.value = ""
   form.css("color", "#000")
}
if(form is unclicked and value = ""){
   form.input.value = form.input.text
   form.css("color", "#999")
}
if(when form is submitted and form.input.value == form.input.text){
   form.input.value == ""
}

Here is the final result:

Oh ya, a bonus is that when they hover over it and have javascript turned off it will show the hint as well, and it is screen reader friendly! Accessability is good!

If anyone wants the code I can post it.

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…

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)