A coders playground.

Archive for the ‘PHP’ Category


Shorthand PHP IF/ELSE Statements = much shorter code!

Feb 14, 2008 Author: Jamie | Filed under: Code, General, PHP

You may or may not have heard of shorthand if/else statements, but they do exist, and trust me they make things SO much more ordered in your code when used in the right way.

This is the Shorthand version – below this, if you cannot work it out for yourself is a nice description.

    // Will print out ‘i am male’ if $myGender is ‘male’ and vice-versa.
    echo ($myGender == ‘male’ ? ‘i am male’ : ‘I am female’);

Description of how this actually works
We all (i would hope) know the normal way of writing if/else statements (here is a recap just incase)

$myVar = true;
if($myVar) {
    echo ‘My Var is TRUE!’;
} else {
    echo ‘My Var is FALSE’;
}

That sort of statement is great for in the body of your code, but when want a dynamic selector within your HTML code, it gets very messy.

EG:

if($myGender == ‘male’) {
    // Gender is MALE.
    $maleOpt = ’selected’;
    $femaleOpt = ;
} else {
    // Gender is FEMALE.
    $maleOpt = ;
    $femaleOpt = ’selected’;
}
<select name="gender"><
option value="female" <?= $femaleOpt; ?>>Female</option>
<option value="male" <?= $maleOpt; ?>>Male</option>
</select>
 

Now – that is alot to write and will make your HTML form look a total mess, this is where the shorthand comes in. It allows us to write the same thing inline

Look:

<select name="gender">
<option value="female" <?= ($myGender == ‘female’ ? ’selected’ : ”); ?>>Female</option>
<option value="male" <?= ($myGender == ‘male’ ? ’selected’ : ”); ?>>Male</option>
</select>
 

As you can see – there is a huge difference in the amount of code – and if Wordpress formatted it nicely – it would look loads better!

And the all important Explanation!

// This will echo selected to the screen if $myGender is male.
echo ($myGender = ‘male’ ? ‘ selected’ : );
 

So – $myGender is the variable we are checking, in this case we are checking to see if $myGender == ‘male’

The questionmark (?) seperates the ‘answers’.

The first ‘answer’ is what is set/displayed when the query is true, the second is if the query is false.

This is the equilivent to :

if($myGender == ‘male’) {
    echo ’selected’;
} else {
    echo ;
}

Hope that helps some of you out!

Captcha – in 5 lines of php :)

Sep 23, 2007 Author: Jamie | Filed under: Code, Dev, PHP

Yes you read it right…! 5 lines of code :)

I know its hard to believe but this has to be one of the simplest implementation of Captcha, mainly because i can’t find any easier ones.

So here goes, this is how it works:

At the top of the page with the form you want to Captcha protect add these 3 lines of code, this generates the Captcha and adds it all to a nice variable to print out later:

    require_once(‘captcha.class.php’);

    $captcha = new Captcha;

    $captchaImage = $captcha->create();

Then in your form where you want the Captcha, usually at the bottom, just above the submit button add this line, enclosed in php tags of course, this adds the captcha elements to the form:

echo $captchaImage;

So that is the form part done, now we just need to verify what the user has put in. For this i am going to assume that you are verifying atleast one part of the users input, so we need to add this line into it:

$captcha->verify($_POST[$captcha->captchaInputName]);

This would probably fit into your current checking like so:

