Jump to content

Archived

This topic is now archived and is closed to further replies.

homchz

Adding more than one value to a db table field

Recommended Posts

I would like to have a list of check boxes and have each value entered into a db table feild seperated by a comma for separation later.

I have tried having a group of check boxes with the same name posted into the db but it only grabs the last one.

<input name='category' type='checkbox' dir='ltr' value='9,' /.>
<input name='category' type='checkbox' dir='ltr' value='10,' />
<input name='category' type='checkbox' dir='ltr' value='11,' />
<input name='category' type='checkbox' dir='ltr' value='12,' />
<input name='category' type='checkbox' dir='ltr' value='13,' />

I want to $_POST['category'] and if all are checked I would get

9,10,11,12,13,

Is this even possible?

Thanks,

Josh

Share this post


Link to post
Share on other sites
Try this: [code]<input name='category[]' type='checkbox' dir='ltr' value='9,' /.>
<input name='category[]' type='checkbox' dir='ltr' value='10,' />
<input name='category[]' type='checkbox' dir='ltr' value='11,' />
<input name='category[]' type='checkbox' dir='ltr' value='12,' />
<input name='category[]' type='checkbox' dir='ltr' value='13,' />[/code]
You can then try this on your processing page to see how its structured:
[code]<?php
echo "<pre>";
print_r($_POST['category']);
echo "</pre>";
?>[/code]

Share this post


Link to post
Share on other sites
That works great for getting the values together, thanks.

However it inserts "Array" into the db table and not the numbers.

could I then insert the numbers as

$categery[$i] after a count and a for stamement or am I over thinking this??

Share this post


Link to post
Share on other sites
Yeah, that should be fine, you could probably even do it with a foreach() for less typing...
[code]<?php
$str = "";
foreach($_POST['category'] as $x) {
    $str = $str.$x.",";
}
$str = substr($str,0,-1);
?>[/code]

Share this post


Link to post
Share on other sites
Thanks that works great,

And thanks for the -1 in the substr(), that would have been my next question.


Josh

Share this post


Link to post
Share on other sites
[code]$db_cats = join (',' , $_POST['category'];[/code]

A better table design would be to to use a separate table to hold categories. so if item #1 is in categories 1,5,7 and item #2 is in cats 5,6 you'd have

[code]itemid  |  catid

   1    |    1
   1    |    5
   1    |    7
   2    |    5
   2    |    6[/code]

Now it's easy to query which cats an item belongs to and also which items are in a particular category

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.