homepage Welcome to WebmasterWorld Guest from 54.227.215.140
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
multiple drop down boxes - adding values to the database
whatson

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4518836 posted 8:55 pm on Nov 13, 2012 (gmt 0)

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?

 

vincevincevince

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



 
Msg#: 4518836 posted 1:29 am on Nov 15, 2012 (gmt 0)

$_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.

whatson

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4518836 posted 7:14 am on Nov 15, 2012 (gmt 0)

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?

swa66

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



 
Msg#: 4518836 posted 2:19 pm on Nov 15, 2012 (gmt 0)

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.

vincevincevince

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



 
Msg#: 4518836 posted 2:38 pm on Nov 15, 2012 (gmt 0)

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.

whatson

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4518836 posted 6:58 pm on Nov 15, 2012 (gmt 0)

It's for tags, I want to be able to tag categories for a page.

swa66

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



 
Msg#: 4518836 posted 12:13 am on Nov 16, 2012 (gmt 0)

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)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved