Jump to content

Looping an array back into a multiple select field


alhen

Recommended Posts

Hi there,

I'm trying to populate a multiple select field from mysql. I already have the info stored in mysql, and separated by "<br>". Now I'm calling that info back like this:

 

<? 
	$pieces = explode("<br>", $society_inf); // society_inf is the mysql field name AND the variable name, for simplicity
	?>
	<select name="society_inf[]" multiple="multiple" id="society_inf" tabindex="290">
	<?php if($pieces[0] !== ""){echo "<option value=\"$pieces[0]\" selected>$pieces[0]</option>"; } else { echo "";}?>
	<?php if($pieces[1] !== ""){echo "<option value=\"$pieces[1]\" selected>$pieces[1]</option>"; } else { echo "";}?>
	<?php if($pieces[2] !== ""){echo "<option value=\"$pieces[2]\" selected>$pieces[2]</option>"; } else { echo "";}?>
	<?php if($pieces[3] !== ""){echo "<option value=\"$pieces[3]\" selected>$pieces[3]</option>"; } else { echo "";}?>
	<?php if($pieces[4] !== ""){echo "<option value=\"$pieces[4]\" selected>$pieces[4]</option>"; } else { echo "";}?>
	<option value="AAPM – American Academy of Pain Medicine" selected>AAPM – American Academy of Pain Medicine</option>
	<option value="APS – American Pain Society">APS – American Pain Society</option>
	<option value="AACE – American Association of Clinical Endocrinologists">AACE – American Association of Clinical Endocrinologists</option>
	<option value="ECE – European Congress of Endocrinology">ECE – European Congress of Endocrinology</option>
	<option value="ENDO – The Endocrine Society">ENDO – The Endocrine Society</option>
	<option value="ENEA – European Neuro Endocrine Association">ENEA – European Neuro Endocrine Association</option>
	<option value="ESPE – European Society for Paediatric Endocrinology">ESPE – European Society for Paediatric Endocrinology</option>
	<option value="LWPES – Lawson-Wilkins Pediatric Endocrine Society">LWPES – Lawson-Wilkins Pediatric Endocrine Society</option>
	</select>

 

Here's what it looks like: (the two selected ARE supposed to be)

box.jpg

You can see this looks kind of ugly, but it's almost working for me.

 

My question is:

Is there a way to loop this array to where it only shows me the selected items (still selected) and the non-selected items (still NOT selected), but no duplicates?

 

I'd like it to look like this:

box2.jpg

 

[attachment deleted by admin]

Link to comment
Share on other sites

Ok, I'm not getting any hits so I thought I'd try to jump start something...

 

What I imagine happening is something like this:

<?php if($pieces[] == "society_inf[]"){echo "<option value=\"$pieces[0]\" selected>$pieces[0]</option>"; } else { echo "";}?>

 

I know that's not right, but is there a way to loop this information through kinda like this?

Link to comment
Share on other sites

For example I dont understand what u want todo.

I dont get it this thing how data on database is diferent from this

	<option value="AAPM – American Academy of Pain Medicine" selected>AAPM – American Academy of Pain Medicine</option>		<option value="APS – American Pain Society">APS – American Pain Society</option>		<option value="AACE – American Association of Clinical Endocrinologists">AACE – American Association of Clinical Endocrinologists</option>		<option value="ECE – European Congress of Endocrinology">ECE – European Congress of Endocrinology</option>		<option value="ENDO – The Endocrine Society">ENDO – The Endocrine Society</option>		<option value="ENEA – European Neuro Endocrine Association">ENEA – European Neuro Endocrine Association</option>		<option value="ESPE – European Society for Paediatric Endocrinology">ESPE – European Society for Paediatric Endocrinology</option>		<option value="LWPES – Lawson-Wilkins Pediatric Endocrine Society">LWPES – Lawson-Wilkins Pediatric Endocrine Society</option>

 

and why some options u retriving from db and some just write staticly?

Link to comment
Share on other sites

hmmm... I'm not really sure how else to explain this. The images I included show what I am trying to achieve.

 

Lets say the multiple select field has:

 

Option 1

Option 2

Option 3

Option 4

 

And I selected:

Option 1

Option 4

 

Only those 2 are entered into the database... now - what I want to do is display the the full menu, with Option 1 & 4 still selected - and Option 2 & 3 not selected. Like this:

 

Option 1 (selected)

Option 2 (not selected)

Option 3 (not selected)

Option 4 (selected)

 

As it stands now it looks like this:

Option 1 (selected)

Option 4 (selected)

Option 1 (not selected)

