Remember what you did with the Powers field? Ditto all of that for the Enemies field. The only difference here is that if there are no values in the $charlist variable (list of all characters except the chosen character), the Enemies field will not show up on the form.
Enemies: (Ctrl-click to select multiple enemies)
; $value) { echo $value n ; } If the character entry form is not in Update mode, then the script will hide the Delete Character button (you can t delete a character you haven t created yet): Finally, the character ID is not passed through any form fields, so you create a hidden field to hold that information. You need that ID if you are going to update an existing character. Of course, if you are creating a new character, then the ID will be created for you when you insert all the appropriate data. > Summary Whew! This chapter covered a lot of ground. You learned about how to plan the design of your application, including database design. You learned how to normalize your data, so that it can easily be linked and manipulated. You created a brand new database for your Web site and started building your Web site by creating tables and creating the Web application needed to access and update those tables. Congratulations! You just created your first, fully functioning Web application with a relational database backend. (That s going to look so good on your resume.) This chapter is only the beginning, however. With the knowledge you gained here, you can create almost any application you desire. Here are some examples of what you could do: . Content Management (CMS): Create a data entry systems that will allow users and administrators to alter the content of the Web site and your database without knowing any HTML. 323 Building Databases We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.
Posted in php | No Comments »
Tuesday, October 30th, 2007
Posted in php | No Comments »
Monday, October 29th, 2007
AND c.lair_id = l.id . AND c.id = $char ; $result = mysql_query($sql) or die(mysql_error()); $ch = mysql_fetch_array($result); Once the script determines there was a record retrieved, it alters the default variables to reflect the edited document. The background is green, and you are Updating rather than Creating. if (is_array($ch)) { $subtype = Update ; $tablebg = #EEFFEE ; $subhead = Edit data for . $ch[ alias ] . and click $subtype Character. ; The next SQL statement retrieves all powers associated with this character. All you really need is the ID so that you can create a $powers array with each element containing the word selected. This will be used in the Powers field on the form, so that each power assigned to the character will be automatically selected. $sql = SELECT p.id . FROM char_main c . JOIN char_power p . JOIN char_power_link pk . ON c.id = pk.char_id . AND p.id = pk.power_id . WHERE c.id = $char ; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $powers[$row[ id ]] = selected ; } } Now you do exactly the same thing with the character s enemies. Note the similarity in this SQL statement to the one in charlist.php. The only difference is that you want only the enemies that match your character. // get list of character s enemies $sql = SELECT n.id . FROM char_main c . JOIN char_good_bad_link gb . JOIN char_main n . ON (c.id = gb.good_id AND n.id = gb.bad_id) . OR (n.id = gb.good_id AND c.id = gb.bad_id) . WHERE c.id = $char ; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $enemies[$row[ id ]] = selected ; } } 321 Building Databases Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.
Posted in php | No Comments »
Sunday, October 28th, 2007
charedit.php This file does double-duty, so it s a little longer. But a lot of it is HTML, and much of what it does you have already done before, so this shouldn t be too difficult. The default functionality of this page is New Character mode. If there is a value in $char other than 0, the script will pull the data and change the default values. if (!isset($_GET[ c ]) || $_GET[ c ] == || !is_numeric($_GET[ c ])) { $char= 0 ; } else { $char = $_GET[ c ]; } $subtype = Create ; $subhead = Please enter character data and click . $subtype Character. ; $tablebg = #EEEEFF ; Next, the script gets all the powers, and puts them into an array to be accessed later (when building the power select field on the form). $sql = SELECT id, power FROM char_power ; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $pwrlist[$row[ id ]] = $row[ power ]; } } All characters except the chosen character will be pulled from the database to be used for the Enemies field. If the character ID is not valid, then all characters will be pulled for the Enemies field. $sql = SELECT id, alias FROM char_main WHERE id != $char ; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result) > 0) { $row = mysql_fetch_array($result); $charlist[$row[ id ]] = $row[ alias ]; } If there is a character id, the script attempts to pull the data from the database. This SQL statement is also a JOIN, although the JOIN keyword is not used. You can identify such a JOIN because there are two or more tables, and the WHERE keyword is matching columns from each of the tables. The JOIN in this case is implied. Once all the tables are joined, all the appropriate fields are pulled as long as the character ID in the character table matches $char. If there is no match, no records will be returned. If there is a match, one record is returned and the row is stored in $ch. if ($char != 0 ) { $sql = SELECT c.alias, c.real_name AS name, c.align, . l.lair_addr AS address, z.city, z.state, z.id AS zip . FROM char_main c, char_lair l, char_zipcode z . WHERE z.id = l.zip_id . 320 Chapter 10 You want to have a cheap webhost for your apache application, then check apache web hosting services.
Posted in php | No Comments »
Saturday, October 27th, 2007
$table .=
; $table .= Alias ; $table .=
; $table .= Name
Alignment
Powers
; $table .=
Enemies
; Next, you alternate the background colors of the table, to make it a little easier to read. // build each table row $bg = ; while ($row = mysql_fetch_array($result)) { $bg = ($bg== F2F2FF ? E2E2F2 : F2F2FF ); Remember the power and enemy arrays you built earlier? You use the character s ID to grab the list of values and put them into a variable to be inserted shortly into the appropriate table cell. $pow = ($powers[$row[ id ]]== ? none :$powers[$row[ id ]]); if (!isset($enemies) || ($enemies[$row[ id ]]== )) { $ene = none ; } else { $ene = $enemies[$row[ id ]]; } The table is built, row by row, inserting the appropriate data in each cell; then it s closed: $table .=
.
. $row[ alias ].
. $row[ name ] .
. $row[ align ] .
. $pow .
.
. $ene .
; } $table .=
; Just for kicks, and to make them more visible, the script changes the color of the good and evil values in the table. This isn t necessary, but it makes the values pop out more. $table = str_replace( evil , evil , $table); $table = str_replace( good , good , $table); This variable contains either the
tag you created earlier or the table of character data. It s inserted in the page here. echo $table; 319 Building Databases
You need excellent and relaible webhost company to host your web applications? Then pay a visit to
Inexpensive Web Hosting services.
Posted in php | No Comments »
Friday, October 26th, 2007
duplication. In other words, if you have a row with good_id=3 and bad_id=7, then good_id=7 and bad_id=3 must be considered a duplicate. There is no way to prevent that in MySQL using primary keys, so you must take care of that contingency in your code. You do that in a couple of places. In this instance, you are combining two queries in one. The first one grabs all instances of each character where the character s ID is in the good_id field and his enemies IDs are in the bad_id field. The second part of the ON statement reverses that, and pulls all instances of each character where the character s ID is in the bad_id field and his enemies IDs are in the good_id field. This does not prevent reverse duplication (that is handled elsewhere), but it does make sure you have grabbed every possible link to a character s enemy. This code is virtually identical to the multidimensional powers array. This time, you are creating a multidimensional array of each character and that character s enemies. You then implode the enemies list and store it in the $enemies array, using the character s ID as the array index. $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $e[$row[ id ]][] = $row[ alias ]; } foreach ($e as $key => $value) { $enemies[$key] = implode( , , $value); } } You are going to build a table of characters in a moment. In case there are no characters to display (as when you first tested your charlist.php page), you want to display a No characters message. This code builds the $table variable (even though it doesn t contain an actual table) using a
tag. If any characters do exist, this variable will be overwritten with an actual table of data. $table =
No characters . currently exist.
?> Next is another simple SQL SELECT, pulling the appropriate data: character s id, alias, real name, alignment, and address info. Note the $order array. You set that value at the beginning of this page, using the $_GET value o in the URL. This is where it s used to sort the characters. $sql = SELECT id, alias, real_name AS name, align . FROM char_main ORDER BY . $order[$ord]; $result = mysql_query($sql) or die(mysql_error()); You are now building up the table of characters, as long as you returned at least one record from the database. Note the first three columns links. They refer back to this same page, adding the ?o=x parameter. This will re-sort the data and display it sorted on the column the user clicked. if (mysql_num_rows($result) > 0) { $table =
; 318 Chapter 10 Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.
Posted in php | No Comments »
Thursday, October 25th, 2007
Notice the use of aliases for the tables. The character table is c, the power link table is pk, and the power table is p. This allows you to refer to the appropriate columns with a shorter syntax (for example pk.char_id instead of char_power_link.char_id). It is not necessary to use table.column syntax if the column name is unique across all tables. However, it is a good practice to keep so that you are always aware of which data you are accessing. It is required, of course, for column names that are duplicated across multiple tables (such as id). Some might recommend that you always use unique names for all of your fields, but we prefer the practice of naming all primary keys id and using proper table.column syntax in SQL queries. Next, you create a multidimensional array. That s fancy talk for an array with more than one index. This one is two-dimensional. Think of a two-dimensional array as being like a spreadsheet, and it isn t difficult to understand. if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $p[$row[ id ]][] = $row[ power ]; } The trick here is that you have multiple powers for the same id. By adding [] to the $p array, a new array item is created for each row that has the same id. The end result is that you have a $p array of x characters, each element of which contains a $p[x] array of y powers. That is a multidimensional array. Now you go back through the temporary array $p, and pull out each array that it holds. The $key variable contains the character id, and $value contains the array of that character s powers. You then implode the powers into a comma-separated list of powers, and store that in the $powers array, using the character ID ($key) as the array index. You end up with an array that contains a list of powers for each character. foreach ($p as $key => $value) { $powers[$key] = implode( , , $value); } Oh boy, another JOIN. This one is similar to the previous M:N query, with a couple of exceptions. First of all, you are linking the character table twice. You can see that you are creating two instances of that table, one called c for character and one called n for nemesis. This distinction is very important. $sql = SELECT c.id, n.alias . FROM char_main c . JOIN char_good_bad_link gb . JOIN char_main n . ON (c.id = gb.good_id AND n.id = gb.bad_id) . OR (n.id = gb.good_id AND c.id = gb.bad_id) ; The other exception is the ON statement. You have characters that you are attempting to link to other characters as enemies. Call them opponents, or nemeses, or whatever. Typically, you expect good versus evil and vice versa. However, you are allowing any character to be the enemy of any other character. That makes linking more interesting because you are using a table with a bad_id and a good_id. If you have two evil characters that are enemies, which one gets stored in the good_id column? The answer is that it doesn t matter. What you want to do is to make sure that you not only don t have any duplicates in the char_good_bad_link table, but also that you don t have what we call reverse 317 Building Databases We recommend high quality webhost to host and run your jsp application: christian web host services.
Posted in php | No Comments »
Wednesday, October 24th, 2007
How It Works You created two different files in this exercise, so we re going to take them apart and look at them individually here. charlist.php The charlist.php page has an optional parameter that can be passed: ?o=x, where x is 1, 2, or 3. This code retrieves that variable if it exists, and converts it to the appropriate value if necessary. If some smart-aleck types o=4 in the browser, the code returns 3. If no value or a bad value is passed, it will default to 1. The value is stored in $ord. if (isset($_GET[ o ]) && is_numeric($_GET[ o ])) { $ord = round(min(max($_GET[ o ], 1), 3)); } else { $ord = 1; } $order = array(1 => alias ASC , 2 => name ASC , 3 => align ASC, alias ASC ); This value determines which column the character display will be sorted on: 1 is by alias, 2 is by real name, and 3 is by alignment and then alias. You will use the value $ord as the key to your order array, which will be appended to the appropriate SQL statement later. Make a connection, and choose a database. You know the drill by now. $conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die( Could not connect to MySQL database. . mysql_error()); mysql_select_db(SQL_DB, $conn); Ah . . . your first JOIN. This SELECT statement might look confusing to the uninitiated, but it is not that complicated. First, look at the JOIN statements. You are joining three tables, using the char_power_link table to link the char_power table and the char_main table. This is a many-to-many (M:N) relationship. You define how they are joined with the ON statement. As you can see, you are linking up the character table to the link table using the character id, and you re linking the power table to the link table using the power id. With that link established, you can see that you are grabbing the character s ID and the powers assigned to each character. $sql = SELECT c.id, p.power . FROM char_main c . JOIN char_power p . JOIN char_power_link pk . ON c.id = pk.char_id AND p.id = pk.power_id ; $result = mysql_query($sql) or die(mysql_error()); 316 Chapter 10 Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.
Posted in php | No Comments »
Tuesday, October 23rd, 2007
Figure 10-5 315 Building Databases Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.
Posted in php | No Comments »