Jump to content

explode? array? list? need to break values from msql text box for select list.


antonyfal

Recommended Posts

Hi,

In my mysql database i have a text input option, in the registration form and edit my details form i have a multiple select dropdown list, which user selects options to populate the text input box, which ultimately populates the text field in the mysql database.

All works perfectly.

 

The dropdownlist consists of 3 parts <optgroups> first is current selection (what is the usesr current selection)works fine, The second <optgroup> is existing words, what words we(the site) have given as options, and the third <optgroup> is the words that others have used. This is where im having a small problem.

Because its a text field when i call the data from the database, it calls the entire text box as a single option in my select list..

 

I want to break the words in the text field (at the comma) and have them listed each one as an option in the select list.

 

 

Example what i need:

 

Words in text box:(my input allows the "comma")

word1, word2, word3, word4, word5, word6,

 

How i want them called/displayed:

<option value=\"word1\">word1</option>

<option value=\"word2\">word2</option>

<option value=\"word3\">word3</option>

<option value=\"word4\">word4</option>

<option value=\"word5\">word5</option>

<option value=\"word6\">word6</option>

 

here's my code:

$query = "SELECT allwords FROM #__functions_experience WHERE  profile_id  = '".(int)$profileId."' LIMIT 1";

$original_functionsexperience =doSelectSql($query,1);

$query = "SELECT allwords FROM #__functions_experience WHERE  profile_id  = '".(int)$profileId."' LIMIT 1";

$functionsexperiencelist=doSelectSql($query);

    $funcexpList ="<select multiple=\"multiple\" onchange=\"setFunctionsexperience(this.options)\">";

foreach ($functionsexperiencelist as $functionsexperienceal)

{

$selected="";

if ($functionsexperienceals->allwords == $original_functionsexperience)

$selected=' selected="selected"';

$allwords=$functionsexperienceal->allwords;

$funcexpList .= "<optgroup label=\"Current selection\">

<option value=\"".$allwords."\" ".$selected." >".$allwords."</option>

            </optgroup>

        <optgroup label=\"Existing Words\">

    <option value=\"existing1,\">existing1</option>

    <option value=\"existing2,\">existing2</option>

    <option value=\"existing3,\">existing3</option>

    <option value=\"existing4,\">existing4</option>

    <option value=\"existing5,\">existing5</option>

    <option value=\"existing6,\">existing6</option>

</optgroup>

<optgroup label=\"Others added\">    //heres problem

<option value=\"".$allwordsgeneral."\">".$allwordsgeneral."</option>

</optgroup>";

}

$funcexpList.="</select>";

$output['FUNCEXPLIST']=$funcexpList;

 

 

The result im getting for optgroup others added:

word1, word2, word3, word4, word5,

 

how can i get it like this:

<option value=\"word1\">word1</option>

<option value=\"word2\">word2</option>

<option value=\"word3\">word3</option>

<option value=\"word4\">word4</option>

<option value=\"word5\">word5</option>

<option value=\"word6\">word6</option>

 

 

 

Link to comment
Share on other sites

If all $allwordsgenral contains a comma delimited list of words then change

         <optgroup label=\"Others added\">    //heres problem
         <option value=\"".$allwordsgeneral."\">".$allwordsgeneral."</option>
         </optgroup>";

to

         <optgroup label=\"Others added\">\n";
        $words = explode(',', $allwordsgeneral);
        foreach($words as $word)
            $funcexpList .= "<option value=\"".$word."\">".$word."</option>\n";

        $funcexpList .= "</optgroup>";

 

NOTE: When posting code please wrap it within


or


) tags.

 

Link to comment
Share on other sites

ahh sory here is where it is defined:

 

$query = "SELECT DISTINCT profile_functionsexperience FROM #__functions_experience WHERE published = '1' ORDER BY profile_functionsexperience ASC";

$allwordsgeneral=doSelectSql($query);

foreach ($allwordsgeneral as $allwordsalsgeneral)

{

$profile_allwordsgeneral=$allwordsalsgeneral->profile_functionsexperience;

}

Link to comment
Share on other sites

SORRY YOU WERE CORRECT!

i only added the commas to the options array after i already saved the first test to the database. So i was explodeing the data at an invisible "comma"  :-[ its working perfectly now--

thanks for the assistance..

I did however remove the "$funcexpList .= "" from the code you gave me-- it worked either way.

regards

Antony

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.