Option 2 (not selected)

Option 3 (not selected)

Option 4 (not selected)

 

As you can see there are duplicate options 1 & 4. Have I clarified this at all or have I obfuscated the question?

Link to comment
Share on other sites

I am not sure if I need Javascript or Ajax. I assumed it would be PHP based.

 

You are correct... I am only collecting this data, to be displayed again in another multiple-select box, and possibly changed. This is an internal tool only, to keep track of information.

Link to comment
Share on other sites

One more question: what is the point of showing selected items in another select box? whwn they user allready sees what he selected in the first box?

 

or maybe what u realy want to show in another box is sub categories?

for example when i click on AACE y display:

Aaaaaaaaaaaaaa

Bbbbbbbbbbbbbb

Cccccccccccccccccc

 

ECE displays:

EEEEEEEEEEEE

FFFFFFFFFFFFFF

TTTTTTTTTTTTTT

 

?

Link to comment
Share on other sites

I have already explain this, here:

I am only collecting this data, to be displayed again in another multiple-select box, and possibly changed.

It is because I don't need to simply display the data... I'm already way past that. I need to be able to alter it again because it may change periodically. And I do not need sub-categories. I need it how I've described in my previous posts.

 

I really do appreciate your questions, but do you have any actual advice that can guide me in the right direction?

Link to comment
Share on other sites

Well maybe I could help but I realy want to understand what u trying to make.

 

The way I see now is that u realy need javascript+ajax, cuz u are playing with UI.

You say that "and possibly changed" who changes that stuf how often it's changed? by user that is clicking on the forms or the server which each 10min updates information on database(in this case u will need use ajax or refresh page in old fashon manner)

W/O javascript you even will not be able to add new options into select boxes dinamiclly unles u want to click "submit" button every time u have selected your chooses.

 

Heres how some tool could be done short pseudocode

<html>
<head>
<script type="text/javascript">
function clone(sender)
{
var size = document.myform.box_b.options.length;
document.myform.box_b[size] = new Option(sender.text, sender.value, false, true);
}
</script>
</head>
<body>
<?php
//retriive data from mysql database
echo "<form name='my_form'>";
echo "<select multiple=multiple name='box_a'>";
while($row = mysql_fetch_assoc($result))
{
   echo "<option value='".$row['id']."' onclick='clone(this)'>".$row['title']."</option>";
}
echo "</select>";

echo "<select multiple='multiple' name='box_b'></select>";
echo "</form>";
?>
</body>
</html>

Link to comment
Share on other sites

  • 1 year later...

<?

//data from DB

$Z3b = $row['Z3b'];

$data =explode(",",($Z3b));

print_r($data);

 

// Array ( [0] => 1194.21 Software applications and operating systems [1] => 1194.22 Web-based intranet and internet information and applications [2] => 1194.23 Telecommunications products )

?>

I need to highlight the data that is stored in db in order to check/edit for user.

 

<select disabled=disabled name="Z3b" multiple='multiple' class="required" disabled>

<option value="1194.21 Software applications and operating systems">1194.21 Software applications and operating systems</option>

<option value="1194.22 Web-based intranet and internet information and applications">1194.22 Web-based intranet and internet information and applications</option>

<option value="1194.23 Telecommunications products">1194.23 Telecommunications products</option>

<option value="1194.24 Video and multimedia products">1194.24 Video and multimedia products</option>

<option value=" 1194.25 Self contained, closed products"> 1194.25 Self contained, closed products</option>

<option value="1194.26 Desktop and portable computers">1194.26 Desktop and portable computers</option>

</select>

Link to comment
Share on other sites

Is the options statically coded, or are they dynamic also?

 

My option would be to set the options in an array, then check them against the database as you build the select box.

 

<?php
$options = array('AAPM – American Academy of Pain Medicine','APS – American Pain Society','AACE – American Association of Clinical Endocrinologists','ECE – European Congress of Endocrinology',
			'ENDO – The Endocrine Society','ENEA – European Neuro Endocrine Association','ESPE – European Society for Paediatric Endocrinology','LWPES – Lawson-Wilkins Pediatric Endocrine Society');
$pieces = explode("<br>", $society_inf); // society_inf is the mysql field name AND the variable name, for simplicity
?>

	<select name="society_inf[]" multiple="multiple" id="society_inf" tabindex="290">
	<?php 
		foreach($options as $value) {
			$selected = (in_array($value,$pieces)) ? 'selected="selected"' : NULL;
			echo '<option ' . $selected . '>' . $value . '</option>';
		}
	?>
	</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.