Archive for October, 2007

. Maintain a database of users visiting your (Shared web hosting)

Wednesday, October 31st, 2007

. Maintain a database of users visiting your site: You can enable user authentication, e-mail your users to give them exciting news, sign them up for newsletters, and so on. . Create an online e-commerce site: Create shopping carts where users can store the merchandise they will purchase. (This can be daunting many choose to use a third-party shopping cart application.) . Create an online discussion forum where your users can go to discuss how wonderful your site looks! These are just a few ideas. In fact, you are going to see how to do each of these things over the course of upcoming chapters. With a little imagination, you can come up with solutions to almost any problem you might face in building your site. If any of the ideas presented in this chapter are difficult for you to grasp, that s okay. It is a lot of material, especially if you are a beginning programmer. The great thing about a book is that you can keep coming back! You will also be revisiting many of these concepts in later chapters. For example, in Chapter 16 where you learn to build your own forum, you will go through database normalization again on a new set of databases. You will also have many more opportunities to create SQL queries, some familiar and some new. For now, take some time to play with your new toy, the Character Database. You have the basic knowledge for creating even the most complex sites. You have the first incarnation installed on your server. Now all you need to do is let all of your friends and family know about your cool new site. If only you knew how to send e-mails using PHP. Well, we ll handle that in Chapter 11. Exercises See how you might accomplish the following tasks: 1. Add a costume description field to the character record, and provide a way to modify the costume description. 2. Modify the character listing to display the characters locations alongside their powers. 324 Chapter 10
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Web site translator - Remember what you did with the Powers field?

Tuesday, October 30th, 2007

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)
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.

You next build the (Web server certificate) table in HTML, and

Tuesday, October 30th, 2007

You next build the table in HTML, and insert values into the appropriate places as defaults. This is how you fill in the fields with character data. Note how the script checks to see if the variable is set before echoing it to the page. If it didn t, and the error reporting for PHP was set to E_ALL, there might be a warning printed to the screen if $ch didn t contain a value. Checking is usually a good idea if you aren t certain a variable will be set. Character Name: >

Now you build the Powers select field. As the script loops through each power in the $pwrlist array (which contains all powers), it concatenates the $powers array value for that power ( selected ). If that power s key (from $pwrlist) doesn t exist in the $powers array, $powers[$key] will simply be blank instead of selected. In this way, the script builds a field of all powers where the character s chosen powers are selected in the list. Neato, huh? Powers:
(Ctrl-click to
select multiple
powers)

Note the [] in the select name attribute. That is necessary for PHP to recognize the variable as an array when it gets POSTed to the next page. This is a requirement for any field that might post with multiple values. The following code creates a set of radio buttons for good and evil. The character s alignment is selected with the checked attribute. > good
> evil

322 Chapter 10
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Web design service - AND c.lair_id = l.id . AND c.id

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.

Web server logs - charedit.php This file does double-duty, so it s a

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.

Web hosting ratings - $table .= ; $table .= Alias ; $table .=

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.

Web site domain - duplication. In other words, if you have a

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.

Post office web site - Notice the use of aliases for the tables.

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.

How It Works You created two different (Business web site) files

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.

Figure 10-5 315 Building Databases (Web hosting compare)

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.