Posts Tagged ‘PHP’

Stuck In The Muck

Friday, March 25th, 2011

Tags: , , , ,
Posted in portfolio | Comments Off

Southland Honda

Friday, March 25th, 2011

Tags: , , ,
Posted in portfolio | Comments Off

Creating an Advanced Contact Form With Kohana – Part 1

Wednesday, November 25th, 2009

If you’ve never heard of the Kohana PHP Framework, then today is your lucky day. Kohana is a PHP 5 framework originally based off CodeIgniter, but with many improvements. We’ll start off slow as an introduction to getting started with Kohana, and then we will really dive into to our contact form, adding all sorts of features. Enough explaining, let’s do this!

(more…)

Tags: ,
Posted in PHP, Web Programming | 14 Comments »

Using the PHP GD Graphic Library

Thursday, October 30th, 2008

Today, I will walk you through the process of using the GD Library.  This library is installed on most PHP servers, and works really well when you want to allow someone to upload an image of any size, and then resize it into a proper thumbnail.

First of lets create a simple form that allows us to upload our image file in a file called image.php

<h1>Upload Image</h1>
<p>
     Below will be a preview of our image once uploaded:
</p>
<img src="images/thumb.jpg" alt="Preview Will Be Shown Here" />
<form action="image.php" enctype="multipart/form-data" method="post">
    <label for="image">Image</label>
        <input id="image" name="image" type="file" />
        <input name="submit" type="submit" value="Upload!" />
</form>

Here we have setup that thumb.jpg will be shown. Of course we have not yet uploaded an image so nothing will show up here yet. We have set the action to be the same file, so it will just keep posting back the information to itself. Another important attribute of the form that we need to assign is the enctype. If this is not here, it will not send our image across properly.

We now need to do some fancy PHP when the above form is submitted. Add this above the form:

< ?php
if( isset($_POST['submit']) )
{
    // gets the variable needed from our type=file field with a name of image
    $image = $_FILES['image']['tmp_name'];

    // makes sure we have browsed to an image and not left it blank
    if( !empt y($image) )
    {
        // put the contents of the image into a php variable
        $src = imagecreatefromjpeg($image);

        // get the original images width and height
        list($width, $height) = getimagesize($image);

        // calculate the new width and height that we want our image to be
        // height is calculated to keep the same ratio with our new width
        $newwidth = 100;
        $newheight = ($height/$width)*$newwidth;

        // create a holder for our new image, and copy the original with new dimensions
        $tmp = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

        // lastly output the new image to our directory structure
        imagejpeg($tmp,'images/thumb.jpg',100);
    }
}
?>

One last thing to do is make sure your “images” directory is writable. Now open up your image.php file and try it out. It should now allow you to resize any image into a great looking thumbnail!

Download Tutorial Files

Tags: ,
Posted in Web Programming | 2 Comments »

PHP CodeIgniter Validation

Friday, October 17th, 2008

Most web developers shudder when the task of validation comes up.  This is if you spend the time to validate your user input.  I remember back in university when our professor (Chris Brown) made us pseudo code out what we were trying to accomplish with our application.  You know what task came up multiple times during our discussions?  Well you guessed it, Validation.  Personally validation is not one of my favorite parts of making a website.  It can be tedious, and a lot of the time developers skip it.

Well I encourage you not to skip it and will show you what kind of functionality CodeIgniter comes with.  I assume you have CodeIgniter installed on your server.  Please visit the installation guide if you haven’t.  As well I have created a database and configured CodeIgniter to connect to it.  I have one table called categories with two fields of categoryID, and title.  Put some dummy information for the purposes of this demo.

Next, lets create a simple controller called “Form” in our system/application/controllers directory.

< ?php
class Form extends Controller
{
    function __construct()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
        $this->load->library('validation');
        $this->load->database();
    }
    function index()
    {
        $query = $this->db->get('categories');
        foreach ($query->result() as $category)
        {
            $data['categories'][] = $category->title;
        }
        $this->load->view('form', $data);
    }
}
?>

In the above code we have loaded some helpers, libraries, and connected to our database.  Our validation library is where our validation code comes from.   In our index function we get all our categories from the database.

The next step is to create a couple views.  One containing the form we wish to validate and another that will be shown when our form is submitted successfully.  Create the following two files in the system/application/views directory.

form.php

<html>
    <head>
        <title>CodeIgniter Validation</title>
    </head>
    <body>
        < ?php echo form_open(form/index'); ?>
            <h5>Title</h5>
            <input type="text" name="title" value="" size="50" />
            <h5>Email</h5>
            <input type="text" name="email" value="" size="50" />
            <h5>Category</h5>
            < ?php echo form_dropdown('categoryID', $categories); ?>
            <h5>Description</h5>
            <textarea name="description" cols="40" rows="5"></textarea>
            <div><input type="submit" value="Submit" /></div>
    </body>
</html>

success.php

<html>
    <head>
        <title>Success - CodeIgniter Validation</title>
    </head>
    <body>
        <h3>Your form was successfully submitted!</h3>
        <p>< ?php echo anchor('form/index', 'Try it again!'); ?></p>
    </body>
</html>

Now take a look at what we have so far in your browser window at index.php/form/index.  We should see the form that we have created above.  Its action is pointed to our form controller and the index function.  The other slightly difficult part of the form.php is the dropdown.  We have used a function of the form helper that we have loaded earlier.  form_dropdown takes three arguments; the first is the name of the <select>; second is the array that contains the data; and the third argument is which <option> you want selected.  We will use this a little later on.

You should see the current problem we have with our form.  We can click submit without filling in any fields.  This is a big problem as we would never want to allow the user to do this.  This is where we need to change our above index function.

function index()
{
    $rules['title'] = "required|min_length[5]|max_length[25]";
    $rules['description'] = "required";
    $rules['email'] = "required|valid_email";
    $this->validation->set_rules($rules);
    if ($this->validation->run() == FALSE)
    {
        $query = $this->db->get('categories');
        foreach ($query->result() as $category)
        {
            $data['categories'][] = $category->title;
        }
        $this->load->view('contact', $data);
    }
    else
    {
        $this->load->view('success');
    }
}

Now, don’t get too frightened by the new code; it is fairly self-explanatory.  We have set rules for each of our fields.  The array key for the $rules variable is the same as our field name on the form.  Then we assign it a set of attributes.  For example the field with a name of ‘title’ is required, has a minimum length of 5 and a max length of 25.  Our email field is required and is checked to be a valid email.  We apply these rules to our validation with the $this->validation->set_rules($result) line.

Lastly we run our validation and load different views based on whether our validation passes or not.  One thing we are still missing is to add the following line under our form.php body tag.


< ?php echo $this->validation->error_string; ?>

This is what actually prints out our error messages.  If we didn’t add this the user would not know what fields are wrong.

There is a lot more I could show you with the validation class but I will leave it at this for now.

Tags: ,
Posted in Web Programming | 7 Comments »

 
connect with me!