/    Sign up×
Community /Pin to ProfileBookmark

How to make an array of selected list/menu items

I want to know how to make an array of the selected items from multiple select menus. I have an order form that looks like this. The product names have a quantity menu beside it,(assuming there is already a quantity menu until the last product name) I want to get the selected quantities and insert them to a ‘quantity’ field in the database separated by “,” just like what you do in checkboxes.

[ATTACH]16501[/ATTACH]

my PHP so far. I only have the array for the checkboxes(product names). I don’t know how to make an array for the quantities/select menus.

[code=php]<?php

include (“dbinfo.php”);

if (isset($_POST[‘button’]))
{
$implode = implode(“,”,$_POST[‘CheckboxGroup’]);
mysql_query(“INSERT INTO order_form (orders) VALUES (‘”.$implode.”‘)”) ;
}
?>

<?php

if(isset($_POST[‘button’]))
{
$implode = implode(“,”,$_POST[‘CheckboxGroup’]);
mysql_query(“INSERT INTO order_form (name, address, contact_no, payment_option, claim_option, orders) VALUES (‘”.$_POST[‘name’].”‘, ‘”.$_POST[‘address’].”‘, ‘”.$_POST[‘contactno’].”‘, ‘”.$_POST[‘pay_option’].”‘, ‘”.$_POST[‘claim_option’].”‘, ‘”.$implode.”‘)”);
}
?>[/code]

[canned-message]attachments-removed-during-migration[/canned-message]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJan 03.2015 — Skipping your coding question and going right to the root of your (future) problem.

Do not design your database to store multiple values in a single field. Verboten. Wrong. Not recommended. Don't Do It.

Store discrete values that represent the same thing as part of a separate record in a 'detail' table, rather than putting them in some comma-separated list in a text field. Say you have an order with multiple line items being purchased, each with their own quantity and price. You start the 'orders' table record with the customer id or something, the order number, the order date, blah,blah,blah. Then for each line item in the order you create a record in an 'order_details' table. The records there all point to an order number stored in the orders table and contain an item number (linking to a separate items table perhaps) or an item description, the quantity and the price (unless you do make an items table that would contain the price).

To join all this info into one set of data for an invoice or a print of the order you use a query with a join to link the order table data to the order-details data and perhaps the items table data.

It's called a properly normalized database. You can look it up.
Copy linkTweet thisAlerts:
@kite111authorJan 04.2015 — thanks I'll keep this in mind.
Copy linkTweet thisAlerts:
@NogDogJan 04.2015 — If you use an array-style name for the select element, the PHP will already have the values in an array for you:
[code=html]
<select name='foo[]' multiple='multiple'>
[/code]

[code=php]
echo "<pre>".print_r($_POST['foo'], 1)."</pre>";
[/code]
×

Success!

Help @kite111 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...