Welcome to WebmasterWorld Guest from 54.145.246.183

Forum Moderators: coopster & jatar k

multiple drop down boxes - adding values to the database

   
8:55 pm on Nov 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So we have:

<form method="post">
<select multiple="multiple" name="multi">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input name="submit" "type="submit" value="submit" />
</form>

AND

if(isset($_POST['submit'])) {
$multi = $_POST['multi'];
}

So if multiple selections are made, then how do we take these values, I assume we need to create an array, but not sure how this is done.
Also how do we input them into the database, separated by commas?
1:29 am on Nov 15, 2012 (gmt 0)

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member



$_POST['multi'] will already be an array; if you want to separate them by commas, implode: implode(",",$_POST['multi']);

However: if this is a public facing system, then please do some careful checks on valid values... I do it like this:


$data=Array();
foreach ($_POST['multi'] as $k=>$v) if (intval($k)) $data[]=intval($k);
$sql="INSERT .......".implode(",",$data)";


That way, only data which is a positive integer will be inserted.
7:14 am on Nov 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And it would be best in the database like 1,2,3, etc.. with the commas? would that be the easiest way to manage them?
2:19 pm on Nov 15, 2012 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



As for the datamodel I''m by far not convinced imploding into one field is a smart choice.
It seems to me you have a 1:N relation, and should model it appropriately to normalize your data and as such maximise the use of what your relational database can offer.
2:38 pm on Nov 15, 2012 (gmt 0)

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member



It really depends on what you want to do with the data later. Comma separated strings are not convenient or efficient for searching by, but if it's just retrieval and you'll always want to retrieve the full set - then yes, do use comma separated strings. Otherwise, what swa66 said.
6:58 pm on Nov 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's for tags, I want to be able to tag categories for a page.
12:13 am on Nov 16, 2012 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Sounds like an N:N relation even.

What I'd model are 3 tables:
e.g.

Widgets
- widgetId
- widgetName
- ...
These essentially descibe a widget for each row, leaving out all info about tags

Tags
- tagID
- tagName
- ...
These essentially define a tag without thinking to what widget they apply

WidgetTag
- widgetId
- tagId
These link the tags and widgets together.

A tag can be used on multiple widgets and a widget can have multiple tags.


That way a query to find all widgets with a given tag can be made, a query to know all possible tags, en query to find what tags a widget has are all eauqlly easy (just a couple of joins in one select statement)
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month