Artem Ushakov
Artem Ushakov

Reputation: 313

PHP: select current option

I have a DB table with all categories (id, category) and table with all of events (id, event, categoryID).

And on the event editing form I have the select field with all of the categories (getting from the DB). But sinse I'm developing an editing form, I need to select current category by default.

This is my select field (PHP method that gets all the categories from DB and puts them in the following order):

<option value="1">Cat1</option>
<option value="2">Cat2</option>
<option value="3">Cat3</option>
<option value="4">Cat4</option>
<option value="5">Cat5</option>

Let's say, current event is under category 3, so I need the following HTML to be generated:

<option value="1">Cat1</option>
<option value="2">Cat2</option>
<option value="3" selected>Cat3</option>
<option value="4">Cat4</option>
<option value="5">Cat5</option>

How do I achieve it with PHP, if I have the catID?

Hopefully, this question is clear enough. Sorry for my bad explanation

UPD: This is my PHP code that generates category list:

public function getCatList($conf) {
  $mysqli = $this->dbConnect($conf);

  // Quering...
  $query = "SELECT * FROM categories";
  $result = $mysqli->query($query);

  while($row = mysqli_fetch_array($result)) {
    echo '<option value="' . $row['id'] . '">' . $row['category'] . '</option>';
  }
}

Upvotes: 0

Views: 428

Answers (4)

Obi Ik
Obi Ik

Reputation: 159

 //query for the categories and options
 $current_value = 3;
 $total_num_of_options = mysql_num_rows($your_query_result);
 for($count = 1; $count <= $total_num_of_options){
   echo "<option value='".$count."' ";
   if($count == $current_value)
      echo "selected";
   echo ">cat".$count."</option>";
 }

Upvotes: 0

Gabor
Gabor

Reputation: 560

Appending to your while iteration a condition will solve this for you:

while($row = mysqli_fetch_array($result)) {
    $isSelected = $row['id'] == $catID;
    echo '<option '.($isSelected ? 'selected="selected"' : '').' value="' . $row['id'] . '">' . $row['category'] . '</option>';
}    

You're making a comparison if the current value is the same as that stored in $catID - and store the boolean result in a variable. In the echo you're just doing a conditional forking and appending the selected attribute if the value was true, otherwise not appending any empty string.

Upvotes: 1

oezi
oezi

Reputation: 51807

you should add the selected catID as a parameter to your getCatList-function. Then just change the creation of your HTML to include the selected-attribute:

public function getCatList($conf, $catID = 0) {
  $mysqli = $this->dbConnect($conf);

  // Quering...
  $query = "SELECT * FROM categories";
  $result = $mysqli->query($query);

  while($row = mysqli_fetch_array($result)) {
    echo '<option value="' . $row['id'] . '"' . ($row['id']==$catID?' selected':'') . '>' . $row['category'] . '</option>';
  }
}

now you can just pass the catID to your function:

$myConf = "something";
$myCatID = 3;
getCatList($myConf, $myCatID);

Upvotes: 0

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

You can do it like

while($row = mysqli_fetch_array($result)) {3

  echo '<option value="' . $row['id'] . '"';
  //if condition is met then make the option selected
  if($row['categoryID'] == 3) {
      echo " selected='selected' ";
  }
  echo '>' . $row['category'] . '</option>';

}

Upvotes: 1

Related Questions