Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi everybody! I made a function

Status
Not open for further replies.

GMcFly

Programmer
Oct 2, 2002
48
0
0
NL
Hi everybody!

I made a function which automatically creates a combo box with values. I am still a beginner so this might not be the best way. The idea is that I only need to define a few variables to make a combo box.
An example would be lib_survey_combo("cbo_test", "width:100px;", "tbl_test", "test_id", "test_name"). This all looks very nice, but there is a problem:

Code:
  	function lib_survey_combo($name, $style, $table, $id_field, $field){
	/*===============================================================================
	  Function which creates a combo box with values
	===============================================================================*/
	
		echo &quot;<select name=\&quot;$name\&quot;&quot;;
		if (!empty($style)){ echo &quot;style=\&quot;$style\&quot;&quot;; }
		echo &quot;><option>--select--</option>&quot;;
        
		$query 		= &quot;SELECT * FROM $table ORDER BY $field;&quot;;
		$result		= mysql_query($query);
		$n_messages	= mysql_num_rows($result);
		
		while ($data=mysql_fetch_array($result)) {
			extract($data);
			echo &quot;<option value=\&quot;[b]$id_field[/b]\&quot;>[b]$field[/b]</option>&quot;;	
		}                    
		echo &quot;</select>&quot;;		
	}

To actually make this work I should put $test_id instead of $id_field and $test_name instead of $field. But since I want this function to work in other cases too this isn't a solution. Does anybody know what I should do to actually make this work. Is there another way to do this. Thanks a lot!
 
Hi all,

Guess what! I found the answer to my question minutes after I posted my message:

Code:
	function lib_survey_combo($name, $style, $table, $id_field, $field){
	/*===============================================================================
	  Function which creates a combo box with values
	===============================================================================*/
	
		echo &quot;<select name=\&quot;$name\&quot;&quot;;
		if (!empty($style)){ echo &quot;style=\&quot;$style\&quot;&quot;; }
		echo &quot;><option>--select--</option>&quot;;
        
		$query 		= &quot;SELECT * FROM $table ORDER BY $field;&quot;;
		$result		= mysql_query($query);
		$n_messages	= mysql_num_rows($result);
		$i 			= 0;
		while ($data=mysql_fetch_array($result)) {
			extract($data);
			$test_field = mysql_result($result,$i,&quot;$field&quot;);
			$test_id_field = mysql_result($result,$i,&quot;$id_field&quot;);
			echo &quot;<option value=\&quot;$test_id_field\&quot;>$test_field</option>&quot;;
			$i++;	
		}                    
		echo &quot;</select>&quot;;		
	}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top