Jump to content

[SOLVED] Function issues


The Little Guy

Recommended Posts

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

<?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;

}

?>

<?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;

}

?>

<?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;

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.