Archive

You are currently browsing the archives for the PHP category.

May

6

My First Drupal Module

By mike@mike-miles.com

Recently while working on a drupal website for a client, I needed to build in some custom functionality.  For drupal this is’nt an issue, basically anything you need to do has been done before and you can find a module in the module directory.

But after searching I was unable to find a module that did what I wanted, so I had to develope one (Which for a nerd like me was really fun).  It took me a day or two to build, and refine but I built a module that I was happy with.

Read more »

Mar

10

Working hard

By mike@mike-miles.com

Sorry for the lack of updates, I’ve been quite busy at my job (doing some major web development stuff). I’m going to try to post later this week about a PHP calendar/date class I’ve been building in my freetime (initial tests are showing that for some instances it’ll be up to 4x faster then the ‘date()’ function.) so hopfully you’ll see that later in the week

Feb

23

Itunes like progress/capcity bar

By mike@mike-miles.com

Back in December I was working on a project that needed to show progress bars.  They progress bars had to show the progress of multiple items, and it was explained to me that they should resemble the percentage bars in iTunes that show how full your ipod / iphone is, like so;

Not a problem I thought to myself, all I needed was a few images and the ever useful PHP GD library .  If you’ve never used the PHP GD library before, a brief overview:  It’s a graphics library that allows you to create dynamic graphics.  (Told you it was brief).

Of course the great thing about PHP is that it is open source, so anything you need todo has already practaclly been done. After googling I found this percentage bar code created by netlobo.com, as well as this progress/percentage bar WebAppers (theirs was in javascript but it gave me a starting point). Both of these were close to what I wanted to do, I just needed to tweak and combine them.

I should note, that since I did use code from both those websites (which are covered under the Creative Commons license and GNU General Public License) this code covers the same licensing.

The Process

My thought process for creating this was pretty straight forward from the get-go.

  1. Get percents of the items that will be on the bar
  2. Create a “bucket” image
  3. for each percent, fill part of the bucket with a color
  4. Output the whole bar.

And that’s pretty much the route I followed.  It was pretty simple actually.

Sorry to say, this code has been removed for legal reasons

but, please check out the sources I used when developing my code (they are mentioned above)

Feb

12

When to be OOP

By mike@mike-miles.com

In case you don’t know (or didnt realize) web languages (like php, javascript, ect…) have evolved a long way from their first verisons (duh, mike).  With that eveloution they have developed into languages just as complex as standard application languages (java, c++, ect). With the complexity comes the wonderful availability for OOP programming.

The thing is most web programmers are not OOP programmers, the standard for the web has been to be a functional programmer (just writing functions) so a straight up web developer might not be very familiar with OOP programming. OOP programming (creating objects and classes) can be a great resource when writing complex applications.  This post is not about how to create an OOP web application (though, writing one might be handy).  I wanted to write a post that describes my thought process of when to be OOP, because there are times when it’ll benefit you as a developer and times when it’s just unnecessary overhead.

Rule of thumb – functions

Now we all know that functions are a nessecity, reduces redundent code, automates things, makes your life easier (Side note:  I had a few friends in one CS course during college who went the whole semester never writing functions just straight up code. The class was a bioinformatics course using perl so I have no idea why they wanted to do this.) I have a general rule of thumb before writing a function.  For me to place code into a function it’s got to meet at least one of these requirments;

  1. Will I end up writing this code more then twice
  2. Is this code going to be utterly complex, and long (will break into multiple functions)
  3. Can I use this anywhere else

If the code matches one of those then I’ll put it into a function.  If not I might even still put it in a function.  The key is reusability.

Rule of thumb – classes/objects

Now my rule of thumb for creating a class and doing some OOP is almost like my rule of thumb for creating functions, except it deals with functions not just code;

  1. Will I need to reuse these functions multiple times? (and together)
  2. Would it benefit to prevent user from accessing these functions (using OOP ‘private’)
  3. Could I use these functions together in the future

So if a group of functions meets any of those requirements, I’ll rewrite them to be a class.  For example, in PHP I have a general Email class that I can put into any project that needs e-mail capabilities.  and to use it I just need to include it and invoke the object.

