Professional PHP Programming phần 8 pptx

86 225 0
Professional PHP Programming phần 8 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

} // End of while Finally we redirect the client to user_admin.php page. header("Location:http://$HTTP HOST/$DOCROOT/user admin.php"); ?> Viewing the Transactions of a User The script view_transactions.php displays the transactions of a user as shown in an earlier screenshot, for example, when the administrator clicks on a user's link, such as hrawat, to view the transactions of the user Harish Rawat. <?php require 'functions.php'; Verify that the administrator is already authenticated: if(!authenticateUser($cookie user, $cookie passwd)){ header("Location:http://$HTTP HOST/$DOCROOT/admin.htm"); exit(); } // Connect to the Database if (!($link = mysql pconnect($DB SERVER, $DB LOGIN, $DB PASSWORD))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql_errno(), mysql_error())); exit() ; } ?> <HTML> <HEAD> <TITLE>Transactions of the User !! </TITLE> </HEAD> <BODY BGCOLOR="#F0F3D1"> <DIV ALIGN="left"> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="90%"> <TR> <TD WIDTH="50%" ALIGN="right"><IMG SRC="wrox.gif" ALT="WroxWare" WIDTH="228" HEIGHT="70"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </TD> <TD WIDTH="50%"><IMG SRC="Shopping Mall.gif" ALT="Shopping Mall" WIDTH="318" HEIGHT="87"></TD> </TR> </TABLE> </DIV> <DIV ALIGN="center"><CENTER> <TABLE BORDER="0" CELLSPACING="1" WIDTH="100%" ALIGN="center"> <TR> <TD WIDTH="25%" ALIGN="center"><A HREF="user admin.php"> <IMG SRC ="User records.gif" ALT="User Records" BORDER="0"></A> </TD> <TD WIDTH="25%" ALIGN="center"><A HREF="transaction admin.php"> <IMG SRC = "Transaction.gif" ALT="Today's Transactions" BORDER="0"></A> </TD> <TD WIDTH="25%" ALIGN="center"><A HREF="search user.htm"> <IMG SRC = "Search for user.gif" ALT="Search for user !" BORDER="0"></A> </TD> <TD WIDTH="25%" ALIGN="center"><A HREF="logout admin.php"> <IMG SRC = "Logout admin.gif" ALT="Logout !" BORDER="0"></A> </TD> </TR> </TABLE> </CENTER></DIV> <BR> <CENTER> <FONT COLOR="#804000" FACE="Sans Serif"><SMALL><STRONG> Records of user : <?php echo($userid); ?> </STRONG></SMALL></FONT><BR> To get the details of the user $user-id from the user_profile table: <?php /* Read records from table transaction to read Account Status */ if (!($result = mysql db query($DB, "select * from user profile where user id='$userid'" ))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } /* Read one record from the queried data */ if (($row = mysql fetch array($result))) { ?> Display the current account balance of the user and free the memory associated with $result variable: <FONT COLOR="#804000" FACE="Sans Serif"><SMALL><STRONG> <?php echo ("Current Account Balance : $"); echo ($row["account balance"]); ?> </STRONG></SMALL></FONT> <BR> <?php } mysql_free_result($result) ; // free memory associated with $result ?> <BR> <TABLE BORDER="1" CELLSPACING="0" WIDTH="80%" CELLPADDING="2"> <TR> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL><STRONG> Order No.</STRONG></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL><STRONG> Item No.</STRONG></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL><STRONG> Quantity</STRONG></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL><STRONG> Date</STRONG></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL><STRONG> Status</STRONG></SMALL></FONT></TD> </TR> <?php Get all the transactions of the user $user-id from the transaction table. The variable $result contains all the rows of the transaction table for which the value of the user_id column is $userid. /* Read records from table transaction to read user names */ if (!($result = mysql db query($DB, "select * from transaction where user id='$userid'" ))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } Display all the transactions of the user, and the details of each transaction: /* Read one record at a time from the queried data */ while ($row = mysql fetch array($result)) { ?> <TR> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL> &nbsp;<?php echo($row["order no"]);?></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL> &nbsp;<?php echo($row["item no"]); ?></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL> &nbsp;<?php echo($row["quantity"]); ?></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL> &nbsp;<?php echo($row["date"]); ?></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL> &nbsp;<?php echo($row["status"]); ?></SMALL></FONT></TD> </TR> <?php } // End of while ?> </TABLE> </CENTER> </BODY> </HTML> Transactions of the Day The script transaction_admin.php is executed on the web server when the administrator clicks on the Transactions link 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 Shipped, after the items are shipped to the user. The script code of transaction_admin.php looks like this: <?php require 'functions.php'; As usual, we begin by verifying that the administrator is already authenticated, but also store the current date in a variable, $today: if(!authenticateUser($cookie user, $cookie passwd)){ header("Location:http://$HTTP HOST/$DOCROOT/admin.htm"); exit(); } // Connect to the Database if (!($link = mysql pconnect($DB SERVER, $DB LOGIN, $DB PASSWORD))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } // Today’s Date $today = date("Y-m-d"); We then get the list of all the users from user_profile table: /* Read all records from table user profile to list all users */ if (!($result = mysql db query($DB, "select * from user profile" ))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } The script then creates an array $users containing the user-ids of all the users and then frees the memory associated with $result variable: // Initialize counter and create an array of all the users $user count = 0; while ($row = mysql_fetch_array($result)) { $users[$user count] = $row["user id"]; $user count++; } mysql free result($result) ; ?> The following draws the HTML page shown in an earlier screenshot. <HTML> <HEAD> <TITLE>Transactions of the day !!</TITLE> </HEAD> <BODY BGCOLOR="#F0F3D1"> <DIV ALIGN="left"> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="90%"> <TR> <TD WIDTH="50%" ALIGN="right"> <IMG SRC="wrox.gif" ALT="WroxWare" WIDTH="228" HEIGHT="70"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </TD> <TD WIDTH="50%"> <IMG SRC="Shopping Mall.gif" ALT="Shopping Mall" WIDTH="318" HEIGHT="87"> </TD> </TR> </TABLE> </DIV> <DIV ALIGN="center"><CENTER> <TABLE BORDER="0" CELLSPACING="1" WIDTH="100%" ALIGN="center"> <TR> <TD WIDTH="25%" ALIGN="center"> <A HREF="user admin.php"> <IMG SRC ="User records.gif" ALT="User Records" BORDER="0"> </TD> <TD WIDTH="25%" ALIGN="center"> <IMG SRC = "Transaction.gif" ALT="Today's Transactions" BORDER="0"> </TD> <TD WIDTH="25%" ALIGN="center"> <A HREF="search user.htm"> <IMG SRC = "Search for user.gif" ALT="Search for user !" BORDER="0"> </A> </TD> <TD WIDTH="25%" ALIGN="center"> <A HREF="logout admin.php"> <IMG SRC = "Logout admin.gif" ALT="Logout !" BORDER="0"></A> </TD> </TR> </TABLE> The script now creates a form for shipping with ship_order.php as the actioned script. <TABLE BORDER="0" CELLPADDING="3" CELLSPACING="0" WIDTH="95%"> <TR> <TD WIDTH="100%">&NBSP;<FORM METHOD="POST" ACTION="ship order.php"> <DIV ALIGN="center"><CENTER> <TABLE BORDER="0" CELLSPACING="0" WIDTH="80%" HEIGHT="63"> <TR> <TD WIDTH="17%" ALIGN="center" HEIGHT="36"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL><STRONG> Check to Ship Order</STRONG></SMALL></FONT></TD> <TD WIDTH="22%" ALIGN="center" HEIGHT="36"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL><STRONG> User</STRONG></SMALL></FONT></TD> <TD WIDTH="15%" ALIGN="center" HEIGHT="36"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL><STRONG> Order No.</STRONG></SMALL></FONT></TD> <TD WIDTH="14%" ALIGN="center" HEIGHT="36"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL><STRONG> Item No.</STRONG></SMALL></FONT></TD> <TD WIDTH="15%" ALIGN="center" HEIGHT="36"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL><STRONG> Quantity</STRONG></SMALL></FONT></TD> <TD WIDTH="17%" ALIGN="center" HEIGHT="36"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL><STRONG> Status</STRONG></SMALL></FONT></TD> </TR> For all users, display their transactions of the day: <?php for ($i=0;$i<$user count;$i++) { if (!($result = mysql db query($DB, "select * from transaction where user id='$users[$i]' AND date='$today' "))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } $new count = 0; while (($row = mysql fetch array($result))) { ?> Now we display checkboxes for each user, setting the name of the checkbox as the user_id of the particular user. The administrator can select the checkbox to change the status of the transactions of a user from Pending to Shipped. <TR> <TD WIDTH="17%" ALIGN="center" HEIGHT="19"><FONT FACE="Sans Serif"> <?php // Code to show Status check box once if ($new_count==0){ ?> <INPUT TYPE="checkbox" NAME="<?php echo($users[$i]) ?>" VALUE="ON"> <?php } // End of if ?> </FONT></TD> <TD WIDTH="22%" ALIGN="center" HEIGHT="19"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL> <?php // Code to ensure that name is displayed once if ($new count==0) { echo ($users[$i]); } ?> </SMALL></FONT></TD> We also display the details of the item: <TD WIDTH="15%" ALIGN="center" HEIGHT="19"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL> <?php echo ($row["order no"]); ?></SMALL></FONT></TD> <TD WIDTH="14%" ALIGN="center" HEIGHT="19"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL> <?php echo ($row["item no"]); ?></SMALL></FONT></TD> <TD WIDTH="15%" ALIGN="center" HEIGHT="19"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL> <?php echo ($row["quantity"]); ?></SMALL></FONT></TD> <TD WIDTH="17%" ALIGN="center" HEIGHT="19"> <FONT FACE="Sans Serif" COLOR="#804000"><SMALL> <?php echo ($row["status"]); ?></SMALL></FONT></TD> </TR> <?php $new count = 1; } // End of while } // End of for ?> </TABLE> Create a submit button with value Ship Order. The administrator clicks on this button, to change the status of the transactions of the selected users, from Pending to Shipped. <BR><INPUT TYPE="submit" NAME="ship order" VALUE=" Ship Order "> </TABLE> </CENTER></DIV> </FORM> </BODY> </HTML> Shipping the Order The script ship_order.php is executed on the web server when the administrator clicks on the Ship Order button in the transactions page. This script changes the status of the transactions of the selected users from Pending to Shipped. We begin by verifying that the administrator is already authenticated: <?php require 'functions.php'; if(!authenticateUser($cookie user, $cookie passwd)){ header("Location:http://$HTTP HOST/$DOCROOT/admin.htm"); exit(); } // Connect to the Database if (!($link = mysql pconnect($DB SERVER, $DB LOGIN, $DB PASSWORD))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } // Today’s Date $today = date("Y-m-d"); Get the list of all the users: // Get the list of all the users if (!($result1 = mysql db query ($DB, "select * from user profile"))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } Get all the rows of the book_shop table that are stored in the variable $result3: // Get all the information of the music and book shop if (!($result3 = mysql db query ($DB, "select * from book shop "))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } Get all the rows of the music_shop table that are stored in the variable $result4: if (!($result4 = mysql db query ($DB, "select * from music shop "))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } while (($row1 = mysql_fetch_array($result1))){ $user= $row1["user_id"] ; For each user, verify that the status of the user’s transaction needs to be changed from Pending to Shipped and get all of today’s transactions of the user. The form variable $$user will have a value ON, 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 ; Calculate the cost of all the items, purchased by the user: while (($row2=mysql_fetch_array($result2))) { Get 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"]; } Update 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'")){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } Free the memory associated with $result variable. mysql free result($result2) ; } } Finally, redirect the client browser to the transactions page. header("Location:http://$HTTP HOST/$DOCROOT/transaction admin.php"); ?> Search for Users The script search_user.php is executed on the web server when the administrator enters a search keyword and clicks on the Search button on the Search for Users page. The script search_user.php is called with the form variable keyword, containing the search text. We begin, as always, by verifying that the administrator is already authenticated: <?php require 'functions.php'; if(!authenticateUser($cookie user, $cookie passwd)){ header("Location:http://$HTTP HOST/$DOCROOT/admin.htm"); exit(); } // Connect to the Database if (!($link = mysql pconnect($DB SERVER, $DB LOGIN, $DB PASSWORD))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } Search in the user_profile table for rows whose user_id column is like $keyword. // Read records from table user profile to list matching users if (!($result = mysql db query($DB, "select * from user profile where user id like '%$keyword%'" ))){ DisplayErrMsg(sprintf("internal error %d:%s\n", mysql errno(), mysql error())); exit() ; } ?> [...]... WIDTH="60%" NOWRAP < ?php echo bgcolor($NavColor); ?>> < ?php echo $nav; ?> >   < ?php echo $lNewerMessages;?>... 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 < ?php < ?php < ?php < ?php < ?php < ?php echo $color name 1; ?> echo $color val 1; ?> echo $color... This ability to embed PHP code within an HTML page means that we don't have to deal with long print statements 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... < ?php echo $lNewerMessages;?>   |   < ?php echo $lOlderMessages;?>   Reading Messages When a user selects... application and receive support for it Why PHP? The 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 ❑ Ability to embed PHP within HTML pages ❑ Exceptional error handling features Database Support PHP has support for a wide range of databases At the last count, PHP natively supported over 15 databases In... write code that can read that format PHP already has a parser and it can generate variable values for us automatically simply by including a file within the Phorum application Writing a file that contains PHP code is in essence quite simple The following code will create a second file called hello .php that writes 'Hello World' out to the browser: < ?php $data ="< ?php\ n"; $data.=" echo \"Hello World\";\n";... build the table itself: >< ?php echo $nav; ?> < ?php if (!$MultiLevel || $$phcollapse) { include "./threads.inc"; } else { include "./multi-threads.inc"; } ?> < ?php } /* while ends */ ?> Summary In this chapter we wrote a complete real-life Shopping Cart application, using PHP in the middle tier This illustrates how PHP can be effectively used in the middle tier, to write web-based applications We have covered only the HTML files containing PHP code embedded... table containing all the forum names First we print the table header: >   < ?php echo $lAvailableForums;?> First we set a variable $empty with . PHP Embedded in HTML The 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. 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. items are shipped to the user. The script code of transaction_admin .php looks like this: < ?php require 'functions .php& apos;; As usual, we begin by verifying that the administrator

Ngày đăng: 12/08/2014, 13:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan