Posts Tagged ‘php basics’

[PHP Tutorial] Tabbed Ajax Content Pane

January 21st, 2010

Hello and welcome to my first tutorial!

Aims: By the end of this tutorial you will, hopefully, be able to create a simple tabbed content area.  This will allow users of your website to view a wide variety of content from an infinite number of tabs(within reason) all in one content space!

» Read more: [PHP Tutorial] Tabbed Ajax Content Pane

[PHP Tutorial] Check For Empty Values

December 11th, 2009

In this tutorial we will create a function to check if a field is empty in either a string or an array. So this could be used to do a simple check on if a form field is left blank. I will show you an example on how to use it to check if a form field is empty.

So heres the function

function empty_check($var){ // Start Function
	if(is_array($var)){ // Determine if $var is an array or not
		foreach($var as $key => $value){ // If it is an array go through the array and assign to $key and $value
			$value = trim($value); // Remove any whitespace from the start and end of the value
			if(empty($value)){ // Check if its empty or not
				$empty[] = $key; // If its empty put the name of the field thats empty in $empty array.
			}
		}
	}else{ // If it isnt in an array
		$var = trim($var); // Remove any whitespace from the start and end of the value
		if(empty($var)){ // Check if its empty or not
			$empty[] = $var; // If its empty put the value into the $empty array.
		}
	}
	return $empty; // Return $empty.
}

So now we have the function I will show you an example on how you can use this.

<?php

if($_POST['submit']){
	$empty = empty_check($_POST);
	if($empty){
		echo "You have left the following fields empty<br />";
		foreach($empty as $value){
			echo $value . ", ";
		}
	}else{
		echo "All fields are filled in.";
	}
}

?>
<form method="post" action="">
	<input type="text" name="first_name" />
	<input type="text" name="last_name" />
	<input type="submit" value="Submit Button" name="submit" />
</form>

[PHP Tutorial] How to use Explode

November 27th, 2009

The explode function is used to grab certain information out of a file or string. This tutorial will show you how to explode out of both a file and a string.

Explode Structure

explode  ( string $delimiter  , string $string  [, int $limit  ] )

Explode From A String

<?php

$string = "Peter;Mark;Rupert;Jack;Harold;Daniel"; // String containing names seperated by ;
$explode = explode(";",$string); // Delimiter is the ; which is seperating the names and then the $string.

print_r($explode); // Prints out the array $explode.

echo $explode[1]." is friends with ". $explode[5]; // Mark is friends with Daniel

?>

 » Read more: [PHP Tutorial] How to use Explode

[PHP Tutorial] Check what people searched in search engines for your site

November 15th, 2009

Find out what people have searched on search engines to access your website. A similar feature can be found on google.com/webmasters but that’s is a 3′rd party script where as this can be hosted directly on your site.

So first we need to choose what search engines we want recorded. To do this I will do the examples on google and yahoo. First go to google.com and search for PK-Tuts then look into the address bar it will look something like this.
http://www.google.co.uk/search?hl=en&source=hp&q=PK-Tuts&btnG=Google+Search&meta=&aq=f&oq=

» Read more: [PHP Tutorial] Check what people searched in search engines for your site

[PHP Tutorial] Create A Captcha Form

November 9th, 2009

This tutorial will teach you how to protect your form’s from being submitted by bots. We will use a simple way, by creating a image using php with some randomly generated numbers then store it as a session and when the form is submitted we will check if the field in the form matches up with the numbers generated.
» Read more: [PHP Tutorial] Create A Captcha Form

[PHP Tutorial] If/Else/Else If Beginners Tutorial

November 1st, 2009

After contact from some visitors starting to learn PHP. I am going to be writing a few tutorials on the basics of PHP. In this tutorial we will cover how to use the If Statements.

If statements are used to check if something matches something else. The syntax for a simple If statement is as

» Read more: [PHP Tutorial] If/Else/Else If Beginners Tutorial

[PHP Tutorial] Simple object orientated template system

October 27th, 2009

I’ve always been a fan of the wordpress template system, Its easy to use and very powerful. Expression Engine has a similar template system in place and today we will be writing a class which will host many of the same features and will give you an extremely flexible and powerful template engine to work with.

Structuring our class

  1. class template
  2. {
  3. private $globals = array();
  4. public function __construct(){}
  5. public function global_vars( $key, $value = null )
  6. {
  7. }
  8. public function load( $tpl, $arr = array(), $return = false )
  9. {
  10. }
  11. }

» Read more: [PHP Tutorial] Simple object orientated template system

[PHP Tutorial] Store An Array

October 20th, 2009

In this tutorial we will learn how to encrypt an array with base64 and then serialize it so we are able to store it in a database without loosing the integrity or values.

So lets start by creating 2 functions the first of which will encrypt and serialize the array.

function store_encrypt($array)
{ // Start function with function name store_encrypt.
    if (!is_array($array))
    { // Check if $array is an array or not if it isnt return false.
        return false; // return false.
    }
    else
    {
        foreach ($array as $key => $value)
        { // Cycle through the array.
            $key = base64_encode($key); // Encode the key with base 64.
            $value = base64_encode($value); // Encode the value with base 64.

            $array[$key] = $value; // Put the key and value back into the array
        }
        return serialize($array); // Use the PHP serialize function to save the array, type and values and return the function.
    }
}

 » Read more: [PHP Tutorial]  Store An Array

[PHP Tutorial] PHP File Downloads with Speed Limit

October 14th, 2009

PHP can be used to securely control access to file downloads. This tutorial will show how you can send file through a PHP script and limit the download rate. The function we will write accepts the path to the file to send and optionally a rate in kB/s to limit the transfer speed. The function should also be able to handle range headers from clients that allow stopping and resuming downloads.

» Read more: [PHP Tutorial] PHP File Downloads with Speed Limit

[PHP Tutorial] Saving a PHP File as a Word Document

October 10th, 2009

So you are looking for a way to save a PHP page as a word doc programatically? Then you have come to the right place.

When I had to overcome this problem my main requirement was that I could not open word on the server.

Searching around the net produced countless pages with the same responses of using COM or using some third party library’s and software. However as with any project if it is possible I, as well as many other developers, try to avoid third party products.

» Read more: [PHP Tutorial] Saving a PHP File as a Word Document