Developing web applications


Published on

Published in: Education
  • 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

Developing web applications

  1. 1. Sending Values ManuallyPengaturcaraan PHPPengaturcaraan PHPIn the examples so far, all of the data received in the PHP script came fromwhat the user entered in a form. There are, however, two different waysyou can pass variables and values to a PHP script, both worth knowing.The first method is to make use of HTMLs hidden input type: 1
  2. 2. Pengaturcaraan PHPThe second method is to append a value to the PHP scripts URL:This technique emulates the GET method of an HTML form. With thisspecific example, page.php receives a variable called $_GET[name] with avalue of Brian.To demonstrate this GET method trick, a new version of the view_users.phpscript will be written. This one will provide links to edit or delete an existinguser. The links will pass the users ID to the handling pages, both of whichwill be written subsequently. 2
  3. 3. Pengaturcaraan PHPStep 2Change the SQL query as follows. We have changed this query in a couple of ways. First, we select the first and last names as separate values, instead of as one concatenated value. Second, we now also select the user_id value, which will be necessary in creating the links. Pengaturcaraan PHP Step 3 Add three more columns to the main table. In the previous version of the script, there were only two columns: one for the name and another for the date the user registered. Weve separated out the name column into its two parts and created one column for the Edit link and another for the Delete link. 3
  4. 4. Pengaturcaraan PHPStep 4Change the echo statement withinthe while loop to match the tablesnew structure.For each record returned fromthe database, this line will printout a row with five columns. Thelast three columns are obviousand easy to create: just refer tothe returned column name. Pengaturcaraan PHPFor the first two columns,which provide links to edit ordelete the user, the syntax isslightly more complicated. Thedesired end result is HTMLcode like <ahref="edit_user.php?id=X">Edit</a>, where X is the users ID.Having established this, all wehave to do is print$row[user_id] for X, beingmindful of the quotation marksto avoid parse errors. 4
  5. 5. Pengaturcaraan PHP Using Hidden Form InputsPengaturcaraan PHP 5
  6. 6. Pengaturcaraan PHP Step 1 Create a new PHP document in your text editor or IDE.Pengaturcaraan PHPStep 2Include the page header. This document will use the same template systemas the other pages in the application. 6
  7. 7. Pengaturcaraan PHPStep 3Check for a valid user ID value.Pengaturcaraan PHPStep 4Include the MySQL connection script. 7
  8. 8. Pengaturcaraan PHPStep 5Begin the main submit conditional.Pengaturcaraan PHPStep 6Delete the user, if appropriate. 8
  9. 9. Pengaturcaraan PHPPengaturcaraan PHPStep 7Check if the deletion worked and respond accordingly. 9
  10. 10. Pengaturcaraan PHPStep 11Display the form. First, the database information is retrieved using themysql_fetch_array() function. Then the form is printed, showing the namevalue retrieved from the database at the top. An important step here isthat the user ID ($id) is stored as a hidden form input so that thehandling process can also access this value. Pengaturcaraan PHP 10
  11. 11. Editing Existing Records Pengaturcaraan PHPPengaturcaraan PHPStep 1Create a new PHP document in your text editor or IDE. 11
  12. 12. Pengaturcaraan PHPStep 3Include the MySQL connection script and begin the main submit conditional.Pengaturcaraan PHPStep 4Validate the form data. 12
  13. 13. Pengaturcaraan PHPPengaturcaraan PHPStep 6Update the database. 13
  14. 14. Pengaturcaraan PHPDisplay the form. The form has but three text inputs, each of which is madesticky using the data retrieved from the database. The user ID ($id) is storedas a hidden form input so that the handling process can also access thisvalue. Paginating Query Results Pengaturcaraan PHP 14
  15. 15. Pengaturcaraan PHPPengaturcaraan PHPStep 2After including the database connection, set the number ofrecords to display per page. 15
  16. 16. Pengaturcaraan PHPStep 3Check if the number of required pages has been determined.Pengaturcaraan PHPStep 4Count the number of records in the database. 16
  17. 17. Pengaturcaraan PHPStep 5Mathematically calculate how many pages are required.Pengaturcaraan PHPStep 6Determine the starting point in the database. 17
  18. 18. Pengaturcaraan PHPStep 7Change the query so that it uses the LIMIT clause.Pengaturcaraan PHPStep 12After completing the HTML table, begin a section for displaying linksto other pages, if necessary. 18
  19. 19. Pengaturcaraan PHPPengaturcaraan PHP 19
  20. 20. Pengaturcaraan PHPStep 13Finish making the links.Pengaturcaraan PHP 20
  21. 21. Making Sortable Displays Pengaturcaraan PHPPengaturcaraan PHPStep 1Open or create view_users.php in your text editor or IDE. 21
  22. 22. Pengaturcaraan PHPStep 3Check if a sorting order has already been determined.Pengaturcaraan PHPStep 4Begin defining a switch conditional that determines how theresults should be sorted. 22
  23. 23. Pengaturcaraan PHPStep 5Complete the switchconditional. There are sixtotal conditions to checkagainst, plus the default(just in case). For each the$order_by variable isdefined as it will be used inthe query and theappropriate link isredefined. Since each linkhas already been given adefault value (Step 2), weonly need to change asingle links value for eachcase.Pengaturcaraan PHP Step 6 Complete the isset() conditional. 23
  24. 24. Pengaturcaraan PHPStep 7Modify the query to use the new $order_by variable.Pengaturcaraan PHPStep 8Modify the table header echo statement to create links out of the columnheadings. 24
  25. 25. Pengaturcaraan PHPStep 9Modify the echo statement that creates the Previous link so that the sortvalue is also passed.Pengaturcaraan PHP Step 10 Repeat Step 9 for the numbered pages and the Next link. 25
  26. 26. Pengaturcaraan PHP Understanding HTTP HeadersPengaturcaraan PHP 26
  27. 27. Pengaturcaraan PHPHTTP (Hypertext Transfer Protocol) is the technology at the heart of the WorldWide Web because it defines the way clients and servers communicate (inlaymans terms).PHPs built-in header() function can be used to take advantage of this protocol.The most common example of this will be demonstrated here, when theheader() function will be used to redirect the Web browser from the currentpage to another.To use header() to redirect the Web browser, type the following:Pengaturcaraan PHP 27
  28. 28. Pengaturcaraan PHPYou can avoid this problem using the headers_sent() function, whichchecks whether or not data has been sent to the Web browser.Pengaturcaraan PHP 28
  29. 29. Pengaturcaraan PHPStep 6Dynamically determine the redirection URL and then call the header() function. header (Location: http:// . $_SERVER[HTTP_HOST] . dirname($_SERVER[PHP_SELF]) . /newpage.php);Pengaturcaraan PHPPassing valuesYou can add name=value pairs to the URL in a header() call to passvalues to the target page. In this example, if you added this line to thescript, prior to redirection:$url .= ?name= . urlencode ("$fn $ln");then the thanks.php page could greet the user by $_GET[name]. 29
  30. 30. EndPengaturcaraan PHP 30