PHP Web Design Tutorial
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.
- 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.
-
1
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