-1

I have a select like this:

<select class="fruitsSelect" name="fruits">
   <option value="Apples">Apples</option>
   <option value="Bananas">Bananas</option>
   <option value="Oranges">Oranges</option>
</select>

And I know that you can set a default value if you use selected attribute.

<select class="fruitsSelect" name="fruits">
      <option value="Apples">Apples</option>
      <option value="Bananas" selected>Bananas</option>
      <option value="Oranges">Oranges</option>
</select>

But what I am trying to do is that, from a value that I retrieve from my database (I send it to HTML via PHP) the option that I retrieve from my database would be the selected option (the default option).

I have the value on my HTML but I do not know how to make it be the selected option because it can change. I am using Smarty and trying to make some ifs statements but I cannot get it out.

How can I compare the value that I retrieve from my database with the value of the select and make it to be the selected option?

Thanks in advance!

11
  • @gibberish As I put above, I have the value on my HTML. I have Bananas value 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. Commented Jun 2, 2016 at 0:54
  • @gibberish Right now, as I am using Smarty, I have a variable $fruit that contains the value Bananas. Commented Jun 2, 2016 at 0:56
  • 2
    ternary operator against an if row equals maybe? if that's what the question is about. Sidenote: I know nothing about Smarty, so I can't be of any help there. Commented Jun 2, 2016 at 0:59
  • 1
    and this stackoverflow.com/a/9710493 doesn't work for you? echo ($key == $default) ? "<option selected=\"selected\" value=\"$key\">$val</option>... - If not, then sorry... I am not grasping the question. Commented Jun 2, 2016 at 1:06
  • 1
    First, it isn't the same dropdown each time. This is because the 'selected' entry is on a different option element. So each dropdown is different. So, there must be some logic that decides to output selected when the select option html is generated. Now, you don't show us the code that generates the select html. That code you need to change by checking the selectedOptionValue against the 'currentOptionVlaue'? Commented Jun 2, 2016 at 2:47

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming the Select is hard coded you could use:

<select class="fruitsSelect" name="fruits">
   <option value="Apples" <?php if ($fruit == "Apples") {echo "Selected" }?> >Apples</option>
   <option value="Bananas" <?php if ($fruit == "Bananas") {echo "Selected" }?> >Bananas</option>
   <option value="Oranges" <?php if ($fruit == "Oranges") {echo "Selected" }?>>Oranges</option>
</select>

Note: My Php is a little rusty so forgive any syntax or stupid errors.

If the Select list is Dynamically generated, I suggest looking at what Fred-ii- suggested in comments.

2 Comments

<? doesn't work everywhere out of the box, there's a setting to enable that syntax in php.ini. Use <?php for better portability
@JeffPuckettII thanks for the tip. It has been 10 or more years since I've used PHP in anger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.