This Season
 

PHP Web Design Tutorial

PHP Web Design Tutorialthumbnail
PHP is used to create dynamic websites

Building websites in PHP allows you to create dynamic experiences for visitors. Websites in PHP are typically built on top of a database at the Server side and joined by other technologies such as JavaScript and CSS at the Client side. For people who have no programming experience, starting to write code in PHP can seem intimidating, but most common tasks are generally straightforward. Focusing on one task at a time is the most sensible approach.

Related Searches:
    Difficulty:
    Moderate

    Instructions

      • 1

        Create your website pages. Start by creating your homepage. Open a blank file in a text editor or Web development environment if you are using one. Save the file "index.php" and enter the following code:

        <?php

        echo

        "<html>

        <head></head>

        <body>

        <div>My PHP Page</div>

        </body>

        </html>";

        ?>

        You can alternatively achieve the same result by alternating between HTML and PHP in your file:

        <html>

        <head></head>

        <body>

        <?php

        echo

        "<div>My PHP Page</div>";

        ?>

        </body>

        </html>

        If you upload the file to your server and browse to it in a Web browser, you'll see that these have the same effect. Use whichever style you find easiest.

      • 2

        Connect to your data. Server-side programming such as PHP is generally used to allow a site data connectivity. PHP has a number of functions that can be used in order to achieve this. For a MySQL database, the following connection code is used:

        <?php

        //alter the 3 parameters: host, username, password

        mysql_connect("localhost", "user", "pass");

        //alter the database name

        mysql_select_db("database_name");

        ?>

        You can include your connection code, or any other code, in a separate file which can be called from other pages. For example, you could include the connection code in a file called "my_connection.php" and then at the top of any pages in which the connection is needed:

        require("my_connection.php");

        This will cause any code in that script to run, unless the code is contained within functions, in which case it executes when the function is called.

      • 3

        Query the data and output what you need for the site pages. Most data-driven websites will use the data in their databases for very specific reasons, which naturally vary according to their overall site purpose. Often a PHP script will be used to query the data and output it in HTML. The following example would work for a database with a table called "client_table" which has columns for "name" and "address" within it:

        <?php

        //create the query in SQL

        $my_query="select * from client_table";

        //execute the query

        $my_result=mysql_query($my_query);

        //loop through the results

        while($my_row=mysql_fetch_array($my_result))

        {

        //output the data in HTML

        $row_name=$my_row['name'];

        $row_address=$my_row['address'];

        echo "<div>";

        echo "<p><strong>".$row_name."</strong></p>";

        echo "<p>".$row_address."</p>";

        echo "</div>";

        }

        ?>

      • 4

        Update your data using PHP. In most cases, a site built in PHP will be required to update or alter the data in a database. This is carried out in a similar way to querying it. A common task in PHP is creating a form in which details held in the database can be updated. In this case when the form is submitted, the details are sent to another script via the "POST" variable. This code is an example of the script that these updates may be sent to:

        <?php

        //create the update statement - the client id and name are sent via POST

        $my_update="update client_table set name='".$_POST['name']."' where id=".$_POST['id'];

        //execute the update

        $update_result=mysql_query($my_update);

        //give feedback

        if(!$update_result) echo "<p>Whoops - something went wrong!</p>";

        else "<p>Thanks for your update!</p>";

        ?>

      • 5

        Test your PHP code functionality. Whenever you build a Web application using PHP it's imperative that you test it thoroughly. The more complex an application, the more things can go wrong. If your data is important, it is doubly important that you make sure the site is functioning correctly before going live. A good idea is to use "dummy" or test data while you're building the site, and only enter the correct data once you know that the PHP code is working. This should avoid the data being compromised.

    Tips & Warnings

    • Refer to online resources for PHP, of which there are many. PHP has a lot of built in libraries for common tasks, which can save you huge amounts of effort. You can also use techniques such as AJAX, through which you can request information from the server, presenting it without having to reload or leave the current page.

    • Be wary of making unnecessary connections to the database in a PHP site. Try to reduce the number of times your site requests data from the Server where possible, and in doing so reduce the network traffic and bandwidth usage for your hosting package.

    Related Searches

    References

    Resources

    • Photo Credit stock image of earth in binary code image by Ruslana Stovner from Fotolia.com

    Read Next:

    Comments

    You May Also Like

    • PHP Design Tutorials

      PHP is a programming language used mostly on website servers. While the language is often integral to creating the website, the actual...

    • AJAX PHP MySQL Tutorial

      AJAX, PHP and MySQL are three common elements of many professional-quality websites. AJAX is a programming technique that allows you to create...

    • How to Learn PHP Web Design

      PHP is one of the most popular scripting languages for developing dynamic websites. PHP syntax is simple to learn, and can be...

    • PHP Tutorial: MySQL

      MySQL is an open source database. SQL, or Structured Query Language, is the language used to create, query and manipulate MySQL databases....

    • PHP SQL Tutorial

      PHP is a programming language of the Internet. Part of creating dynamic pages is using SQL in PHP to retrieve data from...

    • Web Design Tutorial for Beginners

      Web design tutorials come in a variety of formats, from online tutorials to in-person workshops. Online tutorials introduce one or two concepts...

    • Advanced Mysql PHP Tutorial

      PHP and MySQL are two open-source software technologies used to manage websites. PHP controls all the server-side programming that affects how a...

    • PHP Javascript Tutorial

      JavaScript and Hypertext Preprocessor, or PHP, are two of the most popular programming languages used in Web services, but they work in...

    • Advanced Web Design Tutorial Tips

      Learning the basic codes and tags of HTML is easy, but every web designer should step up to learn advanced web design...

    • Dynamic Web Design Tutorial

      A dynamic Web design includes distinctive features, such as elastic content, that encourage interaction from the user or web browser. In this...

    Follow eHow

    Related Ads