Jump to content

Populate select options


kurt.santo

Recommended Posts

I would like to display all counties stored in db in drop down select field, but cannot make it work to show the county name. The code (which works fine to display the number is:

      $query = "SELECT * FROM county ORDER BY county_name ASC";	  $result = @mysqli_query($dbc, $query);	  while ($row = mysqli_fetch_array ($result, MYSQLI_NUM))	{	  echo "<option value=\"$row[0]\"";	  echo ">$row[1]</option>\n";	  }

Each time I change last two lines to:

	  echo "<option value=\"$row['county_name']\"";	  echo ">$row['county_name']</option>\n";

The page does not display in browser.Where is my mistake?Kurt

Link to comment
Share on other sites

while ($row = mysqli_fetch_array ($result)) { //the MYSQLI_NUM constant will stop the relational keys from being setecho "<option value=\"{$row['county_name']}\""; //Proper instring var syntaxecho ">{$row['county_name']}</option>\n"; //"}

Link to comment
Share on other sites

Synook,The population of the drop-down works now. Cheers. Unfortunately, select as opposed to input throws up anohter problem. County is a required field. THe code now is

      <select name="county[]">      <option value="select">Select county</option>      <?php      $query = "SELECT * FROM county ORDER BY county_name ASC";	  $result = @mysqli_query($dbc, $query);	  while ($row = mysqli_fetch_array ($result))	{	  echo "<option value=\"{$row['county_name']}\"";	  echo ">{$row['county_name']}</option>\n";	  }      ?>      </select> 

I use basic validation to display "County*" when form is displayed first and 'County is a required field" if user presses submit without selecting the county. My problem now: County is always set, even if it is just the first "Select county" option. THis is obviously not what I want. The user has to select a county from the list. How can you check for county, but disregard the first <option> as such? My current field validation is as:

		if (!isset($_POST['county']) OR empty($_POST['county'])) {		$county = FALSE;		$errors['county'] = '\'County\' is a required field';		} else {			$county = $_POST['county'];			} 

Also, the result of the insert query into db as

	$query = "INSERT INTO users (county) VALUES ('$county')";

inserts now only the word "Array" into field county. I realise now that I should have checked somehow for the chosen option and inserted this value into db, but again.... Arrays are my biggest weakness. I would appreciate any suggestions:-)Kurt

Link to comment
Share on other sites

I corrected my validation (problem still exists) to:

	if (!isset($_POST['county']) && (is_array($_POST['county']))) {		$county = TRUE;		} else {		$county = FALSE;		$errors['county'] = '\'County\' is a required field';			} 

Realised that I could not test an array in the way I did. Still, my problem exists. Does anyone know how to exclude the first option of the array, so I can make sure the user selected a county?Kurt

Link to comment
Share on other sites

if (isset($_POST['county']) && $_POST['county'] != "Select county") ...

Link to comment
Share on other sites

	if (isset($_POST['county']) && $_POST['county'] != "Select county") {		$county = TRUE;		} else {		$county = FALSE;		$errors['county'] = '\'County\' is a required field';			} 

and select box as:<select name="county[]"> <option value="select">Select county</option> <?php $query = "SELECT * FROM county ORDER BY county_name ASC"; $result = @mysqli_query($dbc, $query); while ($row = mysqli_fetch_array ($result)) { echo "<option value=\"{$row['county_name']}\""; echo ">{$row['county_name']}</option>\n"; } ?>Would I not still need to check for an array somehow? Also, when I did a test registering it enters now 1 into the county (instead of the county name)... Arrays really give me a headache. I cannot get my head around it|-)Kurt

Link to comment
Share on other sites

Oh I see, its a multiple select.

if (!isset($_POST['county']) && (is_array($_POST['county'])) && !in_array("select", $_POST['county'])) {

Link to comment
Share on other sites

Oh I see, its a multiple select.
			$query = "INSERT INTO users (email, pass, first_name, last_name, company, telephone, address1, address2, address3, town, county, postcode, country, source, other, active, registration_date) VALUES ('$email', SHA('$pass'), '$first_name', '$last_name', '$company', '$telephone', '$address1', '$address2', '$address3', '$town', '$county', '$postcode', '$country', '$source', '$other', '$a', NOW() )";	

2. I set up empty echo variable at the top of my script and then use the field validation to assign the $_POST value to make the form field sticky if entries passed relevant validation. How could I do the same for my counties? Am not sure how to insert into

		  echo "<option value=\"{$row['county_name']}\"";	  echo ">{$row['county_name']}</option>\n";with validation as:// check for countyif (isset($_POST['county']) && (is_array($_POST['county'])) && !in_array("select", $_POST['county'])) {		$county = TRUE;$county_echo = $county;} else {$county = FALSE;$errors['county'] = '\'County\' is a required field';} 

It is a bit of a pain to loose the county if you press submit, but you entered another field incorrectly...Kurt

Link to comment
Share on other sites

Well I don't understand, from your form's code it would seem you wanted people to be able to enter multiple counties, which won't fit into one field...

Link to comment
Share on other sites

Well I don't understand, from your form's code it would seem you wanted people to be able to enter multiple counties, which won't fit into one field...
No, I meant to enable them to only select one county. It is for their address. Where did I go wrong?Kurt
Link to comment
Share on other sites

You used an array as the name of the select. Arrays are used when you want multiple values to be selectable. Make the name of the select not an array (e.g. name="county") and modify the if accordingly.

if (isset($_POST['county']) && $_POST['county'] != "select") {

Link to comment
Share on other sites

You used an array as the name of the select. Arrays are used when you want multiple values to be selectable. Make the name of the select not an array (e.g. name="county") and modify the if accordingly.
if (isset($_POST['county']) && $_POST['county'] != "select") {

That solved problem 1, cheers. Thought because there are more than one option to select it would need to be an array... Thanks.Do you know any chance how to make the selected option sticky (I gave an example of my code under 2)? If thats possible that would be very user-friendly (I did it for the input and textarea form fields and love it!)...Kurt
Link to comment
Share on other sites

I acually found an example how to make select sticky. It is as

echo '<select name="s_list">';while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){	$selected = '';   // initialize "selected" attribute to nothing for each particular <option>	/* If the current MySQL row value-column (for the 's-list' <select>) is set (submitted through POST in this case) and is equal to the current row-column value, set the $selected text attribute to true, which will be concatenated to this <option> */	if((isset($_POST['s_list'])) && ($_POST['s_list'] == $row['column1'])){		$selected = ' selected="selected" ';	}	echo '<option value="' . $row['column1'] . '"' . $selected . '>' . $row['column2'] . '</option>';}echo '</select>';

My loop differs:

while ($row = mysqli_fetch_array ($result))	{	  echo "<option value=\"{$row['county_name']}\"";	  echo ">{$row['county_name']}</option>\n";	  }

Should I change my code accordingly? Not sure which is best option...Kurt

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...