290 likes | 492 Views
Chapter 7. Building an Online Auction Site. config.php. Add: $config_auctionname = “Your name's Auction Site”; anywhere in the config.php. header.php. Change $config_forumsname into $config_auctionname Add the closing </div> after the <h1>BidTastic Auctions</h1>
E N D
config.php • Add: $config_auctionname = “Your name's Auction Site”; anywhere in the config.php
header.php • Change $config_forumsname into $config_auctionname • Add the closing </div> after the <h1>BidTastic Auctions</h1> <div id="header"> <h1>BidTastic Auctions</h1> </div>
functions.php • Before typing the code on page 225, you have to have typed the code from Chapter 5 on page 143-144: <?PHP FUNCTION pf_script_with_get($script) { $page = $script; $page = $page . "?“; FOREACH ($_GET as $key => $val) { $page = $page . $key . "=" . $val . "&“; } RETURN SUBSTR($page , 0 , STRLEN($page) - 1) ; } ?> • You do not need the rest of the code at the bottom of page 144 to page 146 You only need this portion of the code from the functions.php in Chapter 5.
bar.php • The code displayed on page 227 should be: <?PHP require("config.php"); $db = mysql_connect($dbhost, $dbuser, $dbpassword); mysql_select_db($dbdatabase, $db); $catsql = " SELECT * FROM categories ORDER BY cat ASC; " ; $catresult = mysql_query($catsql); echo "<h1> Categories </h1>" ; echo "<ul>" ; echo "<li> <a href = 'index.php'> View All </a> </li>"; while ($catrow = mysql_fetch_assoc($catresult)) { echo "<li> <a href = 'index.php?id=" . $catrow['id'] . "'>" . $catrow['cat'] . "</a></li>"; } echo "</ul>"; ?>
index.php • The code displayed at the bottom of page 229 should be: echo "<tr>“; if ($imagenumrows == 0) { echo "<td> No image </td>“; } else { $imagerow = mysql_fetch_assoc($imageresult); echo "<td> <img src = '" . $imagerow['name'] . "' width = '100'></td>" ; } • If you store your code in a sub-folder in your public_html, you have to insert this sub-folder before the $imagerow['name'] . For example: If your sub-folder is “auction”: echo “<td> <img src = ‘” . “/auction/” . $imagerow[‘name’] . “’ width = '100'></td>” ;
index.php • You have to make sure that there is a WHITE SPACE in the followings: Before the LIMIT in the " LIMIT 1"; in the $imagesql = "select * from images where item_id =" . $row['id'] . " LIMIT 1"; Before the GROUP in the " GROUP BY item_id;"; in the $bidsql = "select item_id, MAX(amount) AS highestbid, COUNT(id) AS numberofbids FROM bids WHERE item_id = " . $row['id'] . " GROUP BY item_id;";
itemdetails.php • The code displayed in middle of page 235 should be: IF ($imagenumrows == 0) { echo "No images"; } else { while ($imagerow = mysql_fetch_assoc($imageresult)){ echo "<img src = " . $imagerow['name'] . "' width = '200'>" ; } } If you store your code in a sub-folder in your public_html, you have to insert this sub-folder before the $imagerow['name'] . For example: If your sub-folder is “auction”: echo “<img src = ‘” . “/auction/” . $imagerow[‘name’] . “’ width = ‘200‘>” ;
index.php • Add brackets “(...)” to the code displayed at the bottom of page 228 should be: if($validid == 0) { $sql = "select items.* from items where dateends > NOW()"; } else { $sql = "select * from items where ( dateends > NOW() AND cat_id = " . $validid . “ ) ;" ; }
newitem.php • The code displayed at the bottom of page 247 should be: if($validdate == TRUE) { $concatdate = $_POST['year'] ."-" . sprintf("%02d" , $_POST['month']) ."-" . sprintf("%02d" , $_POST['day']) ."-" . $_POST['hour'] ."-" . $_POST['minute'] .":00";
addimages.php • The code displayed at the bottom of page 253should be: else { $uploaddir = "/home/a3352886/public_html/"; $uploadfile = $uploaddir . $_FILES['userfile']['name']; Replace this a3352886 with your domain username (a#######) that can be seen in your account information
addimages.php • Change the permission of your public_html folder into “777” (allow read, write, execute for everybody). How? Step 4. Click this “check” icon Step 3. Check all of these 9 boxes Step 2. Click this “Chmod” button Step 1. Check this box for the “public_html”
newitem.php • The code displayed at the bottom of page 244 should be: <form action="<?php echo pf_script_with_get($_SERVER['SCRIPT_NAME']); ?>" method="post"> <TABLE> <?PHP $catsql = "select * from categories ORDER BY cat;"; $catresult = mysql_query($catsql); ?> <tr> <td> Category </td> <td> <select name = "cat">
$_SERVER['SCRIPT_NAME'] • Every time you see “$SCRIPT_NAME” you have to replace it with $_SERVER[‘SCRIPT_NAME’] • Example: In the login.php: <form action="<?php echo pf_script_with_get($_SERVER['SCRIPT_NAME']); ?>" method="post"> <TABLE> <TR> <TD> Username </TD> <TD> <input type="text" name="username"></TD> </TR>
$_SERVER[‘HOST_NAME’] • Every time you see “$HOST_NAME” you have to replace it with $_SERVER[‘HOST_NAME’]
itemdetails.php • If your itemdetails.php cannot recognize “too low bids”, use the following code: ... if($_POST['submit']) { if (is_numeric($_POST['bid']) == FALSE) { header("Location: ". $config_basedir . "itemdetails.php?id=" . $validid . "&error=letter"); } $theitemsql = "select * from items where id = " . $validid . ";" ; $theitemresult = mysql_query($theitemsql); $theitemrow = mysql_fetch_assoc($theitemresult); $checkbidsql = "select item_id, max(amount) AS highestbid, count(id) AS number_of_bids from bids where item_id = " . $validid . " GROUP BY item_id;" ; $checkbidresult = mysql_query($checkbidsql); $checkbidnumrows = mysql_num_rows($checkbidresult); if ($checkbidnumrows == 0) { if ($theitemrow['startingprice'] > $_POST['bid']) { header("Location: " . $config_basedir . "itemdetails.php?id=" . $validid . "&error=lowprice#bidbox"); } ELSE { $inssql = "INSERT INTO bids(item_id, amount, user_id) VALUES (" . $validid . ", " . $_POST['bid'] . ", " . $_SESSION['USERID'] . ");" ; mysql_query($inssql); header("Location: " . $config_basedir . "itemdetails.php?id=" . $validid) ; } } else { $checkbidrow = mysql_fetch_assoc($checkbidresult); if ($checkbidrow['highestbid'] > $_POST['bid']) { header("Location: " . $config_basedir . "itemdetails.php?id=" . $validid . "&error=lowprice#bidbox"); } ELSE { $inssql = "INSERT INTO bids(item_id, amount, user_id) VALUES (" . $validid . ", " . $_POST['bid'] . ", " . $_SESSION['USERID'] . ");" ; mysql_query($inssql); header("Location: " . $config_basedir . "itemdetails.php?id=" . $validid) ; } } } ELSE { require("header.php"); ... (the rest of the code is the same)
functions.php FOREACH ($_GET as $key => $val) { $page = $page . $key . "=" . $val . "&"; } This $_GET will store ALL of the form controls (as array indices) and the values returned by those controls (as array elements) This FOREACH ($_GET as $key => $val) will retrieve EVERY form control and the value returned by that control
functions.php RETURN SUBSTR($page , 0 , STRLEN($page) - 1) ; SUBSTR() will return a portion of a string SUBSTR(string, starting point, number of characters) SUBSTR(“abcdef” , 0 , 3) will return “abc” SUBSTR(“abcdef” , 1 , 2) will return “bc” SUBSTR(“abcdef” , 2 , 1) will return “c”
index.php MAX(field_name) MySQL function that returns the HIGHEST VALUE in a field that has been retrieved amount 1 2 3 2 1 MAX(amount) will return 3
index.php COUNT(field_name) MySQL function that returns the number of records in a field that have been retrieved amount 1 2 3 2 1 COUNT(amount) will return 5
index.php sprintf(format, object to be formatted) sprintf() will format a certain object based on a certain format sprintf('%.2f' , 7000) will return 7000.00 ‘%.2f’ will format a number into a decimal format with 2 fraction digits
itemdetails.php UNIX_TIMESTAMP(date) MySQL function that converts a certain date into a timestamp (i.e., the number of seconds after 1/1/1970 00:00:00) Timestamp is useful for doing time arithmetic (comparing times, adding times, subtracting times)
itemdetails.php MKTIME() PHP function to retrieve current time and convert that into a timestamp (i.e., the number of seconds after 1/1/1970 00:00:00)
login.php IF(X == value_1) { Action 1; } ELSEIF (X == value_2) { Action 2; } ELSE { Action 3; } SWITCH(X){ Case value_1: Action 1; Break; Case value_2: Action 2; Break; Default: Action 3; Break; }
newitem.php Sprintf(“%02d”, 1) will return 01 “%02d“ will append/add 0s before the number to have a 2 digit number
newitem.php mysql_insert_id(); will return the last value automatically generated by the MySQL server
addimages.php <input type = "hidden" name = "MAX_FILE_SIZE" value = "3000000"> is used to create a “hidden textbox” to store a certain information in the form. The information must be specified in the “value” attribute
addimages.php <input name = "userfile" type = "file"> is used to create the “input file” control to allow the user to browse and to find a particular file in the local machine To retrieve the value of a certain attribute from the file retrieved by the “input file” control, we use the $_FILES['userfile']['name'];