[SOLVED] Working with comma-delimited lists
Moderator: General Moderators
[SOLVED] Working with comma-delimited lists
Hi guys,
Quick (and hopefully easy) question. If I have a variable that contains a comma-delimited list, how can I isolate the items in it so I can work with them separately? In this specific case, there will only be two items.
What I am wanting to do is to pass more than one number as the value of some <option> tags and have the processing script work first with one number and then with the other.
If you look at this page: http://www.dare2.com.au/productsservices.php you will see the list that I am working with. I want each option in the list to pass a value consisting of CategoryID,SectionID so that I can use that to control the output.
Any suggestions?
Quick (and hopefully easy) question. If I have a variable that contains a comma-delimited list, how can I isolate the items in it so I can work with them separately? In this specific case, there will only be two items.
What I am wanting to do is to pass more than one number as the value of some <option> tags and have the processing script work first with one number and then with the other.
If you look at this page: http://www.dare2.com.au/productsservices.php you will see the list that I am working with. I want each option in the list to pass a value consisting of CategoryID,SectionID so that I can use that to control the output.
Any suggestions?
Ah! You know, I'd come across that function once before when I was looking for something else, but it never occurred to me that it would be the thing to use for doing this. Thanks. 
It seems to work just fine for what I want, but I have one slight problem: because I'm drawing from a multiple-select box in the form, I end up with essentially the same variable being passed with different values. How do I manage to explode them all seperately? At present, I'm just getting the values of the last option selected.
It seems to work just fine for what I want, but I have one slight problem: because I'm drawing from a multiple-select box in the form, I end up with essentially the same variable being passed with different values. How do I manage to explode them all seperately? At present, I'm just getting the values of the last option selected.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
name the field with [] at the end like so:
$_POST['whatever'] is now an array. (if you're using post, of course)
Code: Select all
<select name="whateverї]" multiple="multiple">Cool. I'm still a little hazy on arrays though, it seems. I've made the multiple-select pass into an array as you suggested, and have this in the processing script:
This doesn't seem to work, and I'm sure it's because I have to do something different not that the "Sections" field is an array. I'm not sure what I need to do though.
Sorry for being a pest.
Code: Select all
import_request_variables('g', 'g_');
//check if the form has been submitted
if (isset($g_submit)) {
print_r(explode(",",$g_Sections));
}Sorry for being a pest.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
you don't need explode when it's already an array of the values selected...
Code: Select all
print_r($_GETї'Sections'])Ah, that works much better.
I now have the following:
And that works just as I need it to. Thanks once again for your help. 
Code: Select all
if (isset($g_submit)) {
foreach ($_GETї"Sections"] as $sect) {
print_r(explode(",",$sect));
}
}