Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 86 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
86
Dung lượng
2,47 MB
Nội dung
} // 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"> </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"> TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com <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 TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ?> <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> <?php echo($row["order_no"]);?></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL> <?php echo($row["item_no"]); ?></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL> <?php echo($row["quantity"]); ?></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL> <?php echo($row["date"]); ?></SMALL></FONT></TD> <TD WIDTH="20%"><FONT COLOR="#804000" FACE="Sans Serif"><SMALL> <?php echo($row["status"]); ?></SMALL></FONT></TD> </TR> <?php } // End of while TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ?> </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)) TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com { $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"> </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. TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com <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){ TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ?> <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> TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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))){ TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com $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'")){ TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 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() ; } ?> TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... WIDTH="60%" NOWRAP < ?php echo bgcolor($NavColor); ?>> < ?php echo $nav; ?> > < ?php echo $lNewerMessages;?>... very similar code would work also in PHP However, the following code is much easier on the eye: Color Hex Value TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com < ?php < ?php < ?php < ?php < ?php < ?php echo $color_name_1;... 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... 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... 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 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... BORDER="0"> Generate a form with action as view_transactions .php: < ?php while (($row = mysql_fetch_array($result))) { ?> Display the list of users matching the search criteria View transaction details for TEAM FLY PRESENTS Simpo PDF... table containing all the forum names First we print the table header: > < ?php echo $lAvailableForums;?> TEAM FLY PRESENTS Simpo PDF Merge and Split Unregistered . 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