Jump to content


Photo

Adding more than one value to a db table field


  • Please log in to reply
5 replies to this topic

#1 homchz

homchz
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts
  • LocationLee, New Hampshire

Posted 20 May 2006 - 08:43 PM

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

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 May 2006 - 08:49 PM

Try this:
<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,' />
You can then try this on your processing page to see how its structured:
<?php
echo "<pre>";
print_r($_POST['category']);
echo "</pre>";
?>


#3 homchz

homchz
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts
  • LocationLee, New Hampshire

Posted 20 May 2006 - 09:04 PM

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??

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 May 2006 - 09:16 PM

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


#5 homchz

homchz
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts
  • LocationLee, New Hampshire

Posted 20 May 2006 - 09:48 PM

Thanks that works great,

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


Josh

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 21 May 2006 - 08:23 AM

$db_cats = join (',' , $_POST['category'];

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

itemid  |  catid

   1    |    1
   1    |    5
   1    |    7
   2    |    5
   2    |    6

Now it's easy to query which cats an item belongs to and also which items are in a particular category
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users