Since you specifically asked about Smarty templating, this is actually pretty easy. Use the html_options template from their example file and pass an identifier to the selected attribute.
Contents of index.tpl:
{html_options name=fruits options=$myOptions selected=$mySelect}
The only real messy part is getting the array populated from the database. Since you didn't specify that platform, I used MySQL.
First I set up a simple database table to model your example with a boolean column for selected state. This can be constrained or set however you like, but note for this example if multiple are set to true, then it will use the last one selected. And I used ID's as a bonus because it will probably be more useful for a non-trivial scenario. If you still want to use the same value as the option text, then just find and replace $row['id'] with $row['fruit']
DROP TABLE IF EXISTS fruits;
CREATE TABLE fruits (
id INT PRIMARY KEY AUTO_INCREMENT,
fruit VARCHAR(30),
selected BOOLEAN
);
INSERT INTO fruits (fruit,selected)
VALUES
('Apples',FALSE),
('Bananas',TRUE),
('Oranges',FALSE);
Now use php to retrieve the data, create the smarty object, set values, and finally display.
<?php
require_once(__DIR__.'/vendor/smarty/smarty/libs/Smarty.class.php');
$database_server = "localhost";
$database_username = "username";
$database_password = "p@55W0rd";
$database_name = "example_database";
// connect to database
$mysqli = new mysqli($database_server, $database_username, $database_password, $database_name);
if ($mysqli->connect_error)
trigger_error('Connect Error: '.$mysqli->connect_error, E_USER_ERROR);
// get records
$sql = "SELECT id, fruit, selected FROM fruits";
$result = $mysqli->query($sql);
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
// set array index of fruit as id
$fruits[$row['id']] = $row['fruit'];
// get id of selected fruit
if ($row['selected'] == true)
$selected = $row['id'];
}
// create object
$smarty = new Smarty;
$smarty->assign('myOptions', $fruits);
$smarty->assign('mySelect', $selected);
// display it
$smarty->display('index.tpl');
And this results in the following output:
<select name="fruits">
<option value="1">Apples</option>
<option value="2" selected="selected">Bananas</option>
<option value="3">Oranges</option>
</select>
Bananasvalue on my HTML (I can show it on any input or whatever). I send to the HTML via PHP using Smarty. And no, I am not using AJAX.$fruitthat contains the valueBananas.if row equalsmaybe? if that's what the question is about. Sidenote: I know nothing about Smarty, so I can't be of any help there.echo ($key == $default) ? "<option selected=\"selected\" value=\"$key\">$val</option>...- If not, then sorry... I am not grasping the question.dropdowneach time. This is because the 'selected' entry is on a different option element. So eachdropdownis different. So, there must be some logic that decides to outputselectedwhen theselect optionhtml is generated. Now, you don't show us the code that generates the select html. That code you need to change by checking theselectedOptionValueagainst the 'currentOptionVlaue'?