PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Try It Free
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old August 13th, 2002, 03:56 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
Multiple select and mySQL

Hello people.

I love the turtorials on this site. Finally Im starting to get a hang of PHP and mySQL.

Im fooling around with the "Create dynamic sites with PHP & MySQL" turtorial.

I want to add a user category list. Users should be able to be in many categories.
I have coded the form to use for displaying the diffrent categories available. But I got problems figuring out how to store which category each user have selected.

I have set the categories up in its own table. And Im usig this code to show the categories.

while ($own_row=mysql_fetch_array($own_result))
{
echo "<select name="categories" size="10" multiple>";
echo " <option value="".$own_row["category_id"]."">".$own_row["categories"]."</option>";
}
echo "</select>";

But when I submit the form, only the last selected category is stored in mySQL.


Reply With Quote
  #2  
Old August 13th, 2002, 09:13 PM
zombie zombie is offline
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: serbia
Posts: 1,876 zombie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 3
RE: Multiple select and mySQL

try setting a name of your select to 'categories[]' like this:
php Code:
Original - php Code
  1.  
  2. echo "select name='categories[]' size='10' multiple>";


and then u can use foreach in your php script to see all the selected categories, like this:

php Code:
Original - php Code
  1.  
  2. foreach ($_POST['categories'] as $cat) {
  3.   echo $cat;
  4.   // or do smth else with it
  5. }

Reply With Quote
  #3  
Old August 14th, 2002, 12:03 AM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
RE: Multiple select and mySQL

Thanks, so I could then pipe the array and pass it to mySQL. Interesting....

Thanks m8

Reply With Quote
  #4  
Old August 14th, 2002, 03:26 AM
siteworkspro.com siteworkspro.com is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Sydney, Australia
Posts: 92 siteworkspro.com User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to siteworkspro.com Send a message via AIM to siteworkspro.com
RE: Multiple select and mySQL

if you want to store the selection in a database or file, use this peice of code.

(ive modded it to work with your vars)

var $cats;
foreach($categories as $id)
{
$cats .= $id . ',';
}
$cats = substr($cats, 0, -1);

that way it will print out your array as something like this:

$cats = row1,row3,row6 //what ever you have selected

then you can store it in a database, then when you want to grab the array, just explode it

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Multiple select and mySQL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway