Join Kevin Skoglund for an in-depth discussion in this video Generate options dynamically, part of Easy PHP Projects: Measurement Conversion.
- View Offline
- In this movie, we will use PHP to…dynamically generate the options to…use with our HTML select tags.…In the last movie, we talked about the need to…try and find a smarter way to generate all of…the options we're going to need for the units…of volume measure.…We can do that using PHP.…What I'm thinking that we can do is…use a list of all the volume units of measure,…and then we can loop through that list…two different times, once for the from unit…and once for the to units, and each time to…that list we'll output the option tag…with the correct options that needs included.…
So let's start by going up here,…and right after the form processing,…I'm going to paste in a list of the measures.…I've got that listed as an array,…and I'm assigning that to a variable called volume_options.…You can pause the movie if you need to…copy this down, it's also the same thing we saw…on the previous slide.…So we've got cubic inches, cubic feet,…then we've got all of the imperial measures,…all of the US measures, and then cubic centimeters,…
Along the way, series designer Kevin Skoglund introduces coding strategies, refactoring techniques, and useful skills including dynamically generating form select-option values and formatting decimal numbers and scientific notation.
- Converting length and area measurements
- Using functions to avoid repetition
- Refactoring to produce simpler code
- Converting volume and mass
- Generating form select-option values dynamically
- Formatting scientific notation
- Converting speed and temperature