1 / 54

PHP on a F ast Track

PHP on a F ast Track. a quick introduction to PHP programming b y Jarek Francik last time updated in 2012. Client & Server. Client. Server. Client & Server. Client. Server. Client & Server. Remote File System. Client. Server. Client & Server. Remote File System. REQUEST: GET.

pascal
Télécharger la présentation

PHP on a F ast Track

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP on a Fast Track a quick introduction to PHP programming by JarekFrancik last time updated in 2012

  2. Client & Server Client Server

  3. Client & Server Client Server

  4. Client & Server Remote File System Client Server

  5. Client & Server Remote File System REQUEST: GET RESPONSE: HTML Client Server

  6. Client & Server Remote File System CLIENT SIDE PROCESSING REQUEST: GET RESPONSE: HTML Files served over the network may contain HTML, CSS, JavaScript,Flash and may be pretty much complex! Client Server

  7. Client & Server Remote File System REQUEST: GET RESPONSE: HTML Client Server

  8. Client & Server Remote File System REQUEST: GET RESPONSE: HTML REQUEST: GET RESPONSE: HTML Client Server

  9. Client & Server Remote File System REQUEST: POST RESPONSE: HTML REQUEST: POST DB RESPONSE: HTML Client Server

  10. Client & Server Remote File System REQUEST: POST RESPONSE: PHP REQUEST: POST DB RESPONSE: PHP Great Hiking Shoe Perfect Company Client Server SERVER SIDE PROCESSING

  11. Client-Side Processing Server-Side Processing DB

  12. Client-Side Processing Server-Side Processing Executed remotely, on a web server Results must be sent over the network Network latency Pages must be re-loaded in order to view the results* Information easily shared Database back-end Flexible and powerful security control * AJAX technology allows for remote updates without pages being reloaded but technically it is a combination of server side and client side technologies • Executed locally, on client’s computer • Results visible immediately • Fast & dynamic • Processing within a single webpage • Information cannot be shared • No Databases* • Keeping things secret is very difficult – everything is on the user’s computer * Limited local database functionality is available in HTML5, but without sharing

  13. PHP • Scripting language for web development • Created by RasmusLerdorf 16 years ago • Currently phasing out • Easy to learn but time-consuming to use

  14. Let’s writea shopping cart application

  15. 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... DB

  16. 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... DB

  17. 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... DB

  18. 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... DB

  19. What do we need? • Operating System • Web Server • Database • Scripring Language X - Platform Appache MySQL PHP Perl DB

  20. What do we need? • Operating System • Web Server • Database • Scripring Language X A M P P DB

  21. XAMPP http://www.apachefriends.org/en/xampp.html or google for “xampp”

  22. KU Server • There is a web server available for you at http://studentnet.kingston.ac.uk • Find all details there (or check the end of this presentation)

  23. XAMPP • Download and install – it’s easy • Run XAMPP Control Panel • Start Apache & MySql • Run in your browser:http://localhost • Click Explore and goto htdocsto browseyour web files • Use MySql Admin tosetup your databasewith mySqlAdmin

  24. phpMyAdmin

  25. phpMyAdmin

  26. phpMyAdmin

  27. phpMyAdmin

  28. phpMyAdmin

  29. phpMyAdmin

  30. Database structure (SQL) 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');

  31. The First PHP File <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Your Cart</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Your Cart</h1> <?php ?> </body> </html> cart.php 0

  32. The First PHP File <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Your Cart</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Your Cart</h1> <?php echo "Hello, world!"; ?> </body> </html> cart.php

  33. Another File: Front Page <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Your Shop</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Your Shop</h1> <ul> <li><a href="cart.php?action=add&id=1">Add item #1 to the cart</a></li> <li><a href="cart.php?action=add&id=2">Add item #2 to the cart</a></li> <li><a href="cart.php?action=add&id=3">Add item #3 to the cart</a></li> <li><a href="cart.php?action=add&id=4">Add item #4 to the cart</a></li> </ul> <p><a href="cart.php?action=show">Show your cart</a></p> </body> </html> index.php 1

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

  35. 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 "<p>DEBUG: Action to do is $action, and item id is $id.</p>"; ?> cart.php 1

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

  37. Make information persistent <?phpsession_start(); ?> must appear in the first line (before DOCTYPE) cart.php

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

  39. 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 "<p>Your cart is empty.<p>"; else foreach ($myitems as $i) if ($i != '') { echo "<p>Item id: $i</p>"; } ?> cart.php 3

  40. 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

  41. 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

  42. 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 "<p>$item: &pound;$price</p>"; } ?> cart.php 4

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

  44. <?phpsession_start(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Your title here</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!--<link rel="stylesheet" type="text/css" href="style.css" />--> </head> <body> <h1>Your Cart</h1> <?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 "<p>Your cart is empty.<p>"; else { echo "<table>"; $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 "<tr><td>$item</td><td>&pound;$price</td></tr>"; } echo "<tr><td><strong>Total</strong></td><td><strong>&pound;$total</strong></td>"; echo "</tr></table>"; } ?> <p>[<a href="index.php">Home Page</a>]</p> </body> </html> cart.php

  45. <!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 ?> <html> <head> <title>Your Shop</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!--<link rel="stylesheet" type="text/css" href="style.css" />--> </head> <body> <h1>Your Shop</h1> <table> <?php $result = mysql_query("SELECT * FROM goods"); while ($row = mysql_fetch_array($result)) { $id = $row['id']; $item = $row['item']; $price = $row['price']; echo "<tr>"; echo "<td>$item</td>"; echo "<td>&pound;$price</td>"; echo "<td><a href=\"cart.php?action=add&id=$id\">add to cart</a></td>"; echo "</tr>"; }; mysql_free_result($result); ?> </table> <p><a href="cart.php?action=show">Show your cart</a></p> </body> </html> index.php

  46. Other Possible Options • PHP • ... ???

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

  48. actually, PHPis rarely a good choice!

  49. THE END

  50. How to use studentnet

More Related