if($someothervariable &amp;&amp; ($captcha->verify($_POST[$captcha-> captchaInputName])) {

Of course we have to add these 2 lines at the top of this processing page aswell (these are not included in the ‘5′ because they have already been added.

    require_once(‘captcha.class.php’);

    $captcha = new Captcha;

And thats it, the class handles everything else, well you have to put the files on your web-server, create a new directory to store the images and make it writeable, but thats a one-shot 10 second job!

Now for a screenshot:

Now for demo and the code:

Captcha DEMO

Captcha Formatted Source 

Captcha ZIP

AutoCompleter Tutorial – jQuery(Ajax)/PHP/MySQL

Sep 19, 2007 Author: Jamie | Filed under: CSS, Dev, General, Javascript, PHP, SQL

AutoCompleter Tutorial

As always, links to a demo and ZIP at the bottom, Enjoy!

I thought i would write this tutorial because most of the auto completer applications i have seen just dump the code into a zip and tell you how to use it rather than how and why it works, knowing about this enables you to customise it a lot more (this has been demonstrated with the other apps i have written here)!


(more…)

MySQL Calendar

Jun 27, 2007 Author: Jamie | Filed under: CSS, Code, Dev, General, Javascript, PHP

VERSION 1.1 RELEASED!

Changes: Added (by popular demand) Event deletion!
Ok… I have finally got this script working!

I have not done it like a tutorial because it is just too big, so here are a load of screen shots, There is a link to a fully functional demo and zip at the bottom!

A little setup information:

1. Copy the entire calendar into whatever directory you want.

2. Create your database and import the ‘databaseSQL.sql’ file into it, this will create the default data for running it.

3. Alter the ‘databaseConnection.php’ file to show your connection details.That should be it..

NOTE:By default the username and password are both ‘admin‘ (without the quotes), i suggest you change your password in the control panel.

If you have any problems i will be happy to help, just post your query as a comment to this page.

Screen Shots

Default View

(more…)

I’m going to assume that we all know, or have atleast heard of Prototype and script.aculo.us the two combine to form one powerful javascript library enabling developers to become designers (well, kind of).
– Example and code at the bottom –

I have created a ZIP file to get you up and running right away: Download it now!

I was showing a friend a few days ago something i made for a university module, Multimedia Applications Desgn (a mouthful i know). Basically we had to create a website promoting our group as a duo, thats all your getting about the project as its not really relevant and a tad embarrassing should anyone find the site!

There was a part of the site that my friend really liked and wanted to incorporate into his final year project… after sketching it out for him on paper i reconed that other people could benefit.
This has probably been splashed everywhere, but i havent seen anything that incorporates all the same characteristics on Digg.

So here is a quick, hopefully easy to understand, tutorial of how its done.

Abstract
We are trying to achieve loading content into a ‘div’ element without refreshing the page, as well as showing a cool loading window inplace of the content as it is loading.

(more…)

Image Manipulation using PHP and ImageMagicK

Feb 6, 2007 Author: Jamie | Filed under: Code, Dev, PHP

So there were a couple of problems with my last image processing functions (http://nodstrum.com/2006/12/09/image-manipulation-using-php/).

1. If you were trying to process a massive image > 1024px×768px it failed.
2. I only accounted for people processing Jpegs.
3. CPU spikes, because of the amount of processing that is being done with the image the CPU is really taking a hit.
4. The functions were really quite long.

These new function will use ImageMagicK, not all servers have ImageMagicK installed by default, check with your hosting provider.

(more…)

Calendar System – Easily using PHP & Script.aculo.us

Jan 29, 2007 Author: Jamie | Filed under: Code, Dev, Javascript, PHP

THIS HAS BEEN UPDATED, PLEASE SEE THIS POST: http://nodstrum.com/2007/06/27/mysql-calendar/

Hi,

Quite a few people seemed to like my previous tutorial Image Manipulation using PHP, and looking around many of the tutorial sites there are hardly any Calendar systems. Perfect, here is a simple, in calendar terms one…
You’ll see what i mean.

At the bottom of this tutorial there is a link to the ZIP and the test site.

There are 3 main parts, the Javascript, the HTML and the PHP script.
For this tutorial i am making it so you can change the date with the form and it will update the calendar without having to reload the entire page, AJAX style.

First we need the Script.aculo.us library, if you download my zip test file it is already included, but if not go to: http://script.aculo.us

(more…)

Image Manipulation using PHP

Dec 9, 2006 Author: Jamie | Filed under: Code, Dev, PHP

I have been trawling around the internet looking for some simple functions for do basic automated manipulation to some images im using.

if you want to skip straight to the examples just to prove im not bulls**ting skip to the bottom

Basically i wanted to have these functions:
1. thumbnail
2. resize (actual image)
3. reduce (image file size)
4. rotate (by degrees)

Most of the scripts i found couldnt be changed easily to make them do what i wanted…
So here are what i got working.. and a few instructions…

(more…)

Categories


Archives


Links


Statistics

View blog authority

Meta

Advertising