Jump to content

[SOLVED] Convert CSVs to Select Option Values


11t1

Recommended Posts

Hi, I was just wondering if someone could point me to a good web resource that could help me figure out how to convert a string of comma separated values (being pulled from a database) into a select menu (pulldown box) so that the values from the CSV would automatically fill in the displayed text area of the option tags as well as the value attribute so the value selected could actually be passed as a variable. I imagine it has something to do with converting the CSV list into an array using substring parsing & then dynamically building the array from the elements of the array, but I just haven't been able to put it all together yet. If anyone is familiar with anything published on the web that might help, I'd be deeply appreciative.

 

thx - stv

LOL

 

$text='lots, and lots, and lots of words'; #string with ,es
$array=explode(',',$text);#turns it into an array... $array=array("lots","and lots","and lots of words");
echo '<select>';
foreach($array as $k=>$v){ #for each of the array, split it into its $k=key, $v=value
echo '<option value="'.$k.'">'.trim($v).'</option>';
}
echo '</select>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.