Page 1 of 1

Taking an array as a function arg?

Posted: Tue Jan 14, 2003 10:42 am
by Elmseeker
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.

Posted: Tue Jan 14, 2003 10:59 am
by volka
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'));
?>

Posted: Tue Jan 14, 2003 11:09 am
by Elmseeker
AWESOME! YES! That is exactly what I needed, Thanks a BUNCH Volka!

Posted: Tue Jan 14, 2003 11:15 am
by volka
maybe you should display somekind of description, too
e.g.

Code: Select all

function createCheckBoxes($def)
{
   foreach($def as $key=>$value)
      echo '<input type="checkbox" name="',$key,'" value="', $value ,'" />', $value, '<br/>';
}
if the checkbox' value differs from the displayed description things get a bit more complicated ;)