Make my own web site - Now we re going to get a little tricky.
Now 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.