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

Link to comment
Share on other sites

assuming what your meaning...

 

$text='lots, and lots, and lots of words';
$array=explode(',',$text);
echo '<select>';
foreach($array as $k=>$v){
echo '<option value="'.$k.'">'.trim($val).'</option>';
}
echo '</select>';

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.