Accessing Databases with ADODB - Printing
(Page 8 of 12 )
Record set objects have two methods that return <select> tags built from the data in the record set: GetMenu() and GetMenu2(). The functions are identical except for how they handle default values (more about that in a few paragraphs). The functions use the first column in the record set as the labels for each option and the second column as the values. If you have an ID column and aname column in a table, put the name column first and the ID column second in your query:
$rs = $conn->execute('SELECT flavor,id FROM ice_cream');
print $rs->GetMenu('which_flavor');
This prints the following <select> menu:
<select name="which_flavor" >
<option></option>
<option value="1">Chocolate</option>
<option value="2">Vanilla</option>
<option value="3">"Heavenly" Hash</option>
<option value="4">Diet Cardboard</option>
</select>
The first argument to GetMenu() is used as the name attribute of the <select> tag. The <option> tags are printed one per line in the order that their rows appear in the result set. HTML entities, such as the quotation marks in "Heavenly" Hash, are encoded.
To have one <option> tag marked as selected so the browser treats it as the default value, pass the label for that tag as the second argument to GetMenu():
$rs = $conn->execute('SELECT flavor,id FROM ice_cream');
print $rs->GetMenu('which_flavor','Vanilla');
This produces the following:
<select name="which_flavor" >
<option></option>
<option value="1">Chocolate</option>
<option selected value="2">Vanilla</option>
<option value="3">"Heavenly" Hash</option>
<option value="4">Diet Cardboard</option>
</select>
Default value handling is where GetMenu() and GetMenu2() differ. The GetMenu2() function marks a choice as selected when its value matches the second argument passed in. To make Vanilla the default with GetMenu2(), pass 2 as a second argument:
$rs = $conn->execute('SELECT flavor,id FROM ice_cream');
print $rs->GetMenu2('which_flavor',2);
This prints the same menu as in the previous example.
The menus that you’ve produced with GetMenu() and GetMenu2() so far all have an initial blank option. This is useful for checking that a user has selected an option in mandatory fields. If you want to turn it off, pass false as the third argument to either function:
$rs = $conn->execute('SELECT flavor,id FROM ice_cream');
print $rs->GetMenu2('which_flavor',null,false);
This produces a menu without an initial blank option:
<select name="which_flavor" >
<option value="1">Chocolate</option>
<option value="2">Vanilla</option>
<option value="3">"Heavenly" Hash</option>
<option value="4">Diet Cardboard</option>
</select>
By passing null as the second argument to GetMenu2(), you avoid setting any option to selected while still being able to specify that you don’t want an initial blank.
The next two arguments to GetMenu() and GetMenu2() control whether the user is allowed to select multiple options of the menu and, if so, how many options to display at once. These arguments set the multiple and size attributes of the <select> tag. To set the multiple attribute, pass true as a fourth argument. To set the size attribute, pass the value to set size to as the fifth argument. The size attribute is only relevant if the multiple attribute is set. If multiple is set, then the menu name has square brackets appended to it. This tells PHP to treat the submitted form values as an array. The following is an example:
$rs = $conn->execute('SELECT flavor,id FROM ice_cream');
print $rs->GetMenu('which_flavor',null,false,true,3);
This produces the following menu:
<select name="which_flavor[]" multiple size=3 >
<option value="1">Chocolate</option>
<option value="2">Vanilla</option>
<option value="3">"Heavenly" Hash</option>
<option value="4">Diet Cardboard</option>
</select>
The sixth argument to the menu functions is a string containing additional attributes for the <select> tag. You can use this argument to specify values for a style, class, or other attribute. If you want to pass additional attributes in this argument, you need to fill in defaults or appropriate values for all of the preceding arguments:
$rs = $conn->execute('SELECT flavor,id FROM ice_cream');
print $rs->GetMenu('which_flavor',null,false,null,null,'class="bigselect"');
This adds the class="bigselect" attribute to the <select> tag:
<select name="which_flavor" class="bigselect">
<option value="1">Chocolate</option>
<option value="2">Vanilla</option>
<option value="3">"Heavenly" Hash</option>
<option value="4">Diet Cardboard</option>
</select>
This is from Essential PHP Tools, by David Sklar (Apress, ISBN 1590592808). Check it out at your favorite bookstore today. Buy this book now. |
Next: Splitting Data Across Multiple Pages >>
More Database Articles
More By Apress Publishing