|
Review highlights:
checkpoint: we spent the whole class discussing this and related thoughts, and it seemed to be worth it.
Okay, we watched the
video about
looping over arrays, in php,
and,
writing the function "ol".
So now we
have a php-arrays-practice-files/utils_php.txt
(and
php-arrays-practice-files/utils-test_php.txt)
with the code for
TASK: write a function which takes in a bunch of URLs, and returns an html-ordered-list of those as clickable links.
TASK, version 2: as before, but the input will be key/value pairs of
Pro tip:
coming up: We'll visit this example after discussing html input tags.
Design exercise:
We want to set up a web-form, where people enter information about
PokemonOkaymon.
discussion answers: If solutions are not already being displayed, see php-arrays-practice-soln.html for a sketch of where the discussion might lead.
distance lecture version: breeze video (1h42m); and the tests/code typed therein (roughly same as on this page).
; radioTable : array-of-string, array-of-string → string function radioTable( $rowNames, $colNames ) { return "stub"; } |
Implementation detail: this particular code happens to put the row-names on the right. If you want to make your radio tables have their row-names on the left, it's easy enough to change.
test( radioTable( "fruitses", array("apple","banana","cantaloupe"), array("yucky","yummy") ), <<<END_O_MY_EXPECTED_RESULT <table id='fruitses'> <thead> <tr> <th>yucky</th> <th>yummy</th> <th></th> </tr> </thead> <tbody> <tr> <td><input type='radio' name='???' value='???'/></td> <td><input type='radio' name='???' value='???'/></td> <td>apple</td> </tr> <tr> <td><input type='radio' name='???' value='???'/></td> <td><input type='radio' name='???' value='???'/></td> <td>banana</td> </tr> <tr> <td><input type='radio' name='???' value='???'/></td> <td><input type='radio' name='???' value='???'/></td> <td>cantaloupe</td> </tr> </tbody> </table> END_O_MY_EXPECTED_RESULT ); |
hint: Recall how arrays work with input-names: If one input'sname -attribute is of the form… name='blah[goob]' value='peanut' , and another input'sname is of the form… name='blah[bark]' value='woof' , then$_POST['blah'] will returnarray('goob'=>'peanut', 'bark'=>'woof') . This is neat, since if the table-rows are all related, then we can have the entire table's input-fields collected into a single array. I suggest doing this; it reasonably requires adding a third argument toradioTable , so that you can distinguish multiple radio-tables used within a single form.
(You might even make it an optional argument, and haveradioTableRow create names-not-in-an-array when that argument isn't included! …Making a test-case for this idea can clarify what you might do.)
As well as some more trivial test cases:
test( radioTable( "tablish", array("rowwy"), array("colummy") ), <<<EOT <table id='tablish'> <thead> <tr> <th>colummy</th> <th></th> </tr> </thead> <tbody> <tr> <td><input type='radio' name='???' value='???'/></td> <td>rowwy</td> </tr> </tbody> </table> EOT ); test( radioTable( 'tablish', array(), array("colummy") ), <<<EOT <table id='tablish'> <thead> <tr> <th>colummy</th> <th></th> </tr> </thead> <tbody> </tbody> </table> EOT ); test( radioTable( 'tablish', array("rowwy"), array() ), <<<EOT <table id='tablish'> <thead> <tr> <th></th> </tr> </thead> <tbody> <tr> <th>rowwy</th> </tr> </tbody> </table> EOT ); test( radioTable( 'tablish', array(), array() ), <<<EOT <table id='tablish'> <thead> <tr> <th></th> </tr> </thead> <tbody> </tbody> </table> EOT ); |
; radioTable : string, string[], string[] → string ; Return a table of radio-buttons: ; each row is a bank-of-radio-buttons for the corresponding element of $rowNames, ; and its `name` attribute is $id[$rowName]. ; The allowed values are $colNames. ; ; function radioTable( $id, $rowNames, $colNames ) { $headerRow = radioTableHeader( $colNames ); $rowsSoFar = ""; foreach ($rowNames as $rowName) { $rowsSoFar .= radioTableRow( $id, $rowName, $colNames ) . "\n"; } return <<<EOT <table id='$id'> <thead> $headerRow </thead> <tbody> $rowsSoFar </tbody> </table> EOT; } |
Note that you should have test-cases for
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |