Page 1 of 1

[SOLVED] Working with comma-delimited lists

Posted: Tue Jan 18, 2005 9:07 pm
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?

Posted: Tue Jan 18, 2005 9:12 pm
by feyd

Posted: Tue Jan 18, 2005 9:24 pm
by timvw
usually fgetcsv is more than enough ;)

Posted: Tue Jan 18, 2005 9:26 pm
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.

Posted: Tue Jan 18, 2005 9:29 pm
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. :)

Posted: Tue Jan 18, 2005 9:30 pm
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)

Posted: Tue Jan 18, 2005 9:40 pm
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.

Posted: Tue Jan 18, 2005 9:47 pm
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'])

Posted: Tue Jan 18, 2005 9:57 pm
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. :)