Hi everyone,
I am working on writing up a simple little class to handle forms (I know there are some out there that do this but I want the experience). What I am wondering is, is there a way to accept an array as a function argument, for making checkboxes, drop downs, radios, etc... like:
function checkbox($numboxes, $listofnames, $listofvalues) {
code here
}
then call it using something like
checkbox(4, ("test1", "test2", "test3", "test4"), ("v1", "v2", "v3", "v4") );
??
Thanks for any help or insights you may have.
Taking an array as a function arg?
Moderator: General Moderators
e.g.
Code: Select all
<?php
function createCheckBoxes($def)
{
foreach($def as $key=>$value)
echo '<input type="checkbox" name="',$key,'" value="', $value ,'" />';
}
createCheckBoxes(array('test1'=>'v1', 'test2'=>'v2', 'test3'=>'v3', 'test4'=>'v4'));
?>maybe you should display somekind of description, too
e.g.if the checkbox' value differs from the displayed description things get a bit more complicated 
e.g.
Code: Select all
function createCheckBoxes($def)
{
foreach($def as $key=>$value)
echo '<input type="checkbox" name="',$key,'" value="', $value ,'" />', $value, '<br/>';
}