Taking an array as a function arg?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Taking an array as a function arg?

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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'));
?>
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

AWESOME! YES! That is exactly what I needed, Thanks a BUNCH Volka!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
Post Reply