Archive for March, 2008

886Part VCase StudiesListing 46-2(continued) Congratulations! You have a (Web design online)

Thursday, March 20th, 2008

886Part VCase StudiesListing 46-2(continued)

Congratulations! You have a high score

. Enter your name (or a nickname) for the high . scores list: .
. . . . . . . . ; return($result_string); } private function _highScoreString () { // The table of high scores itself, including // the database interaction necessary to retrieve itif ($this->_highScoreEligible() && !$this->_highScorePosted) { $result_string = $this->_postHighScoreString(); } else { $result_string = ; } $result_string .=

High scores to date

. . . ; $query = select name, answer_count, creditfrom high_scoresorder by answer_count desc, credit desclimit 10 ; $connection = $this->_game->gameParameters->getDbConnection(); if ($connection && is_resource($connection)) { $result = mysql_query($query, $connection); $rank = 1; while ($row = mysql_fetch_assoc($result)) { $name = $row[ name ]; $answer_count = $row[ answer_count ]; $credit = (int) ($row[ credit ]); $result_string .=
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Web hosting ecommerce - 885Chapter 46A Trivia Gameif (mysql_num_rows($result) > 9) {

Wednesday, March 19th, 2008

885Chapter 46A Trivia Gameif (mysql_num_rows($result) > 9) { while ($row = mysql_fetch_assoc($result)) { $answer_count = $row[ answer_count ]; $credit = $row[ credit ]; if (($this->_game->getCorrectAnswers() > $answer_count) || (($this->_game->getCorrectAnswers() == $answer_count) && ($this->_game->_credit > $credit))) { $eligible = TRUE; break; } } } else { $eligible = TRUE; } return($eligible); } else { throw newException( Game display has no database connection ); } } // Checksum is calculated when posting a score // (comprised of a number of correct answers plus// credit remaining) and the checksum is compared with// the submitted scores. A first line of defense against// spoofing (unless, of course, the checksum scheme is// published in a book or something). private function _checksumChecks ($answer_count, $credit, $checksum) { return($checksum == $this->_makeCheckSum($answer_count, $credit)); } private function _makeChecksum ($answer_count, $credit) { return ((round($credit)) * 17) * ($answer_count * 31); } private function _postHighScoreString () { // The greeting plus HTML form for actually submitting// a name to the high scores listglobal $PHP_SELF; $answer_count = $this->_game->getCorrectAnswers(); $credit = $this->_game->getCredit(); $checksum = $this->_makeChecksum($answer_count, $credit); $result_string = Continued52
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

884Part VCase StudiesListing 46-2(continued) n ; $count++; (Web hosting top) } $distractor_string

Wednesday, March 19th, 2008

884Part VCase StudiesListing 46-2(continued)

n ; $count++; } $distractor_string .=
RankNameCorrect answersCredit left at end
; $distractor_string .= ; $distractor_string .= ; $distractor_string .= ; return($distractor_string); } private function _previousQuestionString () { if (!$this->_game->getPreviousQuestion()) { $return_string = ; } else { $return_string = $this->_game->previousQuestionCorrect() ? $this->_rightString() : $this->_wrongString(); } return($return_string); } function _rightString () { return(

RIGHT!

); } function _wrongString () { return(

WRONG!

); } private function _highScoreEligible () { // takes a game-ending score, and queries the// DB to see if the player is eligible for the// high score list$query = select name, answer_count, creditfrom high_scoresorder by answer_count desc, credit desclimit 10 ; $connection = $this->_game->getDbConnection(); if ($connection && is_resource($connection)) { $result = mysql_query($query, $connection); $eligible = false;
You want to have a cheap webhost for your apache application, then check apache web hosting services.

883Chapter 46A Trivia Game$return_string = _distractorString( $this->_game->getCurrentQuestion()) . (Web hosting services)

Tuesday, March 18th, 2008

883Chapter 46A Trivia Game$return_string = <<$title

$title
(How sure are you?)

EOT; return($return_string); } private function _currentQuestionString () { global $PHP_SELF; return(

. $this->_game->getCurrentQuestionText() .

. . . $this->_distractorString( $this->_game->getCurrentQuestion()) . ); } private function _distractorString ($question) { // creates the actual HTML for presentation of // radio-button alternatives for guesses. // Assumes that the array representing the// actual alternatives has been calculated in// advance, retrievable from the question using// getDistractorArray$distractor_array = $question->getDistractorArray(); $distractor_string =
; $distractor_string .= ; $count = 1; // 1-based labels are preferable, // so we can just use if ($label) … $total = count($distractor_array); foreach ($distractor_array as $distractor) { $lower_selected = ($count == 1) ? CHECKED : ; $upper_selected = ($count == $total) ? CHECKED : ; $formatted_distractor = ($distractor >= 10000) ? number_format($distractor) : $distractor; $distractor_string .= n . Continued52
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Web hosting faq - 882Part VCase StudiesListing 46-2(continued) $left_side $right_side EOT; return($page_string);

Monday, March 17th, 2008

882Part VCase StudiesListing 46-2(continued)

At leastNot more than
$formatted_distractor
$left_side $right_side
EOT; return($page_string); } } function handleHighScore () { // Handles database update for case where player// has earned high score, and has submitted// a name for the recordif (!$this->_highScorePosted) { $this->_highScorePosted = TRUE; if (get_post_value( NICKNAME ) && get_post_value( ANSWER_COUNT ) && get_post_value( CREDIT ) && get_post_value( CHECKSUM ) && $this->_checksumChecks( get_post_value( ANSWER_COUNT ), get_post_value( CREDIT ), get_post_value( CHECKSUM ))) { $name = get_post_value( NICKNAME ); $answer_count = get_post_value( ANSWER_COUNT ); $credit = get_post_value( CREDIT ); $query = insert into high_scores(name, answer_count, credit) values ( $name , $answer_count, $credit) ; $connection = $this->_game->gameParameters->getDbConnection(); $result = mysql_query($query, $connection); } else { // do nothing — failure to add high score// should not be a deal killer } } } // PRIVATE FUNCTIONSprivate function _makeTopMatter ($title) { // returns HTML fragment that heads both// regular page and error page, containing// HTML head and title52
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Web hosting compare - 881Chapter 46A Trivia Game$top_matter_string = $this->_makeTopMatter($this->_pageTitle); $current_question =

Sunday, March 16th, 2008

881Chapter 46A Trivia Game$top_matter_string = $this->_makeTopMatter($this->_pageTitle); $current_question = $this->_currentQuestionString(); $previous_question = $this->_previousQuestionString(); $game_state = $this->_gameStateString(); $introduction = $this->_gameText->introduction(); $rules = $this->_gameText->rules(); if ($this->_game->getGameLost()) { $left_side = $this->_gameText->gameLostText() . $this->_highScoreString(); } elseif ($this->_game->getGameWon()) { $left_side = $this->_gameText->gameWonText() . $this->_highScoreString(); } else { $left_side = $current_question; } if ($this->_game->getPreviousQuestion()) { $right_side =

$previous_question
$game_state
$rules
; } else { $right_side =
$introduction
$rules
$game_state
; } // actually construct page$page_string = <<We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

880Part VCase StudiesListing 46-2(continued) function getGame() { return($this->_game); (Web site translator)

Sunday, March 16th, 2008

880Part VCase StudiesListing 46-2(continued) function getGame() { return($this->_game); } function getHighScorePosted() { return($this->_highScorePosted); } function updateWithAnswer ($lower, $upper) { $game = $this->getGame(); $game->updateWithAnswer($lower, $upper); } function makeErrorPage ($problem_string) { // constructs the HTML page to display when/// something has gone horribly wrong$top_matter_string = $this->_makeTopMatter($this->_pageTitle); $page_string = <<

We re sorry, but the game is not available right now.

($problem_string)

EOT; return($page_string); } function display () { // returns entire page as string — // backbone structure of page, plus// overridable methods to print components// sanity checksif (!$this->_game || !is_object($this->_game)) { throw new Exception( Cannot find valid game object ); } elseif (!$this->_game->getDbConnection()) { throw new Exception( No database connection ); } // display of apparently valid pageelse {
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Web file server - 879Chapter 46A Trivia GameThe code file depends on

Saturday, March 15th, 2008

879Chapter 46A Trivia GameThe code file depends on two other files: game_class.phpand game_text_class.php. Theformer contains most of the logic for the inner workings of the game, whereas the latter justcontains some boilerplate text. The job of the GameDisplayclass is to extract all the informa- tion from the game state necessary to produce actual HTML pages. The important public functions in the class are .The constructor function. .updateWithAnswer(), which is called with data from the user s submission of a guess. .makeErrorPage(), which returns HTML to display if something has gone wrong. .display(), which returns HTML to display when everything has gone right. Listing 46-2:game_display_class.php_game = $game; $this->_gameText = new GameText(); } // PUBLIC FUNCTIONS// accessorsfunction getPageTitle() { return($this->_pageTitle); } function getBlueColor() { return($this->_blueColor); } function getRedColor() { return($this->_redColor); } Continued52
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

878Part VCase StudiesThe object types that index.phpcares about (Web host sites)

Friday, March 14th, 2008

878Part VCase StudiesThe object types that index.phpcares about are GameDisplayand Game. The Gameobjectcontains all the state information that needs to be preserved from page to page about wherethe user is in the game (score, questions asked already, and so on). The GameDisplayobjectcontains a Gameinstance and does everything necessary to produce an HTML page from it. If the user is starting off for the first time, we create a new Gameobject, relying on the object sconstructor to initialize it appropriately. For subsequent pages, though we rely on the auto- matic object serialization feature of PHP sessions to store the Gameobject for us. (The actualdefinitions of get_session_value()and set_session_value()are in certainty_utils. php, but all that is happening is that we stash the Gameobject in a session. PHP takes care ofthe serialization that is necessary to read the object into a session and back out again.) For more on what it means to serialize an object, see Chapter 20; for an explanation of ses- sions and their workings, see Chapter 24. If the user is in the middle of a game, we expect both a Gameobject stored in the session anda form submission representing either a guess at the answer or a request to be listed on theHigh Scores page. Regardless of whether we create a new Gameobject or retrieve the one from the last page, wecreate a new GameDisplayobject around it and then ask that object for a string that repre- sents the entire HTML page. We store this in a string, ready to echo it out to the browser inthe very last code line. ExceptionsMany things can go wrong with the execution of this game s code. For one thing, of course, there s always a possibility of a code bug that leaves the game in a strange state. In addition, though, the code relies on at least three external services, any of which might misbehave: .The database, which stores the questions and answers .The session mechanism, which is in turn probably relying on files on the hard disk .Cookies stored on the user s browser, which may refuse themIf any of these services turn out to be unreliable, the game will not be playable. Our goal inthis situation should be to fail as gracefully as possible. In a previous edition of this chapter, the code had to catch all the possible failures and propa- gate an error up to this page, which would then detect the failure and display an appropriateerror string. This time, though, we can use PHP5 s exception mechanism, which makes itmuch easier to structure the code. Whenever we encounter a problem that cannot be recov- ered from, we throwthe problem, along with a descriptive string. The catchstatement inindex.phpis the only one in the game s code, and so will receive any of the exceptions thathappen as a result of its calls to functions in other code files. In addition, it will catch theexception thrown in Case 4 of index.php, which probably indicates that the user s browser is not accepting cookies. See Chapter 31 for an introduction to exceptions and error handling. game_display_class.phpAlmost all of the look-and-feel action for this game is in game_display_class.php, as shownin Listing 46-2. Cross- ReferenceCross- Reference52
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

877Chapter 46A Trivia Game$game_display->updateWithAnswer($lower, $upper); } // CASE (Unable to start debugging on the web server)

Friday, March 14th, 2008

877Chapter 46A Trivia Game$game_display->updateWithAnswer($lower, $upper); } // CASE 3: Player has either just arrived or has // finished a game and asked for a new one. elseif (!get_post_value( POSTCHECK ) || get_post_value( NEW )) { $game_display = new GameDisplay(new Game()); } // CASE 4: Something is wrong. // The page is the result of a POST operation, // yet we don t seem to have a live session, so// we are not successfully tracking a game. // The only thing to do is complain, and ask about // cookies. else { $game_display = new GameDisplay(new Game()); throw (new Exception( We couldn t track your game. . You may have to enable cookies to play )); } // Construct string that will be displayed as page$page_string = $game_display->display(); // Store game state in session so that next// page can pick it upset_session_value( game , $game_display->_game); } // end of try blockcatch (Exception $exception) { // There is a problem somewhere. Create// an error page. $exception_msg = $exception->getMessage(); $display = new GameDisplay(null); $page_string = $display->makeErrorPage($exception_msg); // hope to start fresh next timeunset_session_value( game ); } // Actually echo page to browserecho($page_string); ?>
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.