A Beginners Guide To Develop Web Applications in PHP
[Source: Woork ] Recently i started development in PHP and was searching for online guides to learn development process the proper way and i found several great sources. I have summarized all i learned here and hope will help newbies to quick-start. Better grab your coffee before you start on this! ;)
About the application
What kind of application we'll develop in this tutorial? I chose a simple to do list application to manage a list of tasks with these basic features:
- insert taskIn this first part, I'll explain how to:
- mark a task as completed
- remove task
- search task
- define application structureAre you ready? Let's go!
- create a new database for your application
- create database tables and relationships
- setup the application home page
1. Application structure
First question is: how to organize our application? We can start using this simple structure which will be modified in the next steps adding new elements (folders and files):
The main folder todo-list will contain all application files and folders; db folder will contains all files to create and manage application database. connection.phpenstablishes a database connection in all pages which require interaction with data stored into our database (see step 2.3).
2.1 - Database: create a new DB
Second step:create a new database for our application to store tasks information. We can use phpMyAdmin to create a new DB:
You have to add a name (in this case todo-list) in the input field and click on createbutton. Your new database will appear on the left panel: the number (0) indicates your database is empty (no tables).
2.2 - Database: define DB tables
At this point, we have to define database tables. In our simple to do list application we have only a table TASK with these attributes:
Now we have to create this table into our new database. In general, you can create database tables and relationship directly using phpMyAdmin interface or writing SQL code in a separated PHP file ( tables_structure.php). I prefer to use the second option because it's simpler to manage. So, open tables_structure.php and copy and past this code:
php
//Database Connection
include('connection.php');
// Create table TASK
$sql="CREATE TABLE TASK (
task_id_pk INT NOT NULL AUTO_INCREMENT,
task_name VARCHAR(250),
task_description LONGTEXT,
task_completed INT, PRIMARY KEY (task_id_pk)
) TYPE=INNODB";
mysql_query($sql);
// Close DB Connection
mysql_close($db_con);
?>
//Database Connection
include('connection.php');
// Create table TASK
$sql="CREATE TABLE TASK (
task_id_pk INT NOT NULL AUTO_INCREMENT,
task_name VARCHAR(250),
task_description LONGTEXT,
task_completed INT, PRIMARY KEY (task_id_pk)
) TYPE=INNODB";
mysql_query($sql);
// Close DB Connection
mysql_close($db_con);
?>
In the first row I included the following line:
//Database Connection
include('connection.php');
include('connection.php');
...which enstablishes a database connection (see next step). So I added a PHP var $sqlwhich contains SQL code to create a new table "Task". This code:
mysql_query($sql);
...executes a query with SQL code declared into $sql var.
2.3 - Database: connection.php
Enstablishing a database connection using PHP is very simple. You can use the following code only changing connection parameters values ($db_host, $db_name,$username, $password):
// Connection Parameters
$db_host="localhost";
$db_name="todo-list";
$username="root";
$password="root";
$db_con=mysql_connect($db_host,$username,$password);
$connection_string=mysql_select_db($db_name);
// Connection
mysql_connect($db_host,$username,$password);
mysql_select_db($db_name);
?>
In general $db_host, $username, $password don't require changes. This var:
$db_name="todo-list";
...contains the name of database we created at step 2.1.
Note: you have to include connection.php in each page which uses interaction with database.
2.4 - Database: create tables
Ok, now you can publish tables_structure.php and connection.php on your testing server and load tables_structure.php with your browser to create tables and relationships (in this case tables_structure.php will create only the table TASK ):
If code it's ok, using phpMyAdmin, you can find the new table "TASK" into our todo-list database:
3.1 - Index.php: include connection.php
Open index.php and add this line of code at the beginning of the page to enstablish a database connection:
php include('db/connection.php'); ?>
Remember, you have to include this file in each page which interacts with database:
Read full post here
1:25 AM |
Labels:how-to,PHP,Tools,Tutorials,Web Designing |
This entry was posted on 1:25 AM
and is filed under how-to,PHP,Tools,Tutorials,Web Designing
.
You can follow any responses to this entry through
the RSS 2.0 feed.
You can leave a response,
or trackback from your own site.
Comments for this post All comments