It makes it simple for me to add e-mail to any project, and reduces the code I would have to rewrite.  This increases my time for further development.

OOP in web development is also a great plus because you can keep classes and objects independent from each project, allowing you to build up a library that you can use in any project (again reusability!).  this reduces the time you have to take to rebuild something you’ve done in a previous project and gives you more time to produce richer, fuller web applications.

Feb

5

AJAX file upload

By mike@mike-miles.com

Recently for a project I needed to build a custom photo gallery web application, which would allow the administrators and site users to both uploaded photos. The thing about file uploads is that to the user (which in my case was administrators and site visitors) is that it can be very boring to the user, and not very elegant. Since users were providing additional information to the photo, I was saving everything to a database including the filename of the photo uploaded (for each upload I would generate a random name). I wanted the user to be able to upload their photo, preview it, then finish completing the rest of the form. Normally to do this would require at least 3  steps;

  1. Have the user upload their file, and submit the page.
  2. Reload the page, save the file then display a second form for the user to provide details
  3. save the details to the database.

Now this fine, a tried and true way to do it, but boring to the user because before they can finish filling out the form they would have to wait for the file upload to finish (which could take awhile depending on file size and connection). We dont want the user to have to sit there for a long time waiting for the upload to complete (idle users are BAD, for a number of reasons, example it  gives them time to say ‘screw it’ and navigate away from our site).  It would be much better if the user had something to do while their photo was uploading, keeping them busy and happy.

So what I wanted to do was have the user select a photo,  which would then (using AJAX) be sent to another page to upload, and return the file name.  While this was happening the user would see a loading icon, and be able to complete the rest of the form.  Once the upload page returned the filename, I would display the form’s submit button.

At first I was stuck on how to exactly accomplish this effect (I knew I wanted to use AJAX).  After doing some googling I came across this solution.

Basically a full blown AJAX file upload is not possible, but we can get the feel and effect using a hidden Iframe.

Sorry to say I’ve had to remove this code for legal reasons.
but feel free to checkout the source I used to create the code to draft your own solution.

Feb

4

Variable function calls

By mike@mike-miles.com

When using AJAX or sending vaiables to a PHP page to run a function there is a nice peice of code I like to put at the top of the PHP page.  It is a peice of code that I use to run a function by passing its name through a variable. This allows you to GET or POST to a page many times and preform multiple functions. Here is the code and I’ll explain a bit more of a few ways that you can use it;

1
2
3
4
5
6
7
8
9
10
11
<?PHP 
	//if passed the variable 'function' through GET or POST 
	if($function = $_REQUEST['function']){ 
		//if function exists, run function 
		if(function_exitst($function)) $function(); 
		//else show error 
		else echo 'Error: function '',$function,'()' does not exist.'; 
		//Exit script
		exit; 
	} 
?>

So to reiterate this is what this function does.

If the page is passed the varaible ‘function’ (either through GET or POST, hence the $_REQUEST) then it knows to run a function on the page.

It’ll then set the passed variable to the $function variable, and test to see if the function exists (by using PHP’s function_exists() function)

If the function does exists, the code then calls if ($function();), else it’ll show an error.

for example you could use this code like so;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?PHP 
	//if passed the variable 'function' through GET or POST 
	if($function = $_REQUEST['function']){ 
		if(function_exitst($function)) $function(); 
		else echo 'Error: function '',$function,'()' does not exist.'; 
		exit; 
	} 
 
	function submitForm(){ 
		$value = $_POST['field1']; 
		echo 'You inputed ',$value,'.'; 
	} 
?> 
<html>  
	<head>  
		<title>Sample</title>  
	</head>  
	<body> 
		<form action="<?= $_SERVER['PHP_SELF']; ?>?function='submitForm'" method="POST">  
			<input name="field1" type="text" />  
			<input type="submit" value="submit" />  
		</form>  
	</body> 
</html>

So the form will submit to itself, and run the function ‘submitForm’ when it does so.  Of course this is a very simple example.

you can download the complete version of this code, on my code page