Jump to content

Select boxes in a loop


JasonBruce88

Recommended Posts

Hello I am able to populate a select box with items form a mysql table no problem. But what I am creating is a form that repeats the fields  depending on the users selection on the previous page. This works no problem except for the select box. It fails to select all the results. I have tried many combinations of nested loops but with no joy. In stead of posting all the code I will post the code which generates the select box. Any help or point in the right direction is very much welcome.

 

	    echo "<fieldset>\n";
	echo "<legend>\n";
	  echo "Products required for property\n";
	echo "</legend>\n";
//echo $productItemLoop;
	  $ProductIDArray = "ProductID$propNumber";
	  $ProductIDArrayBrackets = "[]";

while($nt1=mysql_fetch_array($result)){
  
  $product_options_open = "<option value=\"$nt1[ProductID]\">";
  $row_productType = $nt1['ProductType'];
  $product_options_close = "</option>\n";
  
}


	  echo "<p>\n";
	  echo "Hold 'Ctrl' to select more than one product\n";
	  echo "<br />\n";
	    echo "<select multiple";
	      echo " ";
	      echo "name=";
	      echo "\"$ProductIDArray$ProductIDArrayBrackets\"";
	      echo " ";
	      echo "id=\"ProductID$propNumber\" size=\"5\">\n";
		echo "$product_options_open\n";
		echo "$row_productType\n";
		echo "$product_options_close\n";
	      echo "</select>\n";
	  echo "</p>\n";
      echo "</fieldset>\n";

 

Link to comment
Share on other sites

'non selected' data from elements that give an array are lost when submitted.

 

That would be your

<select multiple>

but also for example

<input type='checkbox' value='cows' name='opt[]' /><input type='checkbox' value='sheeps' name='opt[]' />

 

There is no way getting around this besides either sticking all options in <input type='hidden' />  as well, or by simply using static code (or a query) to retrieve the information like you did on the original page.

 

I think HTML5 might offer a solution for this, but too early to use and I'm not even sure if it did.

Link to comment
Share on other sites

If you simply want to read out the values, this is the most basic example I can come up with.

<?php
if( !isset( $_POST['food'] ) )
{
  echo '
  <form action="" method="post">
  <label for="food">Which vegatable would you like for dinner?</label>
  <select name="food[]" MULTIPLE>
    <option value="carrots">carrots</option>
    <option value="peas">peas</option>
  </select>
  <input type="submit" />';
}
else
{
  foreach( $_POST['food'] as $veggy )
  {
    echo $veggy."<br/>";
  }
}
?>

 

[edit]

Forgot to explain :)

The magic part is in the name attribute, it has to end on []. PHP automatically creates an array out of form elements whos name ends on [].

Link to comment
Share on other sites

Your while loop is only producing one OPTION element.  It will be the last one from the database.  You are overwriting the values on each pass.

while($nt1=mysql_fetch_array($result)){
  $product_options_open = "<option value=\"$nt1[ProductID]\">";
  $row_productType = $nt1['ProductType'];
  $product_options_close = "</option>\n";
}

 

You might try rearranging the code just a bit to output the OPTIONS directly:

  echo "<p>\n";
  echo "Hold 'Ctrl' to select more than one product\n";
  echo "<br />\n";
    echo "<select multiple";
      echo " ";
      echo "name=";
      echo "\"$ProductIDArray$ProductIDArrayBrackets\"";
      echo " ";
      echo "id=\"ProductID$propNumber\" size=\"5\">\n";
while($nt1=mysql_fetch_array($result)){
  echo "<option value=\"$nt1[ProductID]\">\n";
  echo $nt1['ProductType'] . "\n";
  echo "</option>\n";
}
      echo "</select>\n";
  echo "</p>\n";

 

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.