2012 | PHP by Jochem

1,337 views
1,209 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,337
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

2012 | PHP by Jochem

  1. 1. PHP & MYSQL The basicsThursday, November 15, 12
  2. 2. purpose of this course You can only master web-development by doing it a lot This course enables you to start doing it (a lot) Learn the language Walk through the possibilities Know the (online) references for the futureThursday, November 15, 12
  3. 3. end of this week ?Thursday, November 15, 12
  4. 4. planning Day 1: PHP Day 2: CodeIgniter Day 3: MySQL Day 4: Form Handling & AJAX Day 5: Integrating (login system)Thursday, November 15, 12
  5. 5. today Overview web-development PHPThursday, November 15, 12
  6. 6. OVERVIEW WEB-DEVELOPMENT What does it take to build a websiteThursday, November 15, 12
  7. 7. Browser / Frontend HTML Content CSS Style Java- Behavior scriptThursday, November 15, 12
  8. 8. Server / Backend PHP data baseThursday, November 15, 12
  9. 9. Front-end & Back-end Frontend Backend Java- HTML CSS script generate PHP data baseThursday, November 15, 12
  10. 10. PHP ScriptingThursday, November 15, 12
  11. 11. PHP Hypertext Preprocessing Executed on server Communication with database Handle data Parsing (parts of) HTMLThursday, November 15, 12
  12. 12. PHP file write in text-editor .php HTML & PHP code Executed on request by browser Return HTML to browserThursday, November 15, 12
  13. 13. Syntax localhost/day1/hello.php <?php the php code..; ?>Thursday, November 15, 12
  14. 14. Commenting One line comment: //... Multiple line comment: /* ... */Thursday, November 15, 12
  15. 15. Variables Store a value in a variable Use/change the value later on PHP var starts with $ Name begins with letter or underscore Name may contain A-z, 0-9, _ Case sensitiveThursday, November 15, 12
  16. 16. year.phpThursday, November 15, 12
  17. 17. Main vars int string array number text list of values $x = 25; $x = “Hello”; $x = array(1,24,12); $y = 25.5; combine with . to be continued.. $y = “world”; $z = $x . $y;Thursday, November 15, 12
  18. 18. Single & double quotesThursday, November 15, 12
  19. 19. Exercise ...... Use double quotes for $sentence 1 Use single quotes for $sentence 2Thursday, November 15, 12
  20. 20. Arithmetic operatorsThursday, November 15, 12
  21. 21. ShortcutsThursday, November 15, 12
  22. 22. IfThursday, November 15, 12
  23. 23. ElseThursday, November 15, 12
  24. 24. ElseifThursday, November 15, 12
  25. 25. ExerciseThursday, November 15, 12
  26. 26. Logical operatorsThursday, November 15, 12
  27. 27. Exercise find largest use AND or && indicate if no largestThursday, November 15, 12
  28. 28. Arrays List of values Stored in 1 variable Each value has its own indexThursday, November 15, 12
  29. 29. Numeric Array Each index is a numberThursday, November 15, 12
  30. 30. Associative Array Each index is a wordThursday, November 15, 12
  31. 31. For loop Run block of code several times for(init; condition; increment){ } optional: stop with break;Thursday, November 15, 12
  32. 32. Loop over ArrayThursday, November 15, 12
  33. 33. ForeachThursday, November 15, 12
  34. 34. Exercise Create 1 array with your family members Find a way to store relation and name Echo per member “I’ve got a relation named name” So, “I’ve got a brother named Maarten”Thursday, November 15, 12
  35. 35. Multidimensional Arrays Array in array See multidimensional.phpThursday, November 15, 12
  36. 36. Function 700+ predefined functions Self-made functionsThursday, November 15, 12
  37. 37. Create your own functionThursday, November 15, 12
  38. 38. Function parametersThursday, November 15, 12
  39. 39. Default parametersThursday, November 15, 12
  40. 40. Exercise Create calculating function 3 parameter: 2 numbers and the operator (text) Function returns result Example: calc(2,3,‘multiply’); //returns 6 Hint: use switch()Thursday, November 15, 12
  41. 41. Form Handling PHP can handle the data of an HTML form Data is communicated in a array named $_POST See post.phpThursday, November 15, 12
  42. 42. CODEIGNITER PHP frameworkThursday, November 15, 12
  43. 43. PHP Framework Set of folders & PHP files Predefined structure Many predefined functionsThursday, November 15, 12
  44. 44. Model View Controller Separate Lay-out, Logic & Database traffic Request Answer Lay-out / view: HTML Logic / controller: PHP Controller Database / model: MySQL Model ViewThursday, November 15, 12
  45. 45. Model View Controller Separate Lay-out, Logic & Database traffic Request Answer Lay-out / view: HTML Logic / controller: PHP Controller Database / model: MySQL data Model View baseThursday, November 15, 12
  46. 46. Models Users Products Sales Texts Controllers Home Catalog Product Buy History Views Home Catalog Buy History ProductThursday, November 15, 12
  47. 47. Models Users Products Sales Texts Controllers Home Catalog Product Buy History x5 Views Home Catalog Buy History ProductThursday, November 15, 12
  48. 48. Models Users Products Sales Texts Controllers Home Catalog Product Buy History x1 Views Home Catalog Buy History ProductThursday, November 15, 12
  49. 49. Install www.codeigniter.com download unzip place content in folder day2 (in www or htdocs) go to http://localhost/day2Thursday, November 15, 12
  50. 50. From URL to Controller Go to http://localhost/day2/index.php/welcome/index Site url Controller Function http://localhost/day2/index.php welcome index Check it in application/controllers/welcome.phpThursday, November 15, 12
  51. 51. Loading views $this->load->view() CI knows where the views folder isThursday, November 15, 12
  52. 52. Sending variables Create associative array Send as second parameter in $this->load->view()Thursday, November 15, 12
  53. 53. Creating more controllers Duplicate existing Change filename Change ClassName (Capitalized) function index() is called if no function is called in URLThursday, November 15, 12
  54. 54. Creating more functions Create a public function ...() Load a view Create the view Go to URLThursday, November 15, 12
  55. 55. Configuring your website applications/config/config.php applications/config/autoload.php applications/config/routes.phpThursday, November 15, 12
  56. 56. Config.php Information for CI URL of the websiteThursday, November 15, 12
  57. 57. Autoload.php Load libraries & helpers E.g. Database, Session Load in autoload.php for site-wide Load in controller for only that pageThursday, November 15, 12
  58. 58. Routes.php Convert URLs to controllers Point /about-me to controller aboutThursday, November 15, 12
  59. 59. Navigating through your site Create a view ‘menu_view.php’ Create simple navigation Use site_url() in links Load view in top of every viewThursday, November 15, 12
  60. 60. Using HTML template Download ‘Template.php’ from dropbox Place in application/libraries/ Add to $libraries in application/config/autoload.php Change $this->load->view() to $this->template->load(‘template_view’, ‘viewfile’, vars)Thursday, November 15, 12
  61. 61. Creating HTML template Create template_view.php Add basic HTML structure Load ‘menu_view.php’ in bodyThursday, November 15, 12
  62. 62. Recreating Calculator Create controller calculator.php Add to menu Create view calculator_view.php HTML in view, Logic in controllerThursday, November 15, 12
  63. 63. Form Validation Lookup at codeigniter reference Create contact_view.php Build email form with name, email, message, hidden, submit Create controller contact.php Load Form_validation in controller, set up and check for correct valuesThursday, November 15, 12
  64. 64. Send email Lookup at codeigniter reference Load Email in controller contact.php If correct values, send emailThursday, November 15, 12
  65. 65. Exercise Create 5 page website (home, about me, portfolio, calculator, contact) Use template and ‘menu_view.php’ Make calculator and contact workThursday, November 15, 12
  66. 66. SQL DatabasesThursday, November 15, 12
  67. 67. Structured Query Language Query is a request/command to a database In our case, a MySQL databaseThursday, November 15, 12
  68. 68. Possibilities Retrieve data from DB > SELECT Insert data in DB > INSERT Update data in DB > UPDATE Delete data from DB > DELETE Create tables in DBThursday, November 15, 12
  69. 69. Database Database is build up in tables Tables contain records (rows) with dataThursday, November 15, 12
  70. 70. Select Select * FROM persons Select LastName, FirstName FROM personsThursday, November 15, 12
  71. 71. Where SELECT * FROM persons WHERE City = Sandnes SELECT LastName FROM Persons WHERE FirstName = Tove = <> < > <= >=Thursday, November 15, 12
  72. 72. And SELECT P_id FROM Persons WHERE FirstName = tove AND LastName = SvendsonThursday, November 15, 12
  73. 73. Order By SELECT * FROM persons ORDER BY LastNameThursday, November 15, 12
  74. 74. Insert INSERT INTO Persons VALUES (4,Nilsen, Johan, Bakken 2, Stavanger)Thursday, November 15, 12
  75. 75. Update UPDATE Persons SET Address=Nissestien 67, City=Sandnes WHERE LastName=Tjessem AND FirstName=JakobThursday, November 15, 12
  76. 76. Delete DELETE FROM PersonsWHERE LastName=Tjessem AND FirstName=JakobThursday, November 15, 12
  77. 77. CodeIgniter $this->db->select(‘*’); $this->db->from(‘persons’); $this->db->where(‘P_id’, 5); $this->db->where(‘P_id’, 5); $this->db->get(‘persons’)->row_array(); $this->db->get()->row_array();Thursday, November 15, 12
  78. 78. Configuring application/config/database.php Enter MySQL Username Enter MySQL Password Enter MySQL Database Add ‘database’ to autoload.php in LibrariesThursday, November 15, 12
  79. 79. Security INSERT INTO persons (FirstName, LastName) VALUES (‘Dirk’, ‘Hens’); Text in FirstName input: Dirk’, ‘Hens’), (‘Jochem INSERT INTO persons (FirstName, LastName) VALUES (‘Dirk’, ‘Hens’), (‘Jochem’, ‘van Kapel) CodeIgniter auto adds “”: INSERT INTO persons (FirstName, LastName) VALUES (“‘Dirk’, ‘Hens’), (‘Jochem’”, “‘van Kapel’”);Thursday, November 15, 12

×