intTypePromotion=1

Professional PHP Programming phần 8

Chia sẻ: Hà Nguyễn Thúy Quỳnh | Ngày: | Loại File: PDF | Số trang:86

0
56
lượt xem
4
download

Professional PHP Programming phần 8

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'professional php programming phần 8', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: Professional PHP Programming phần 8

  1. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com } // End of while F inally we redirect the client to u ser_admin.php p age. header("Location:http://$HTTP_HOST/$DOCROOT/user_admin.php"); ?> Viewing the Transactions of a User T he script v iew_transactions.php d isplays the transactions of a user as shown in an earlier screenshot, for example, when the administrator clicks on a user's link, such as h rawat, t o view the transactions of the user Harish Rawat. Transactions of the User !!              TEAM FLY PRESENTS
  2. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Records of user : T o get the details of the user $ user-id f rom the u ser_profile t able:
  3. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ?> Order No. Item No. Quantity Date Status
  4. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ?> Transactions of the Day T he script t ransaction_admin.php i s executed on the web server when the administrator clicks on the T ransactions l ink from any of the pages of the application. This page displays all the transactions of the day. The administrator can change the status of the transactions to S hipped , after the items are shipped to the user. The script code of t ransaction_admin.php l ooks like this:
  5. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com { $users[$user_count] = $row["user_id"]; $user_count++; } mysql_free_result($result) ; ?> T he following draws the HTML page shown in an earlier screenshot. Transactions of the day !!             T he script now creates a form for shipping with s hip_order.php a s the actioned script. TEAM FLY PRESENTS
  6. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com   Check to Ship Order User Order No. Item No. Quantity Status F or all users, display their transactions of the day:
  7. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ?>
  8. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Shipping the Order T he script s hip_order.php i s executed on the web server when the administrator clicks on the S hip Order b utton in the transactions page. This script changes the status of the transactions of the selected users from P ending t o S hipped . We begin by verifying that the administrator is already authenticated:
  9. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com $user= $row1["user_id"] ; F or each user, verify that the status of the user’s transaction needs to be changed from P ending t o S hipped a nd get all of today’s transactions of the user. The form variable $ $user w ill have a value O N , if the administrator had selected the user’s transactions for shipping in the transactions page. if(($$user) && ($$user == "ON")) { // Get all the pending transactions of the user that needs to be shipped if (!($result2 = mysql_db_query ($DB, "select * from transaction where user_id='$user' and date='$today' and status='Pending'"))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error())); exit() ; } $amount=0 ; C alculate the cost of all the items, purchased by the user: while (($row2=mysql_fetch_array($result2))) { G et the details of the item, which are then stored in the variable $ row3 : mysql_data_seek($result3, 0) ; mysql_data_seek($result4, 0) ; while (($row3 = mysql_fetch_array($result3)) || ($row3 = mysql_fetch_array($result4))){ if ($row3["item_no"] == $row2["item_no"]) break ; } if ($row3 == NULL){ DisplayErr("error in the tables\n") ; exit() ; } $amount = $amount + $row3["price"] * $row2["quantity"]; } U pdate the new balance and the account of the user: // Update all the transactions of the user as shipped if (!mysql_db_query($DB, "update transaction set status='Shipped' where user_id='$user' AND date='$today'")) { DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error())); exit() ; } // Update the account of the user if (!mysql_db_query($DB,"UPDATE user_profile SET account_balance = account_balance-$amount where user_id='$user'")){ TEAM FLY PRESENTS
  10. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error())); exit() ; } F ree the memory associated with $ result v ariable. mysql_free_result($result2) ; } } F inally, redirect the client browser to the transactions page. header("Location:http://$HTTP_HOST/$DOCROOT/transaction_admin.php"); ?> Search for Users T he script s earch_user.php i s executed on the web server when the administrator enters a search keyword and clicks on the S earch b utton on the Search for Users page. The script s earch_user.php i s called with the form variable k eyword , containing the search text. We begin, as always, by verifying that the administrator is already authenticated:
  11. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com User Search Results !!             G enerate a form with action as v iew_transactions.php : D isplay the list of users matching the search criteria. View transaction details for... TEAM FLY PRESENTS
  12. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com C reate a submit button with the name as u serid , and value as the user_id of the user. The administrator clicks on this button to view the transactions of the user.
  13. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Case Study 2: Phorum Discussion Software P horum is a bulletin board application written in PHP. It allows users to post and read messages on the Web, storing these messages in a relational database. The messages are organized according to their conversation threads, as with some popular Usenet news readers. It was developed as an Open Source project. Information about Phorum can be found at h ttp://phorum.org/ . Phorum, like a lot of software applications, was born of necessity. The authors' web site needed an area where users could exchange ideas. At that time, there were a lot of Perl-based solutions that used files to store all their data. The problem with this approach was that the rest of the site was using PHP and, this area would also need to use PHP if it was to integrate well with the rest of the site. In addition, having thousands of files on the server could be a headache. There were horror stories of operating systems having difficulties with too many files in a single directory. Therefore, Phorum also set itself the goal of storing its data on a SQL database server. This would allow retrieval of the data in all sorts of formats and in any order with only minimal changes. Fortunately, PHP's database support is very good. Integration with the rest of the site and good database support made PHP an easy choice for this project. The next hurdle was to develop the project quickly. Once the initial groundwork was laid, other users were invited to try it out, and in return they could use the code. Amazingly, this increased the speed of production exponentially. Within one month, Phorum became a product that, we believed, was superior to any of the Perl solutions available. Soon, the licensing for Phorum was changed from informal sharing of code to that of the GNU General Public License (GPL). This meant that anyone could use Phorum for their website. Phorum grew slowly in popularity until version 3 was released, and there was a large increase in the number of users. Now there are sites running Phorum that receive over 500 new messages a day. Many of these new users are people who, like the Phorum originators, are finding the Perl/file-based solutions inadequate for this discussion area of this size. Phorum is currently used on some very high profile sites. A list of these sites is available at the Phorum web site ( http://www.phorum.org ). You can download the latest version of Phorum there, and also view demos of the application and receive support for it. Why PHP? T he reasons why PHP is so good for Phorum are the same reasons that it is good for most web-based applications: Support for many different databases ❑ TEAM FLY PRESENTS
  14. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Ability to embed PHP within HTML pages ❑ Exceptional error handling features ❑ Database Support P HP has support for a wide range of databases. At the last count, PHP natively supported over 15 databases. In addition it supports several varieties of ODBC, or Open DataBase Connectivity (iODBC and OpenLink ODBC, as well as custom and unified ODBC libraries). This makes it a natural choice if we are planning to perform any kind of database operations on our website. Compare this with, for example, Microsoft's Active Server Pages (ASP), which relies entirely on ODBC and/or OLE DB for database access. For those not familiar with ODBC, this is how it works from a programmer's prospective: a programmer writes code in a given language (in our case, PHP). That code uses a standard set of functions – the ODBC functions for that language. These functions internally use code (usually written by the database manufacturer) that implements the standard set of ODBC functions specified in the ODBC API. ODBC uses a Driver Manager to manage drivers for the available data sources; these drivers process the function calls from the ODBC API and translate standard SQL syntax into the native SQL syntax of the database. A graphical representation of this architecture would look like this: ***Insert figure (2025_03_06.cdr?) W ith PHP supporting so many databases natively, we can avoid the whole of the ODBC level entirely. When writing code for a database that PHP supports natively, we are in effect using the functions supplied by the manufacturer for use with that database. As you can see, there is a whole step removed from the equation there. By removing that extra level, native support allows faster database access and is able to utilize additional features specifically related to the actual database server being used. This is because ODBC has a standard interface, which must be followed when working with the database. By supporting each database independently, PHP can communicate with the database on its own terms. This allows us to use features in a database system that may not be available through the ODBC API. The downside of this is that applications written for a particular database cannot be easily ported to other database systems. Phorum tackles this issue by implementing a loose abstraction layer around the most popular database systems in use with PHP. The difference between this and ODBC is that in our abstraction layer we have made concessions to allow the code to use the special features of each database system. This abstraction layer will be discussed later. PHP Embedded in HTML T he second advantage of PHP is the ability to embed the PHP code right inside HTML files. This makes it easy to write both the PHP and the HTML. This ability to embed PHP code within an HTML page means that we don't have to deal with long print s tatements to output HTML; in fact, we can completely separate our PHP code from PHP. New web developers and those who just want scripts on their sites may find Perl's pure programming style a bit confusing and intimidating. With PHP, large blocks of HTML can be separated from the PHP and this is likely to be much more comprehensible to the novice coder. Suppose we had a program that would allow users to input color names and then print an HTML table containing the hexadecimal values for those colors for use in HTML. This table will have two columns – one each for the color name and the hex value – with a row for each color. The differences in an TEAM FLY PRESENTS
  15. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com e mbedded language like PHP and non-embedded languages (like regular Perl) could make all the difference in the world to an inexperienced programmer. The HTML code could look like this: Color Hex Value Red #FF0000 Blue #0000FF Green #00FF00 With Perl, we would first have to parse any information passed to the script from the web server. Next the code would look something like this: print "\n"; print " \n"; print " Color\n"; print " Hex Value\n"; print " \n"; print " \n"; print " $color_name_1\n"; print " $color_val_1\n"; print " \n"; print " \n"; print " $color_name_2\n"; print " $color_val_2\n"; print " \n"; print " \n"; print " $color_name_3\n"; print " $color_val_3\n"; print " \n"; print "\n"; N ot very readable, is it? While we can improve it slightly (using a 'here document' to print out long sections of pure HTML), we will still need p rint s tatements for the lines containing variables. The ironic part is that very similar code would work also in PHP. However, the following code is much easier on the eye: Color Hex Value TEAM FLY PRESENTS
  16. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com A s you can see, the PHP only comes into play when we need it. Moreover, with PHP, the variable values are already available when the script starts: we don't need to parse the form input to abstract them. Exceptional Error Handling M ost people agree that PHP has some of the most helpful error messages in web application building today. PHP gives advanced error messages that help the developer find the problem quickly. A typical PHP error message looks like this: Fatal error: Call to unsupported or undefined function foo() in /usr/web/bar.php on line 8 T his error message tells us the name of the function called, the filename of the script in which the error occurred (if it were in an included file, it would give the included file's name), and the line on which the error occurred. In this case, a function called f oo() w as called on line 8 of / usr/web/bar.php a nd that the function is not defined. With so much information, a developer can quickly and easily track down the problem. PHP error messages and debugging are discussed in more detail in Chapter 19. How Phorum Works A s we said at the start of the chapter, Phorum is an application that allows users of a web site to post messages to a bulletin board and to read and search through earlier postings. Messages are stored in a relational database (using wrapper functions to abstract database-specific functions, so that if the database is upgraded, only this abstraction layer needs to be changed – we don't need to go through the entire code, changing every single database function). The messages are organized according to conversation threads – that is, when a user replies to a message, the reply is added to the same thread as the original posting. We will first look at the graphical interface presented to the user, before looking at some of the code behind the application. Interface Overview T he Phorum user interface consists of five pages. The first page which users see is the forum list page. This file is named i ndex.php3 by default and lists the name and a description of each forum as well as the total number of posts in the forum and the date of the most recent post: TEAM FLY PRESENTS
  17. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ****Insert 2963_24_01.bmp F rom here, users can enter a forum by clicking on its name. This will take them to the message list page, which by default is called l ist.php3 . This page shows a list of the messages in this forum. This may contain all the messages, or just the first messages of each conversation. The default setting for this is decided by the administrator and can be different for each forum. Users can select a link that will allow them to specify their own preference for this setting. The setting for each user is stored in a cookie, which is accessed each time they return to the forum. TEAM FLY PRESENTS
  18. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com N ext, a user may select to read a message by clicking on a topic of interest. This leads to a page where individual messages may be read, named r ead.php3 by default. This page displays the subject, the author's name (which is an email link if an email address was provided), and, of course, the body of their message. Below this is the full list of all the messages in the conversation. The user can click on any of these and be taken to that message. In all cases, the entire conversation thread is displayed, as opposed to some other systems, which only show the messages from the current one down. Below the thread list is a form that can be used to add a message to this thread. The message would appear in the list below the message that is currently being viewed. TEAM FLY PRESENTS
  19. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com U sers can start new threads by clicking on a link that takes them to a page which by default is named p ost.php3 . On this page is a form similar to the one on the r ead.php3 p age. There are input boxes where users can type their name and email address and the subject and body of the message they want to post. There is also a checkbox which allows users to specify that any responses to their messages should be sent to the specified email address: TEAM FLY PRESENTS
  20. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com I f all is well, the user will be sent back to the message list after clicking on the P ost b utton. If an error occurred, the post form will be shown again with a message explaining the error. All of the information entered will be preserved. The final page which users can access is the search page ( search.php3 ). This page illustrates another advantage of using a database. Searches can be done quickly and can be quite complex. However, the search facility built into Phorum is quite basic: it does not attempt to determine a rating or order messages according to their relevance to the search criterion. A user enters some words and a list of the subjects of messages containing those words is returned, sorted by date in reverse order. A user can read a message by clicking on its subject. There is also a short summary of the message body listed on the page: TEAM FLY PRESENTS
ADSENSE
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản