Archive for April, 2007

Web site counters - Try It Out Calculating Movie Takings In this

Wednesday, April 25th, 2007

Try It Out Calculating Movie Takings In this exercise, you ll create a new page to display the data you added in the previous exercise. 1. Open your text editor and type the following program: 0) { $font_color = green ; $profit_or_loss = $ . $difference . m ; } else { $font_color = blue ; $profit_or_loss = Broke even ; } return . $profit_or_loss . ; } ?> This function will make life easier for you. This will become clearer as we proceed through the rest of this example. 2. Save this file as movie_details.php. How It Works The line that contains the code substr is placed before the $profit_or_loss line because a loss will return a negative number, and no one actually says, The movie made a loss of minus 10 million dollars. Instead we say, The movie lost 10 million dollars. However, what happens when the movie takings are the same as the movie production costs? That s where the last else comes into play. You ve covered all eventualities. The important thing to remember is that in PHP you can very easily create new variables by performing actions on existing ones. Just because you don t hold the information in the database doesn t mean you can t create it. Try It Out Displaying the New Information In this exercise you are going to alter the original master table to include the new data, and this will serve as your new child table. 1. Add the following code to movie_details.php: 124 Chapter 4
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

Christian web host - mysql_select_db( moviesite ) or die (mysql_error()); //alter movie table to

Tuesday, April 24th, 2007

mysql_select_db( moviesite ) or die (mysql_error()); //alter movie table to include running time/cost/takings fields $add = ALTER TABLE movie ADD COLUMN ( . movie_running_time int NULL, . movie_cost int NULL, . movie_takings int NULL) ; $results = mysql_query($add) or die(mysql_error()); //insert new data into movie table for each movie $update = UPDATE movie SET . movie_running_time=102, . movie_cost=10, . movie_takings=15 . WHERE movie_id = 1 ; $results = mysql_query($update) or die(mysql_error()); $update = UPDATE movie SET . movie_running_time=90, . movie_cost=3, . movie_takings=90 . WHERE movie_id = 2 ; $results = mysql_query($update) or die(mysql_error()); $update = UPDATE movie SET . movie_running_time=134, . movie_cost=15, . movie_takings=10 . WHERE movie_id = 3 ; $results = mysql_query($update) or die(mysql_error()); ?> 2. Save this file as alter_movie.php, then open this file in your browser. Don t worry you will see a blank screen, but your table has been altered and the information has been entered automatically. How It Works First, the script used the ALTER TABLE command to add the appropriate fields into the existing movie table, and then it used the UPDATE command to insert the new data into those fields. If you aren t familiar with these commands, you might try rereading Chapter 3. Now that you have the data in place, you need to create a new page that you ll use to display the extra movie information (movie_details.php). 123 Using Tables to Display Data
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Figure 4-5 Good, eh? No more having (Web design service) to

Tuesday, April 24th, 2007

Figure 4-5 Good, eh? No more having to type lots of different hyperlinks (what a bore that used to be). Before you can go any further, you need to add some data to your existing database that you can use for your movie details. If you recall from Chapter 3, for each movie, you currently have the movie name, director, lead actor, type, and year of release. Let s also add the running time, how much the movie made, and how much it cost to produce. For all you sticklers out there, a word of warning: the dollar amounts we are using are for instructional purposes only. In fact, we have no idea how much money these movies actually made, nor how much they cost to produce. Work with us on this one, okay? Try It Out Adding Data to the Table In this exercise, you ll add some additional data about each movie to the database. 1. Open your text editor and type the following code: Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web and email hosting services

$query = SELECT movie_id, movie_name, . movie_director, (Database web hosting)

Monday, April 23rd, 2007

$query = SELECT movie_id, movie_name, . movie_director, movie_leadactor . FROM movie ; $result = mysql_query($query, $link) or die(mysql_error()); $num_movies = mysql_num_rows($result); $movie_details = ; while ($row = mysql_fetch_array($result)) { $movie_id = $row[ movie_id ]; $movie_name = $row[ movie_name ]; $movie_director = $row[ movie_director ]; $movie_leadactor = $row[ movie_leadactor ]; //get director s name from people table get_director(); //get lead actor s name from people table get_leadactor(); $movie_details .=<< $movie_name $director $leadactor

EOD; } 2. Save the file as table3.php, upload the file to your Web server, and open the page with your browser. Your screen should look like that in Figure 4-5. How It Works You should notice a change between Figure 4-4 (table2.php) and Figure 4-5 (table3.php). You now have links to more detailed information about each movie for your visitor to click. The first change made in the previous section altered the MySQL query to include the $movie_id field. Then you added the new field to the results set returned from the query. (Otherwise, you d just be selecting a field and not actually doing anything with it, and what s the point of that?) The final change created the HTML code that produces a hyperlink on the movie name. You ve also added a nice little touch with the inclusion of tooltips for each of the movies in the list. Unfortunately, some Web browsers don t support this (apologies to those of you who have such browsers). So now that the changes have been made, what does it actually do? Place your mouse over some hyperlinks, and if you view your status bar, you ll see that each link is unique and is created dynamically. This page is known as the master page, and the page that we are going to link to is known as the child page. 121 Using Tables to Display Data
Note: In case you are looking for affordable webhost to host and run your web application check Vision
http web server services

Web design templates - Figure 4-4 Who s the Master? Now let s build

Monday, April 23rd, 2007

Figure 4-4 Who s the Master? Now let s build on the good work that you ve done so far and add more information and functionality to your table. Implementing master and child relationships in your site can allow your users to click on a movie title in your table for more information about the movie. Of course these would all be dynamically generated, so let s find out how to do such a cool thing, and what master/child relationships mean. Try It Out Adding Links to the Table The steps in this section will enable you to load extra information depending on the movie that you click. This requires you to do the following: 1. Open table2.php in your favorite text/HTML editor and add the lines of code that appear highlighted. We haven t displayed the whole page of code, as we re sure you know it by heart already. 120 Chapter 4
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services

$movie_director = $row[ movie_director ]; $movie_leadactor = $row[ movie_leadactor ]; //get director s (Web server extensions)

Sunday, April 22nd, 2007

$movie_director = $row[ movie_director ]; $movie_leadactor = $row[ movie_leadactor ]; //get director s name from people table get_director(); //get lead actor s name from people table get_leadactor(); $movie_details .=<< $movie_name $director $leadactor

EOD; } $movie_details .=<< Total :$num_movies Movies

EOD; $movie_footer =

; $movie =<< 2. Save your file and reload it in your browser. Your screen should now look like that in Figure 4-4. How It Works With the functions get_director and get_leadactor added, the script requests that specific information be requested from the server for each separate row in the table. This enables you to pull the information you want without muddling up your original query. You also cleaned up the formatting for the last two rows with the change in code near the end of the script. Congratulations! You have successfully developed a powerful script that will query a database and put its contents into an HTML table. Give yourself a pat on the back. But like all good explorers, onward we must go. 119 Using Tables to Display Data
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services

or die(mysql_error()); mysql_select_db( moviesite ) or die (mysql_error()); $query = (Web site developers)

Sunday, April 22nd, 2007

or die(mysql_error()); mysql_select_db( moviesite ) or die (mysql_error()); $query = SELECT movie_name, movie_director, movie_leadactor . FROM movie ; $result = mysql_query($query, $link) or die(mysql_error()); $num_movies = mysql_num_rows($result); $movie_header=<<

Movie Review Database

EOD; function get_director() { global $movie_director; global $director; $query_d = SELECT people_fullname . FROM people . WHERE people_id= $movie_director ; $results_d = mysql_query($query_d) or die(mysql_error()); $row_d = mysql_fetch_array($results_d); extract($row_d); $director = $people_fullname; } function get_leadactor() { global $movie_leadactor; global $leadactor; $query_a = SELECT people_fullname . FROM people . WHERE people_id= $movie_leadactor ; $results_a = mysql_query($query_a) or die(mysql_error()); $row_a = mysql_fetch_array($results_a); extract($row_a); $leadactor = $people_fullname; } while ($row = mysql_fetch_array($result)) { $movie_name = $row[ movie_name ]; 118 Chapter 4
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Figure 4-3 echo (Web hosting service) There are $num_movies movies in

Sunday, April 22nd, 2007

Figure 4-3 echo There are $num_movies movies in our database ; echo $movie; You printed the statement about how many movies there are in your database, and then you sent the whole ball of wax ($movie_header, $movie_details, and $movie_footer) with the next line. The table may look pretty, but as in Chapter 3, it doesn t do the user much good if they don t have their secret decoder ring to decipher what actors and directors were associated with your movies. You need to link your tables to pull this information. Try It Out Improving Your Table In this exercise, you ll link the tables together, as you saw in Chapter 3, so you can output meaningful data. 1. Modify your table2.php file as shown in the highlighted text: Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

Then you come across the (Remote web server) familiar tag that

Saturday, April 21st, 2007

Then you come across the familiar tag that you saw at the beginning of this chapter. It s not quite the same as the one before because this one has .=<<

EOD; } The preceding snippet does exactly the same thing, but it would then limit you if you wanted to do any formatting on the variable s values (you ll see what we mean a bit later on). Try It Out Putting It All Together The data has now been retrieved, but you need to send it all to the browser so it will display in the table. 1. You assign the $movie_footer variable by entering the following: $movie_footer =

Movie Title Movie Director Movie Lead Actor
$row[ movie_name ]

; $movie =<< 2. Append this to your previous code, save this file as table2.php, and upload it to your Web server. 3. Load that page into your Web browser. You should see something similar to the screen shown in Figure 4-3. How It Works First, your code took the information stored in $movie_header, $movie_details, and $movie_footer, and rolled all that up, tied it with a bow, and put it in $movie, with the use of heredoc. Then there are the following lines: 116 Chapter 4
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

Hosting web - $movie_header =

Saturday, April 21st, 2007

$movie_header =<<

Movie Review Database

EOD; 4. Then enter the block of code that was originally there (minus the echo statement). Pay attention to the fact that it s called $movie_header, not $movie (as it was in the first example). $movie_details = ; while ($row = mysql_fetch_array($result)) { $movie_name = $row[ movie_name ]; $movie_director = $row[ movie_director ]; $movie_leadactor = $row[ movie_leadactor ]; $movie_details .=<<

EOD; } $movie_details .=<<

EOD; How It Works The preceding code does quite a lot of work for you, so let s look at it in more detail. As you know, the while { } statement loops through the records that have been returned. For each record, it executes the block of code that is between the brackets. Don t worry; PHP is smart enough to know how many records there are and what record number it is currently on in this case. There is no danger of having the wrong values assigned to the wrong record. The first line within the while loop tells the script to pull out the value of the field movie_name in the current record and put it into a variable called $movie_name. The next four lines do pretty much the same thing they simply assign different field values to differently named variables. 115 Using Tables to Display Data
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services


Movie Title Movie Director Movie Lead Actor
$movie_name $movie_director $movie_leadactor
 
Total :$num_movies Movies