Building an e:commerce site with PHP
-
Upload
webhostingguy -
Category
Documents
-
view
744 -
download
4
description
Transcript of Building an e:commerce site with PHP
Server-side Technology
Dave Elliman
To Implement Megagamez’s Web Site:
• We need to build shopping trolley contents in a database
• We need to keep track of our customers and orders
• We will use MySQL and PHP to do this
Client-side and Server-side Scripting
• Client-side– Javascript– VBScript (MS)
• Can be used to validate input
• Can implement mouse-over effects etc
• Server-side– cgi– PHP– ASP– ASP.NET– JSP– Perl & Ruby – these
ugly sisters will not be studied in ELC
• Communicates with database
• Application logic
04/10/23 PHP for MegaGamez 4
G53ELC
Server Side Processing
Web Server Usage
There Are Two Worlds
• Apache/Open Source
• PHP • JSP J2EE Servlets• Linux Servers• MySQL DB• ASP possible with
Chillisoft (now Sun ASP)
• Microsoft IIS• ASP, ASP.NET• Windows 2000
Server• Write in C# • MS SQL Server
2000• Develop with
Visual Studio .Net
Which World Is Best?
• Open Source• Free or nearly so• Cheap hosting• Excellent
Performance• May need to spend
money to make scalable (JBOSS free)
• Development tools OK but the best ones not free
• Microsoft• Expect to spend
£10,000 on software• Hosting expensive• Excellent
Performance – more scalable
• Compelling ease of development
Megagamez Has Only £70 in the Bank
• We will use for Apache/MySQL/PHP• We will design our database tables• We will design our security idea• We will make it work• First of all let’s understand PHP
PHP History
• 1994: Created by Rasmis Lesdorf, a software engineer in Apache Team
• 1995: Called Personal Home Page Tool• 1997: used by 50,000 web sites• 1998: used by 100,000 websites• 1999: used by 1,000,000 websites• 2002: 30% of Web sites uses PHP
04/10/23 PHP for MegaGamez 10
G53ELC
http://www.netcraft.com/Survey
PHP Is
• Open-source• Easy to use but not pretty• Stable and fast• Runs on all platforms• Supports all databases • Has some useful libraries• Pre-installed in Linux distributions
04/10/23 PHP for MegaGamez 12
G53ELC
How It Works
Cache of compiled pages
Or in Words…
• The Client from browser sends an HTTP request (with POST/GET variables)
• Apache recognizes that a PHP script is requested and sends the request to PHP module
• The PHP interpreter executes the PHP script and returns the script output
• Apache replies to client using the PHP script output interspersed with HTML output
04/10/23 PHP for MegaGamez 14
G53ELC
“Hello World” in PHP
<html><head> <title>My personal Hello World! PHP script</title></head><body><?
echo “Hello World!”;?></html>
04/10/23 PHP for MegaGamez 15
G53ELC
How Do You Run It?
• See: http://www.cs.nott.ac.uk/TSG/manuals/webpages/php/
• #!/usr/local/bin/php // 1st line of script• mkdir ~/cgi-bin • chmod 711 ~/. ~/cgi-bin (sorry!)• chmod 755 *.php• URL is http://
robin.cs.nott.ac.uk/~<you>/cgi-bin/test.php
04/10/23 PHP for MegaGamez 16
G53ELC
Using Variables
• Variables start with a $• The assignment operator is =• No need to declare variables• Variables type is implicit• No check on variable used before it
is assigned• Arrrrrrrrrgh… Reminiscent of BASIC
- for the cowboys? (wja says so)
04/10/23 PHP for MegaGamez 17
G53ELC
An Example
• A string is a sequence of characters in quotes for example “This is a String” and ‘so is this’
• A string can be assigned to a variable:
• $Homer = “The next president”• $Homer = ‘The next president’• Both work…
04/10/23 PHP for MegaGamez 18
G53ELC
Substitution in Strings
$age = 37$Homer = “Mr Simpson is $age”$Homer is Mr Simpson is 37This does not work in single quotesWhich would come out as:Mr Simpson is $age
04/10/23 PHP for MegaGamez 19
G53ELC
This Is Slightly Scary to Me
$A = 1;
$B = “2”;
$C = ($A + $B); // Integer sum
$D = $A . $B; // String catenation
echo $C; // prints 3
echo $D; // prints 12
04/10/23 PHP for MegaGamez 20
G53ELC
I Do Like the Explode Function – Good for csv
$sequence = “A,B,C,D,E,F,G”;
$elements = explode (“,”,$sequence);
// Now elements is an array with all substrings between “,” char
echo $elemets[0]; // output: A;
echo $elemets[1]; // output: B;
echo $elemets[2]; // output: C;
echo $elemets[3]; // output: D;
echo $elemets[4]; // output: E;
echo $elemets[5]; // output: F;
echo $elemets[6]; // output: G;
04/10/23 PHP for MegaGamez 21
G53ELC
Arrays
$books = array( ”Enduring Love”,”The Child in Time”,”The Cement Garden”);
for ($i=0; $i < count($books); $i++) print ($i+1).”: $books[$i]”;
04/10/23 PHP for MegaGamez 22
G53ELC
Arrays As a Key->element Mapping
• $students = array( 0=>”Mike”, 1=>”Bill”, 2=>”Fred”, 3=>”Ann”)
• echo $students[2]; Fred• Echo $students[“Fred”]; 2• In other words PHP arrays are
associative
04/10/23 PHP for MegaGamez 23
G53ELC
More on Array Maps
$books = array(”Enduring Love”=>1,” The Child in Time”=> 2,” The Cement Garden”=>3);
while ($item = each( $books )) print $item[“value”].” : ”.$item[“key”];1 : Enduring Love2 : The Child in Time3 : The Cement Garden
04/10/23 PHP for MegaGamez 24
G53ELC
Connecting to a Database
<?php //connect to database $conn=mysql_connect(“sauron.cs.nott.ac.uk",“usr",“pwd") or die(“could not connect to database “.mysql_error());mysql_select_db(“megagamez”) or die(“message”);
// do something
………
//disconnect from database mysql_close($conn);
?>
04/10/23 PHP for MegaGamez 25
G53ELC
Executing Database Queries
<?php $query = "SELECT name, age, phone FROM students
WHERE (name LIKE '%Smith%')"; //execute query $result = mysql_execute($query); while($row = mysql_fetch_array($result)) {
echo($row[“name”].”, “);echo($row[“age”]);
}
?>
04/10/23 PHP for MegaGamez 26
G53ELC You Will Want More Control Over the Format –
So Make a Table<table width="75%" border="1" cellspacing="1" cellpadding="1" bgcolor="#FFFFFF"> <tr bgcolor="#CCFFFF"> <td height="22"><b>Name</b></td> <td height="22"><b>Age</b></td> <td height="22"><b>Telephone</b></td> </tr>
…some php stuff
</table>
04/10/23 PHP for MegaGamez 27
G53ELC
What About Inserting Data?
• I cannot do better than the excellent tutorial which makes a web site of jokes:– http://www.zend.com/zend/tut/tutorial-yank.php
04/10/23 PHP for MegaGamez 28
G53ELC
Learning More About PHP
• Try http://www.php.net/manual/• Books:• O’Reilly Press, Web Database
Applications with PHP and MySQL Hugh E Williams, David Lane
04/10/23 PHP for MegaGamez 29
G53ELC
Learning More About PHP