Intent

Web programming in Language:PHP

Motivation

Currently the most popular and complete web programming language, which is focused on the creation of web resources - is Language:PHP. Its popularity is due to a number of advantages that it gives the developer. Code written in this language, does not look bulky and basic scenarios are created in a few lines.

Language:PHP is embedded in ordinary Language:HTML-code page, which provides a high level of flexibility in its use, and security is provided by Language:PHP -stealth insertions in the browser (displayed normal Language:HTML). Languageallows you to create dynamic pages that open up great opportunities for web design and programming.

Due to the simplicity of the code, script execution takes place at a fairly high speed, since the nature of language can be described as an interpreter translating (the script is processing and displaying its result in a visual form). Speed ​​of processing scenarios is ideal for applications of any complexity.

In this Contribution:PHP implementation, will be described basic functions and methods of Language:PHP language.

Illustration

Architecture

All requests, which are executing in current Language:PHP implementation are going through index.php file.

Lets have a look on it:

1) On first step we connection configuration file and file with functions

include once('lib/config.php');
include once('lib/functions.php');

2) Next in file you can see part, which will catch all actions. This mean, that all POST queries (and queries that are not required display something to user) will be executed, and page will be redirected back.

if (get input('type') == 'action'){
	switch(get input('action')){
		case 'company':
			include('actions/company.php');
			back();
			break;
		case 'department':
		…………

3) Includes part.

Includes header and in the end of file footer. This HTML code, that will be the same for all pages (it includes Language:CSS and Language:JavaScript files).

include once('template/header.php');
……
include once('template/footer.php');

In the middle located code, that will catch “section” variable. And display need content to user

switch(get input('section')){
	case 'employee':
		$eid = get input('eid');
		$employeeInfo = getEmployees($eid, true);
		include('template/employee.php');
		break;
		……..

PHP and Mysql connection

For more details about Mysql, please read Contribution:mySqlMany

Language:PHP has build-in functions for working with database. Lets have a look more detailed on them.

// /lib/functions.php
$db = mysql connect(DB HOST, DB USER, DB PASSWORD);
if (!$db) {
    	die('Db connection error: ' . mysql error());
}
$dbSelected = mysql select db(DB NAME, $db);
return $dbSelected;

On this example:

$db = mysql connect(DB HOST, DB USER, DB PASSWORD);

is function for connecting with Technology:MySQL database, taking as parameters Technology:MySQLhost, Username and password. Look http://www.php.net/manual/en/function.mysql-connect.php for more information

After we initialized connection, we will use it as default in all operations with database in this implementation.

List of Technology:MySQL functions that were used in Language:PHP implementation.

mysql select db(); select used Database name;
mysql query(); // execution of Mysql query;
mysql fetch assoc(); // return query result as associative array;

// get company information
function getCompany(){
	//Execute query
	$result = mysql query('SELECT * FROM company');
	
	//get DB table as Array
	$data = array();
	while($row = mysql fetch assoc($result)){
		$data[] = $row; 
	}
	
	return $data;
}

Feature implementation

Cut salary for all employees or for all department, making using Technology:MySQL queries:

function cutCompanySalary($did = 0){
	
// Prepare query for cutting the salary
	$query = 'UPDATE employee SET salary = salary/2';
	$query .= " WHERE cid = 1"; // set Id of company – in our case we have just one company
	
	if ($did != 0){ // if we have department ID to cut, then add department id selector
		$query .= ' AND did='.$did;
	}
	
//Execute mysql query
	if (mysql query($query)){
		return true;
	}else{
		return false;
	}
	
}

Usage

You need a Web Browser to open the HTML files.

This implementation required to have Technology:MySQL-database. Can be used company.sql and sampleCompany.sql of Contribution:mySqlMany for this project.

For run this implementation, you should have Technology:Apache HTTP Server installed, with supporting of Technology:PHP and Technology:MySQL server installed.

Metadata


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    10 most similar pages:

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.