The Little Guy Posted April 27, 2007 Share Posted April 27, 2007 I am trying to create a function that will make a select box from an array of items, I got it to work for one array. If at some point, I decide I want to use a different array in this function, the current function wont let me, right now it only lets me use the array $manName. How can I do this, any ideas? Here is the function: <?php function create_select_p($formtxt,$formname,$setval='-1'){ $field = '<p>'.$formtxt.':<br>'; $field .= '<select name="'.$formname.'">'; $field .= '<option>Select...</option>'; global $manName; foreach($manName as $num => $type){ $field .= '<option value="'.$num.'"'; if($setval == $num){ $field .= ' selected'; } $field .= '>'.$type.'</option>'; } $field .= '</select>'; $field .= '</p>'; return $field; } ?> Link to comment https://forums.phpfreaks.com/topic/48878-solved-function-issues/ Share on other sites More sharing options...
jitesh Posted April 27, 2007 Share Posted April 27, 2007 <?php function create_select_p($formtxt,$formname,$setval='-1'){ $field = '<p>'.$formtxt.':<br>'; $field .= '<select name="'.$formname.'">'; $field .= '<option>Select...</option>'; global $manName; global $otherarray; foreach($otherarray as $num => $type){ $field .= '<option value="'.$num.'"'; if($setval == $num){ $field .= ' selected'; } $field .= '>'.$type.'</option>'; } $field .= '</select>'; $field .= '</p>'; return $field; } ?> Link to comment https://forums.phpfreaks.com/topic/48878-solved-function-issues/#findComment-239563 Share on other sites More sharing options...
The Little Guy Posted April 27, 2007 Author Share Posted April 27, 2007 that won't work... this function needs to be a global function, and work with any array, at any time. Link to comment https://forums.phpfreaks.com/topic/48878-solved-function-issues/#findComment-239571 Share on other sites More sharing options...
jitesh Posted April 27, 2007 Share Posted April 27, 2007 <?php function create_select_p($formtxt,$formname,$setval='-1',$otherarray){ $field = '<p>'.$formtxt.': '; $field .= '<select name="'.$formname.'">'; $field .= '<option>Select...</option>'; foreach($otherarray as $num => $type){ $field .= '<option value="'.$num.'"'; if($setval == $num){ $field .= ' selected'; } $field .= '>'.$type.'</option>'; } $field .= '</select>'; $field .= '</p>'; return $field; } ?> Link to comment https://forums.phpfreaks.com/topic/48878-solved-function-issues/#findComment-239573 Share on other sites More sharing options...
The Little Guy Posted April 27, 2007 Author Share Posted April 27, 2007 Still won't work. I don't think you are understanding what I am trying to do. I have 2+ arrays, I want to be able to choose an array, and the function will grab the chosen array and create a select box with that array's values. Link to comment https://forums.phpfreaks.com/topic/48878-solved-function-issues/#findComment-239576 Share on other sites More sharing options...
jitesh Posted April 27, 2007 Share Posted April 27, 2007 <?php $ar1 = array(1,2,3,4); $ar2 = array(5,6,7); $ar3 = array(8,9,10); $otherarray = array_merge($ar1, $ar2,$ar3); create_select_p('','','',$otherarray) ?> <?php function create_select_p($formtxt,$formname,$setval='-1',$otherarray){ $field = '<p>'.$formtxt.': '; $field .= '<select name="'.$formname.'">'; $field .= '<option>Select...</option>'; foreach($otherarray as $num => $type){ $field .= '<option value="'.$num.'"'; if($setval == $num){ $field .= ' selected'; } $field .= '>'.$type.'</option>'; } $field .= '</select>'; $field .= '</p>'; return $field; } ?> Link to comment https://forums.phpfreaks.com/topic/48878-solved-function-issues/#findComment-239580 Share on other sites More sharing options...
The Little Guy Posted April 27, 2007 Author Share Posted April 27, 2007 Won't work either, I don't want to merge the arrays. I want to make different select boxes from different arrays, I just want to make this a global function to do this, currently it will only work with one array. Link to comment https://forums.phpfreaks.com/topic/48878-solved-function-issues/#findComment-239581 Share on other sites More sharing options...
neel_basu Posted April 27, 2007 Share Posted April 27, 2007 Use foreach($GLOBALS['otherarray'] as $num => $type) Where otherarray is teh name of another Global Array. Link to comment https://forums.phpfreaks.com/topic/48878-solved-function-issues/#findComment-239600 Share on other sites More sharing options...
The Little Guy Posted April 27, 2007 Author Share Posted April 27, 2007 That is exactly what I'm looking for.. Thanks! Link to comment https://forums.phpfreaks.com/topic/48878-solved-function-issues/#findComment-239611 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.