multidimensional arrays - form menu/list boxes

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
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

multidimensional arrays - form menu/list boxes

Post by gjb79 »

I have a form that uses list/menu boxes to allow a user to select multiple items from the list. Once selected these items are to be logged into a sql database, each selected item as its own seperate insertion into the table.

I understand that the list boxes submit variables as arrays. How can I setup my insert sql statment to insert the value of each item into the table seperately?

I thought about using a loop to do this, where the insert clause would loop until the list of items in the array ended. The table would have 2 columns aside from the unique id column. one would be a set variable $pageid the other would be the array.

Does anybody know how to handle this situation?

Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hm, could you please explain in more detail. I don't get the multidim-array part of your approach.

maybe starting with this simple example

Code: Select all

<html>
	<body>
		<pre>
<?php
if(isset($_POST['choices']) && is_array($_POST['choices']))
{
	foreach($_POST['choices'] as $item)
	{
		$query = 'INSERT ... '.$item;
		echo htmlentities($query), "\n";
	}
}
?>		
		</pre>
		<form method="post">
			<select name="choices[]" multiple="multiple">
				<option value="1">a</option>
				<option value="2">b</option>
				<option value="3">c</option>
				<option value="4">d</option>
			</select>
			<input type="submit" />
		</form>
	</body>
</html>
you can show me what you want to do.
Post Reply