Array help with a form's select list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Array help with a form's select list

Post 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>";
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post 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
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post 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.
Post Reply