1 Replies - 694 Views - Last Post: 03 August 2012 - 07:40 AM Rate Topic: -----

#1 zukeru  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 104
  • Joined: 25-December 09

How to set the selected option of a combobox from string from database

Posted 03 August 2012 - 07:20 AM

ok so I need some help again



'Code to load database here I just dont want to type it out it is not needed for this. 

$string = $databasevalue

echo' <select name='LAS' value=$String>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>

'ive also tried

echo' <select name='LAS' selected=$String>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>




as you can see none of that code works, I need this because when I click update I goto this php page and I need it to laod the values and set them to what exist already in the database. Then when you click submit on this page it writes the $string value please help so that every time I come to this page the selected value = that of the database.

Is This A Good Question/Topic? 0
  • +

Replies To: How to set the selected option of a combobox from string from database

#2 xxxjj18  Icon User is online

  • D.I.C Head
  • member icon

Reputation: 49
  • View blog
  • Posts: 163
  • Joined: 30-November 11

Re: How to set the selected option of a combobox from string from database

Posted 03 August 2012 - 07:40 AM

Firstly, using " ' " in PHP is not valid for comments.

Secondly, when you echo'd the forms out, you used the same single quote character in the form that is used to encase the data to be echo'd; this will not parse correctly.

Here are a few legal examples of accomplishing what you were attempting:


echo "<select name='LAS'>";

//Or

echo '<select name=\'LAS\'>';

//Or

echo '<select name="LAS">';

//Or

echo "<select name=\"LAS\">";




If you use the same quotation character in the data echo'd that is used to encapsulate it, then you must escape the quotation character; hence the backslashes.

Now, to your question.

I don't understand; you would like to load the combo box with the values from the database? Or you would like to have a premade combo box and make the selected value whatever value is in the database?

If you would like to populate the combo box with values in the database, then you could save all of the database values to a variable and loop through that variable and add them that way;

try {
		$sql = "SELECT * FROM $table";
			
		$sth = $this->dbh->prepare($sql);
		$sth->execute();
		
		$array = $sth->fetchAll();
		
		if(!empty($array)) {
			$row =  $array;
		}
		
}
catch(PDOException $e) {
	print $e->getMessage();
	return false;
}

//This code is an example to retrieve the data from the DB, now to use it

print "<select name='LAS'>";

foreach($row as $single_row) {
	
	print "<option value=\"{$single_row["value"]}\">{$single_row["value"]}</option>";
	
}

print "</select>";



Something like this, though I'm not sure where the select's "selected" value would be set.

Does this help somewhat, or was your question aimed at a different issue?

Edit:

Also, to have an item in a combo box selected by default, you do it for that individual option; not in the <select> tag;


<select>
  <option selected="selected">Test</option>
  <option>Test</option>
  <option>Test</option>
</select>



This post has been edited by xxxjj18: 03 August 2012 - 07:43 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1