Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP on a Fast Track a quick introduction to PHP programming by Jarek Francik.

Similar presentations


Presentation on theme: "PHP on a Fast Track a quick introduction to PHP programming by Jarek Francik."— Presentation transcript:

1 PHP on a Fast Track a quick introduction to PHP programming by Jarek Francik

2 Apologies to ECommerce students: this will be more or less what you know...

3 Possible Options PHP ASP.NET Java Ruby on Rails Python Perl So, which way to go?

4 The goal of this lecture is to show that PHP is not a good technology

5 PHP Scripting language for web development Created by Rasmus Lerdorf 16 years ago Currently phasing out Easy to learn but time-consuming to use

6 Let’s write a shopping cart application

7 What do we need? Operating System Web Server Database Scripring Language Windows, Linux, MacOS... Appache, IIS, WEBrick... MySQL, Postgres, SQLite, Oracle... PHP, Perl, Python, Ruby, C#, Java...

8 What do we need? Operating System Web Server Database Scripring Language Windows, Linux, MacOS... Appache, IIS, WEBrick... MySQL, Postgres, SQLite, Oracle... PHP, Perl, Python, Ruby, C#, Java...

9 What do we need? Operating System Web Server Database Scripring Language Linux, Windows, MacOS... Appache, IIS, WEBrick... MySQL, Postgres, SQLite, Oracle... PHP, Perl, Python, Ruby, C#, Java...

10 What do we need? Operating System Web Server Database Scripring Language MacOS, Windows, Linux... Appache, IIS, WEBrick... MySQL, Postgres, SQLite, Oracle... PHP, Perl, Python, Ruby, C#, Java...

11 What do we need? Operating System Web Server Database Scripring Language X - Platform Appache MySQL PHP Perl

12 What do we need? Operating System Web Server Database Scripring Language X A M P

13 First thing: The Database USE test; CREATE TABLE goods ( id int(6) unsigned NOT NULL auto_increment, item varchar(100) NOT NULL default '', price decimal(6,2) NOT NULL default '0.00', image varchar(100) NOT NULL default '', PRIMARY KEY (id) ); INSERT INTO goods VALUES (1, 'Soap', '4.99'); INSERT INTO goods VALUES (2, 'Strawberry Jam', '1.99'); INSERT INTO goods VALUES (3, 'Toothpaste', '2.49'); INSERT INTO goods VALUES (4, '8GB Memory Stick', '22.99');

14 The First PHP File <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> Your Cart Your Cart <?php ?> cart.php

15 The First PHP File <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> Your Cart Your Cart <?php echo "Hello, world!"; ?> cart.php

16 Another File: Front Page <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> Your Shop Your Shop Add item #1 to the cart Add item #2 to the cart Add item #3 to the cart Add item #4 to the cart Show your cart index.php

17 What the application should do http://localhost/cart.php?action=show http://localhost/cart.php?action=add&id=2

18 What the application should do http://localhost/cart.php?action=show http://localhost/cart.php?action=add&id=2 <?php $action = $_GET['action']; $id = $_GET['id']; echo " DEBUG: Action to do is $action, and item id is $id. "; ?> cart.php

19 Make information persistent HTTP as a stateless protocol protocol with no memory of who you are Cookies Sessions Session variables $_SESSION['cart'] Name of the variable Collection of session variables

20 Make information persistent must appear in the first line (before DOCTYPE) cart.php

21 Make information persistent...... <?php $cart = $_SESSION['cart']; $action = $_GET['action']; $id = $_GET['id']; echo " DEBUG: Action to do is $action, and item id is $id. "; if ($action == 'add') { $cart = $cart. ",$id"; $_SESSION['cart'] = $cart; } echo " DEBUG: Cart is: $cart "; ?> must appear in the first line (before DOCTYPE) cart.php

22 Display Your Cart <?php $cart = $_SESSION['cart']; $action = $_GET['action']; $id = $_GET['id']; if ($action == 'add') { $cart = $cart. ",$id"; $_SESSION['cart'] = $cart; } $myitems = explode(',', $cart);// explode using comma as a separator if (count($myitems) <= 1) echo " Your cart is empty. "; else foreach ($myitems as $i) if ($i != '') { echo " Item id: $i "; } ?> cart.php

23 Connect to the Database <?php $hostname = 'localhost';// localhost is the URL of the server $username = 'root';// the username in this example is root $password = 'elvis';// put here your MySQL root password // connect to the database server $con = mysql_connect($hostname, $username, $password) or die ('Could not connect: '. mysql_error()); // display if connection failed mysql_select_db("test", $con); // choose the test database... cart.php

24 Connect to the Database <?php $hostname = 'localhost';// localhost is the URL of the server $username = 'root';// the username in this example is root $password = 'elvis';// put here your MySQL root password // connect to the database server $con = mysql_connect($hostname, $username, $password) or die ('Could not connect: '. mysql_error()); // display if connection failed mysql_select_db("test", $con); // choose the test database... Provide the proper username & password (the latter maybe ‘’) cart.php

25 Display Your Items.... foreach ($myitems as $i) if ($i != '') { $result = mysql_query("SELECT * FROM goods WHERE id = $i"); $row = mysql_fetch_array($result); $item = $row['item']; $price = $row['price']; echo " $item: £$price "; } ?> cart.php

26 Final Polishings cart.php: Better HTML formatting Total price of the cart calculated index.php: Connected to the database

27 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> Your title here --> Your Cart <?php $hostname = 'localhost'; // localhost is the URL of the server $username = 'root'; // the username in this example is root $password = 'elvis'; // put here your MySQL root password // connect to the database server $con = mysql_connect($hostname, $username, $password) or die ('Could not connect: '. mysql_error());// display if failed mysql_select_db("test", $con); // choose the test database $cart = $_SESSION['cart']; $action = $_GET['action']; $id = $_GET['id']; if ($action == 'add') { $cart = $cart. ",$id"; $_SESSION['cart'] = $cart; } $myitems = explode(',', $cart);// explode using comma as a separator if (count($myitems) <= 1) echo " Your cart is empty. "; else { echo " "; $total = 0; foreach ($myitems as $i) if ($i != '') { $result = mysql_query("SELECT * FROM goods WHERE id = $i"); $row = mysql_fetch_array($result); $item = $row['item']; $price = $row['price']; $total += $price; echo " $item £$price "; } echo " Total &pound ;$total "; echo " "; } ?> [ Home Page ] cart.php

28 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <?php $hostname = 'localhost'; // localhost is the URL of the server $username = 'root'; // the username in this example is root $password = 'elvis'; // put here your MySQL root password // connect to the database server $con = mysql_connect($hostname, $username, $password) or die ('Could not connect: '. mysql_error()); // display if failed mysql_select_db("test", $con); // choose the test database ?> Your Shop --> Your Shop <?php $result = mysql_query("SELECT * FROM goods"); while ($row = mysql_fetch_array($result)) { $id = $row['id']; $item = $row['item']; $price = $row['price']; echo " "; echo " $item "; echo " £$price "; echo " add to cart "; echo " "; }; mysql_free_result($result); ?> Show your cart index.php

29 Second Part: Shop created with Ruby on Rails watch at: http://vimeo.com/30927971 http://vimeo.com/30927971


Download ppt "PHP on a Fast Track a quick introduction to PHP programming by Jarek Francik."

Similar presentations


Ads by Google