Combine string into multiple option

A

Anonymous

Guest
I use 'explode' function to split a string (from multiple selection) to be inserted into mysql text field.

this is my multiple selection code
Code:
echo "<select name='bcc[]' size='5' multiple>";
$query = "select value, label from bcc order by label asc";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
    echo '<option value='.$row['value'].'>'.$row['label'].'</option>';
}
mysql_free_result($result);
echo "</select>";

And this is my query and 'explode' function
Code:
$bcc = explode(",", $bcc);
$query2 = "INSERT INTO maintable (nama, org, bcc) VALUES ('$nama', '$org', '$bcc')";

It works fine when inserted into text field. What I got in text field is similar like this:
ABC,DEF,GHI,JKL

What I want is how to pull that string (to be updated) into multiple selection so that it will appears like this
____________
ABC
DEF
GHI
JKL
____________
and it will select any options that was selected previously

And one more question:
How to have my browser address show PHP session id
sample:http://www.php-forum.com/p/login.php?sid=e461ef9f4deb7f8d31c44aac1e592f0a

Thanks
 
wmdrumaizi, I'm a little confused as to what you're trying to do here:

wmdrumaizi said:
And this is my query and 'explode' function
Code:
$bcc = explode(",", $bcc);
$query2 = "INSERT INTO maintable (nama, org, bcc) VALUES ('$nama', '$org', '$bcc')";

Perhaps you don't understand what explode() does. explode() takes a delimited string (like your comma-separated values) and turns them into an array, with each value in an element of the array.

But in your code above, you seem to be taking the array ($bcc) and putting it directly in an SQL INSERT statement. You can't do that, as trying to directly output an array as a string will just print "Array". See what I mean by adding echo $query2; to the end of your code above.

wmdrumaizi said:
What I want is how to pull that string (to be updated) into multiple selection so that it will appears like this
____________
ABC
DEF
GHI
JKL
____________
and it will select any options that was selected previously

Once you have your values exploded into an array, you need to use a loop (such as while() or foreach(), I prefer foreach()) to get each of the values, one at a time, and output them as an HTML select option. If you want to make certain menu items selected, you must add "selected" to their HTML option tag. So, it'll look something like this:

Code:
<?
   // the items
   $items = 'ABC,DEF,GHI,JKL';

   /* Since this is a multiple select, you'll want to have 
      whichever items are to be selected in an array of their own
      -- it's up to you how you get them there. For the purpose 
      of this demonstration, we'll select 'ABC' and 'GHI'.
   */
   $selected = array('ABC', 'GHI');

   // explode the items into an array (and give it a new name)
   $items_array = explode(',', $items);

   // start your HTML multi-select
   echo "<select multiple name="bcc" size="5">\n";

   /* Now loop through the array and print each item as an
      option. Add "selected" if the item is in the $selected
      array.
   */
   foreach($items as $this_item) {
      echo  '<option value="', $this_item, '" .
            (in_array($this_item, $selected) ? ' selected>' : '>') .
            $this_item . "</option>\n";
   }

   // close the HTML multi-select
   echo "</select>\n";
?>

I hope that's helpful. I'm not sure how your database table is set up, but it might be tricky for you to get the $selected values right.

wmdrumaizi said:
And one more question:
How to have my browser address show PHP session id
sample:http://www.php-forum.com/p/login.php?sid=e461ef9f4deb7f8d31c44aac1e592f0a

Just tack the session id onto the end of your URL when you create a link or a form. You can use session_id() to retrieve the current session id.
 
Swirlee; thanks for your concern.

Actually I used implode instead of explode (sorry for the mistake)

Code:
$bcc = implode(",", $bcc);

I tyr your code:
Code:
foreach($items as $this_item)
              {
              echo  '<option value="', $this_item, '" . (in_array($this_item, $selected) ? ' selected>' : '>') . $this_item . "</option>\n";
              }

But it gave me an error:
Parse error: parse error, expecting `','' or `';'' in C:\apache\htdocs\researchers\update.php on line 244

Line 244 point to your code...
Could you please show me what cause the error...

Thanks and regards
 
Read what the error says, and you can probably work it out.

Parse error: parse error, expecting `','' or `';'' in C:\apache\htdocs\researchers\update.php on line 244

This says the script was expecting a comma or semicolon on line 244 but there was not one.

This code should work.

Code:
foreach($items as $this_item) { 
	echo  "<option value=\"", $this_item, "\"" . (in_array($this_item, $selected) ? ' selected>' : '>') . $this_item . "</option>\n"; 
}
 
Thanks for cleaning up after me, Joel. There's a reason I have the disclaimer in my sig. :wink:
 
Thanks guys;

and one more Q:

I have one table with 4 records:
value|label
------------
ABC|ABC
------------
DEF|DEF
------------
GHI|GHI
------------
JKL|JKL
------------

How do I convert those values into this array form:

$items = array('ABC','DEF','GHI','JKL');
 
In your table diagram, are the values and labels in to different columns, or are they both in the same column as a string, separated by |? I wasn't sure by your description.

Anyway, if they're in different columns, you just need to initialize the target array before you loop through your query results and for each row, add the value to the array.
 
Back
Top