Building an e:commerce site with PHP


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Building an e:commerce site with PHP

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