RU beehive logo ITEC dept promo banner
ITEC 325
2012fall
ibarland

homelectsexamshws
D2Lbreeze (snow day)

lect04a
processing input; array functions

We've been talking about input forms (html elements), and then php scripts to process a submitted form:

When creating a page which includes something typed in by the user (from a previous text-input), we must be careful! What could the user have typed, that would goof up our web page?

Quick q: suppose a user types:

  hi
<3
in a textarea whose name is msg. What is


We have seen arrays, and mentioned that if they have all-numeric indices (keys) then we can process them with a for loop or a while loop, using the same syntax that Java and Javasript happen to use.

Then we saw that if an array has keys which aren't all numeric, we can use a foreach loop to process them:

  $myData = array( 'hi' => 'hallo', 'good day' => 'guten Tag', 'see you later' => 'auf wiedersehen' );

  foreach ($mydata as $german) {
    echo $german, "\n";
    }

  foreach ($mydata as $english => $german) {
    echo "You say '$english', I say '$german'.\n";
    }
The foreach loop is of course one way to process each element of the $_POST array (if you didn't want code specific/different for each input form).

Exercise: Let's write a function which takes in an array of strings, and returns one long html-string mentioning each small string on its own HTML line: something like:

test( htmlLines( array( "one fish", "two fish", "green fish", "blue fish" ) ),
      "one fish<br />two fish<br />green fish<br />blue fish<br />" );

What if we want a web page which lists every file in a directory, each filename on its own html line?
Easy, with the above as a helper, plus1 scandir:
echo htmlLines( scandir( '/ibarland/Tmp' ) );

(See lect04c-soln.html)

Exercise: Write a function that takes in an array of key/values pairs, and returning a string that happens to be html for a pull-down menu of those values, where the key is is the desired value for the HTML form.

Optional: extend the above by including an optional parameter: a top-option like “please select:”.

Left as an exercise: Similar to above, make a function that takes in an array, and returns (the html for) a bunch of checkboxes, inside table.


Before calling filemtime (which takes in a file-name, and returns a date):

  ; Either:
  ini_set('date.timezone','America/New_York');
  date_default_timezone_set('America/New_York');

  echo strtotime('2012-Feb-08 11:03:27');   // don't use '2012.Feb.08 ...'; it returns null.
  echo date('Y-M-d H:m:s',234234234);


1Usually, a google search like “php list files” gives good matches; in this particular case we get sidetracked by related functions that deal with file-handles and are iterators.      

homelectsexamshws
D2Lbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2012.Sep.24 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme