[SOLVED] Working with comma-delimited lists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

[SOLVED] Working with comma-delimited lists

Post by Seona »

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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

usually fgetcsv is more than enough ;)
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

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.
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

Err, I'm not sure that getcsv one is what I need. I'm getting the values of the list passed as the value of a form field, not from a file. And from what I've read in the link you supplied, getcsv is for use with files only.

Correct me if I'm wrong though. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

name the field with [] at the end like so:

Code: Select all

<select name="whatever&#1111;]" multiple="multiple">
$_POST['whatever'] is now an array. (if you're using post, of course)
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

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:

Code: Select all

import_request_variables('g', 'g_');
//check if the form has been submitted
if (isset($g_submit)) &#123;
	print_r(explode(",",$g_Sections));
&#125;
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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't need explode when it's already an array of the values selected...

Code: Select all

print_r($_GET&#1111;'Sections'])
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

Ah, that works much better. :) I now have the following:

Code: Select all

if (isset($g_submit)) &#123;
	foreach ($_GET&#1111;"Sections"] as $sect) &#123;
		print_r(explode(",",$sect));
	&#125;
&#125;
And that works just as I need it to. Thanks once again for your help. :)
Post Reply