[thelist] From Array Tip

Liam Delahunty ldelahunty at britstream.com
Tue Sep 17 10:45:00 CDT 2002


TJ wrote:

>In PHP, you can give form elements array names. For example, instead
>of calling a checkbox field "states" you can call it "states[]". When
>you do so, an array called "states[]" is populated and sent onto the
>results page.


...and if you do that then the following code may well be useful...

<tip type="PHP Form Array Checkbox thingy" author="Liam Delahunty">

// Original code by Robin Vickery
function docheckbox($events, $boxvalue)
{
 $events       = is_array($events) ? $events : array();
 $displayvalue = htmlentities($boxvalue);
 $comparevalue = get_magic_quotes_gpc() ? addslashes($boxvalue) : $boxvalue;
 $checked      = in_array($comparevalue, $events, true) ? 'checked' : '';
 printf('<input type="Checkbox" name="events[]" value="%s" %s>',
$displayvalue, $checked);
}


// event_array is defined earlier, or pulled from a db...

for ($i=0; $i<count($event_array) ; $i++)
{
 print ("<tr><td>$event_array[$i]:</td><td>");
 docheckbox($events, "$event_array[$i]");
 print ("</td></tr>\n");
}

</tip>


Kind regards,
Liam Delahunty
Mega Products Limited, 10-11 Moor Street, Soho, London W1D 5NF
t: 020 7434 4201/2 f: 0870 135 8412
http://www.liamdelahunty.com/ web/ design/ database/ programming
http://www.britstream.com/ Hosting/ Domain Names From UKP 7.50 p.a.




More information about the thelist mailing list