Creating a jQueryUI Accordian

An accordion is the ideal choice for when you have to put a lot of information on the screen, but you do not have a lot of screen real estate available for that information.

Luckily for us we can create an accordion very easily using jQueryUI.

First thing you need to do is place all of your information inside your HTML like this:


<div id="accordion">
	<a href="#">First Section Title</a>
	<div>First Section's Content would be placed inside of this div</div>

	<a href="#">Second Section Title</a>
	<div>First Section's Content would be placed inside of this div</div>

	.... Repeat this for as many sections you want ....

</div>

Now that you have your HTML structure set up, now all you have to do is use some Javascript to tell jQuery UI to turn it into a the accordion.


<script type="text/javascript">
	jQuery(document).ready(function(){
		$('.accordion .head').click(function() {
			$(this).next().toggle('slow');
			return false;
		}).next().hide();
	});
</script>

Now you have a very easy way to put a large amount of information inside of a limited space.

Securing Passwords With PHP

Any website that stores users login information has to store their passwords. One thing that provides an extra layer of security for your users information is to do one way encryption on their passwords. You could in theory store their passwords as plain text, but if someone were able to get in to your database, they would have instant access to every users account.

One way encryption allows the password to be store in a secure manner. In fact it is so secure that you cannot decrypt it to recover the encrypted data. In order to authenticate the user you actually have to encrypt submitted data and compare the results that way.

One of the most widely used ways to accomplish one way encryption is to create an MD5 hash of the password. Lucky for us, it is so incredibly easy to create an MD5 hash of a string. You simply use the function md5(). See the example below:

<?

$encrypted_password = md5("secret");

?>

The value of $encrypted_password is the secured password.

PHP Function – str_replace

The str_replace function is used to replace part of a string with another string. The syntax of this PHP function is detailed below:

<?
mixed str_replace( $search, $replace, $subject, $count );
?>

$search – This can either be a string value or an array of strings. This will be the string or strings you want to be replaced.

$replace – This can either be a string or an array of strings. These will be the value that is replaced when one of the $search items are found.

$subject – The string that will be searched and have replacements placed inside of it.

$count (optional) – The maximum number of times a replace should occur.

There are a number of reasons why you would want to use this function. One of the ways that I use str_replace is when I need to remove line breaks from the a large string that you are working with. You would do that by doing the following:

<?
$input = str_replace( "\n", '', $input );
?>

You may have noticed that I used $input as the return variable and the string that is having the replace performed on it. This is a quirk that I use in order to not have to create extra variables and you cannot pass the $input to the function as reference.

PHP Boolean Type

The boolean data type in PHP is very easy to use (and why should it be difficult, it is either True or it is False).

The basic way to set a boolean value is to use the true and false keyword. Unlike other programming languages, true and false are case insensitive so you can either use true, True, TrUe, or any other variation to get the same effect.

For example:

<?
$yes = true;
$yes2 = True;

$no = false;
$no2 = False;
?>

These are all valid ways to set a variable to true or false.

PHP has some interesing ways of determining if a variable is true or false. Since PHP is a dynamically typed language, nearly any type can be evaulated as true or false. The way you can keep this straight in your head is if the variable’s value is at its default state, it will likely be evaulated as false.

To be more official, here is how PHP evaulates true or false from various values:

  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string “0″
  • an array with zero elements
  • an object with zero member variables (PHP 4 only)
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags

Text taken from PHP manual at: http://www.php.net/manual/en/language.types.boolean.php

Ternary Operator – Your Best Friend

If you have never used the Ternary Operator, or ? operator, your life is about to change. The ternary operator is used as a shorthand for if .. else structures that can really clean up your code. Some may want to classify this under a intermediate or advanced skill, but it is so useful that I think it should be included with the most basic tutorials.

Take this for example:

<?
if( $use_live_server ) {
     $url = "http://www.thelivesite.com"
} else {
     $url = "http://www.thedevsite.com";
}
?>

This block of code sets which URL we want to connect to based on if a variable called $use_live_server. While the block of code is perfectly valid, there is a much better way to do the exact same thing in a single line of code.

<?
$url = $use_live_server ? "http://www.thelivesite.com" : "http://www.thedevsite.com";
?>

That one line of code does the exact same thing and once you understand how this works. Here is how the ternary operator works:

<?
$variable = $expression_to_evaulate ? true : false
?>

You do not necessarily have to have a variable that is true or false. You can do any expression you need to.

Page 5 of 16« First...«34567»10...Last »