Page 1 of 1

Array help with a form's select list

Posted: Fri May 10, 2002 10:02 am
by Crashin
I've made a drop-down select form element that gets it's options from a query. The options that are displayed are the names of the applications that have been setup so far in the knowledgebase, and the form's intention is to setup categories. Each category will be associated with a specific application. What I'm trying to figure out is when I setup the insert query for the category the select variable will be set to the app_name, as opposed to the app_id (the P-Key for the application table). The categories table is related to the application table by the app_id. Maybe the answer is more obvious than I can see.

Code: Select all

echo "<form name='form' action='insert_cat.php' onsubmit='return do_submit();'>";
	echo "<table border='0'>";
		echo "<tr>";
		
		//get array for application drop-down
		$query = "SELECT * FROM application ORDER BY app_name";
		$result = mysql_query($query);
			if(!$result) &#123;
				echo 'Cannot perform query.';
				exit;
			&#125;
			if (mysql_num_rows($result) == 0) &#123;
				echo "<td><font class='small'>No applications setup - go to the Applications page to setup applications before setting up categories</font></td>";
				exit;
			&#125;
			else &#123;
				echo "<td><font class='small'>Application</font></td>";
				echo "<td>";
					echo "<select name='app_id' size='1'>";
					while($row = mysql_fetch_array($result)) &#123;
						echo "<option>".$row&#1111;1]."</option>";
					&#125;
					echo "</select>";
				echo "</td>";
			&#125;
		echo "</tr>";
		echo "<tr>";
			echo "<td><font class='small'>Category Name</font></td>";
			echo "<td><input type='Text' name='cat_name' maxlength='20' size='15'><br></td>";
		echo "</tr>";
		echo "<tr>";
			echo "<td></td>";
			echo "<td><input type='Submit' value='Create'></td>";
		echo "</tr>";
	echo "</table>";
echo "</form>";

Posted: Fri May 10, 2002 11:28 am
by samscripts
Hi, set the <option value='$app_id'> like this:

Code: Select all

echo "<select name='app_id' size='1'>"; 
while($row = mysql_fetch_assoc($result)) &#123; 
   echo "<option value='".$row&#1111;'app_id']."'>".$row&#1111;'app_name']."</option>"; 
&#125; 
echo "</select>";
sam

Posted: Fri May 10, 2002 11:32 am
by Crashin
Thanks so much for the tip...it was fairly obvious. :oops: I guess sometimes the answer is just too simple to figure out! It worked perfectly.