Wednesday, April 4th, 2007
In the game Keno you might have noticed that games begin at 001 and iterate to 999. Let’s look at some code to cycle properly and stay formatted in three digits using sprintf().
$game_number =
1;
echo ‘Game #’ .
sprintf(“%03d”,
$game_number);
// Game #00x
Now let’s add some style magic to give the output a blackground with red numbers:
$game_number =
1;
echo ‘New game #<font style="background-color: black;color: red">’ .
sprintf(“%03d”,
$game_number) .
‘</font><br />’;
Finally we need to create some program logic to test when to reset the game number to 1. An IF statement will suffice and we’ll also add some code to go to show the next game number.
$game_number =
(int
)$_GET[‘gid’];
if($game_number >
999 or
$game_number <
1) {
$game_number =
1;
}
echo ‘New game #<font style="background-color: black;color: red">’ .
sprintf(“%03d”,
$game_number) .
‘<br />’;
$game_number++;
echo “<p><a href=\”?gid=$game_number\”>next game</a></p>”;
by TDavid | Permanent Link | Related Entries: Formatting, Functions
You can follow this entry by subscribing to the comment feed.
You can also leave a response, or trackback from your own site.
Leave a Reply