Make my own web site - Now we re going to get a little tricky.
Saturday, October 13th, 2007Now we re going to get a little tricky. Because the list of powers could get quite large, you want to try to distribute them across multiple columns. However, you would probably like to distribute them fairly evenly. The following 13 lines of code do this for you (if math is not interesting to you at all, or you simply don t want to know how this part of the code works, skip this section). First, you get a count of the number of powers in the array. Next, you set the threshold to 5 lines (after which a second column will be created), and a maximum number of columns (in this case, 3). $numpwr = count($pwrlist); $thresh = 5; $maxcols = 3; Next, you determine how many columns to create. Assume there are 7 powers to display. First, you divide the count by the threshold (7/5), which gives you 1.4. Next, you use ceil() to round up to the nearest integer (ceil (1.4) = 2). Then you take the smaller of the two values (3 and 2), and store it in the $cols variable. In this example, $cols would equal 2. To figure out how many powers go into each column, you divide the count by the number of columns, and round up to the nearest integer. In this case, ceil(7/2) = 4. So, you ll have two columns, with four values in each column (the last column will contain the remainder of powers if there are fewer than four). $powerchk is a string that will contain each power, with a checkbox attached to it. For now, you initialize it to an empty string . $cols = min($maxcols, (ceil(count($pwrlist)/$thresh))); $percol = ceil(count($pwrlist)/$cols); $powerchk = ; Now you loop through each element of the $pwrlist array, which contains the ID as the key ($id), and power as the value ($pwr). The counter $i will start at 0 and increment each time through the loop. In each loop, you add the tag to create the checkbox, using the ID as the value, and the name of the power as the label. When the counter reaches a value that is divisible by $percol, you add a close table definition and start a new one. $i = 0; foreach ($pwrlist as $id => $pwr) { if (($i>0) && ($i%$percol == 0)) { $powerchk .=
n
n ; $i++; } In this example, increments 0, 1, 2, and 3 end up in the first column. When $i reaches 4 (the value of $percol), the script starts a new column. If this is confusing, don t worry. You can play around with it by changing your $thresh and $maxcols values and adding a bunch of random power values to see how the table is built. For now, let s check out the rest of the code. 304 Chapter 10
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.
3. Enter an ultra-cool superpower such (Web hosting comparison) as invisibility
Friday, October 12th, 20073. Enter an ultra-cool superpower such as invisibility or X-ray vision in the text box, and click Add Power. If you need help with power ideas, here are a few: super strength, invisibility, X-ray vision, speed, soccer mom, stretchable, flight, breathes underwater. Add a total of six powers. Moving on, you should now see a new button and a list of powers with checkboxes next to them. 4. Check one or two powers and click Delete Powers. They should go away. How It Works You will see this on every page, but we will mention it this one time only. You include the config.php file that contains the constants used in the next couple of lines. By putting these constants in an included file, you can make any required changes in one place. You use the require command instead of include because of the way PHP works: An included file will not stop the processing of the rest of the page, whereas a required file, if not found, would immediately stop processing. require( config.php ); Next, a connection to the server is made, and the appropriate database is selected. Notice the use of the constants you defined in config.php: $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); What follows is a somewhat simple SQL select statement. It grabs the id and power columns from the char_power table and sorts them by power. This way, when you iterate through them later and put the data on the Web page, they will be in alphabetical order. $sql = SELECT id, power FROM char_power ORDER BY power ; The following code executes the SQL statement and throws an error if there are any problems: $result = mysql_query($sql) or die(mysql_error()); Now the script checks to make sure at least one row was returned. If so, it iterates through each row, building up an array of powers, using the power ID as the array key. Note the use of mysql_fetch_array. Other options are mysql_fetch_row and mysql_fetch_assoc. Using mysql_fetch_array gives you the flexibility to reference the results by numerical index or named index. if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $pwrlist[$row[ id ]] = $row[ power ]; } When the script retrieves data from the database, it will usually need to retrieve appropriate ids so that you can later insert or update the correct record. In this case, the ID serves as the key to the array, making it easy to retrieve the values. You could have certainly used a multi-value array, but that gets a little more confusing, and it s just not necessary here. Just be sure you understand that many times in this application (and many applications using relational databases) you will use the table IDas an array key. 303 Building Databases
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.
Apache web server - ?> Add/Delete Powers Comic BookAppreciation Editing Character Powers
Thursday, October 11th, 2007?>

Comic Book
Appreciation
Editing Character Powers
Return to Home Page 2. Load poweredit.php in your browser. When the page appears (see Figure 10-1), it initially will be empty. Figure 10-1 302 Chapter 10
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.
Each case sets a destination page after running (Cool web site)
Wednesday, October 10th, 2007Each case sets a destination page after running its queries. This command will now send the user to that destination. Try It Out Editing Superhero Powers The next page you re going to create is a script to allow you to create and modify superhero powers. 1. Enter the following code in your favorite PHP editor, and save it as poweredit.php: 0) { while ($row = mysql_fetch_array($result)) { $pwrlist[$row[ id ]] = $row[ power ]; } $numpwr = count($pwrlist); $thresh = 5; $maxcols = 3; $cols = min($maxcols, (ceil(count($pwrlist)/$thresh))); $percol = ceil(count($pwrlist)/$cols); $powerchk = ; $i = 0; foreach ($pwrlist as $id => $pwr) { if (($i>0) && ($i%$percol == 0)) { $powerchk .=
n
n ; $i++; } $delbutton =
deleting will remove all associated powers
from characters as well — select wisely
; } else { $powerchk =
; $delbutton = ; $cols = 1; } 301 Building Databases
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.