can't get it to work: db with foreach() in form function

Discussion in 'PHP' started by lampie1978, Aug 30, 2006.

  1. #1
    Some help please,

    I'm trying for a couple of days now and I don't get the code to work the way I want it to work.

    What I want is a query that provides data for an array (this works) that is displayed on screen by a foreach in a dropdownbox (this works also), but when I put it all in to a function it doesn't work anymore. And it doesn't matter where I put the query.
    So if one can help, one would be very happy.

    Here is the code that's causing the problem:

    if($result)
    	{
    		$knsa_nr_array=array();
    	
    		while ($row = mysql_fetch_object($result))
    		{
    			$knsa_nr = ($row -> knsa_nr);
    			$vnaam = ($row -> vnaam);
    			$anaam = ($row -> anaam);
    			$voorvoegsel = ($row -> voorvoegsel);
    
    			$knsa_nr_array[] = array("$anaam, $vnaam $voorvoegsel"=>"$knsa_nr") + $knsa_nr_array;
    			$knsa_nr_array[] = array("Naam"=>"00") + $knsa_nr_array;
    		}
    	}
    
    	function show_form($knsa_nr="", $schiet_dd_dag="", $schiet_dd_maand="", $schiet_dd_jaar="", $diopler="", $diopler_tot="", 
    		$kkg="", $kkg_tot="", $kkp="", $kkp_tot="")
    	{
    		?>
    
    		<form name="schietstand_toevoegen" method="post" action="standen_add.php">
    		<table width="750" bordercolor="#000000" border="1">
    		<tr><td width="350">KNSA nummer</td>
    			<td width="400">
    			
    			<?
    			if ($_GET[knsa_nr])
    			{
    				?>
    				<input type="text" name="knsa_nr" value="<? echo $_GET[knsa_nr] ?>" size="10" onFocus="blur()">
    				<?
    			}
    			else
    			{
    				foreach($knsa_nr_array as $knsa_nr_show => $knsa_nr_input)
    				{
    					?>
    					<option value="<? echo $knsa_nr_input?>" <? if($knsa_nr_input==$knsa_nr) { ?> selected <? } ?> onfocus="blur()"> <? echo $knsa_nr_show ?> </option>
    					<?
    				}
    			}
    			?>
    
    			</td>
    
    PHP:
    It doesn't matter where I place this code, every time I adjust the code I get the following error-message:
    and this reverse to the following line
    I can get this code to work without the function show_form(), but I need it incase a invalid value is inserted.
    Or can I get it to work without the show_form() function with pasting the values in a string?? If so how do I get multiple values in the <a href>?

    Thanxs
     
    lampie1978, Aug 30, 2006 IP
  2. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #2
    Vars are only local to the function, declare the the db array within the function
    ie global $knsa_nr_array;
     
    Big 'G', Aug 30, 2006 IP
  3. lampie1978

    lampie1978 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanxs :D

    That was indeed the trick after a few tries.

    I set
    and it worked like a charm.
    How stupid of myself, didn't imagine I could forget that :eek:

    Cheers
     
    lampie1978, Aug 30, 2006 IP