Jump to content

Default selected value in dropdown list through php


Darkmatter5

Recommended Posts

If I have the following code:

<?php
  include 'library/dbconfig.php';
  include 'library/opendb.php';

  $query="SELECT state_id, state, state_abbrev
      FROM byrnjobdb.states
      ORDER BY state ASC";
  $result=mysql_query($query);
  echo "<select method='get'>";
  echo "<option>---Select---</option>";
  while ($row=mysql_fetch_array($result)) {
    $r1=$row['state_id'];
    $r2=$row['state']. ", " .$row['state_abbrev'];
    echo "<option value='$r1'>$r2</option>";
  }
  echo "</select>";

  include 'library/closedb.php';
?>

 

How can I use <option selected value="Texas, TX">Texas, TX</option> or something similar to have it by default select Texas.  I can't figure out how to do this as the list is dynamically generated.

 

Thanks in advance!

<?php
  include 'library/dbconfig.php';
  include 'library/opendb.php';

  $query="SELECT state_id, state, state_abbrev
      FROM byrnjobdb.states
      ORDER BY state ASC";
  $result=mysql_query($query);
  echo "<select method='get'>";
  echo "<option>---Select---</option>";
  $sel = FALSE;
  while ($row=mysql_fetch_array($result)) {
    $r1=$row['state_id'];
    $r2=$row['state']. ", " .$row['state_abbrev'];
    
    echo "<option value='$r1'" . ($sel) ? " selected" : "" . ">$r2</option>";
  $sel = TRUE;
  }
  echo "</select>";

  include 'library/closedb.php';
?>

 

oops i didn't think that through. this works.

<?php
  include 'library/dbconfig.php';
  include 'library/opendb.php';

  $query="SELECT state_id, state, state_abbrev
      FROM byrnjobdb.states
      ORDER BY state ASC";
  $result=mysql_query($query);
  echo "<select method='get'>";
  echo "<option>---Select---</option>";
  $sel = "somestate";
  while ($row=mysql_fetch_array($result)) {
    $r1=$row['state_id'];
    $r2=$row['state']. ", " .$row['state_abbrev'];
   
    echo "<option value='$r1'"; 
    echo ($row['state'] == $sel) ? " selected" : "";
    echo ">$r2</option>";
}
  echo "</select>";

  include 'library/closedb.php';
?>

 

I copy and pasted the last code you wrote and changed "somestate" to "Texas", but it still doesn't work.  Here's the code.

<?php
include 'library/dbconfig.php';
include 'library/opendb.php';

$query="SELECT state_id, state, state_abbrev
    FROM byrnjobdb.states
    ORDER BY state ASC";
$result=mysql_query($query);
echo "<select method='get'>";
echo "<option>---Select---</option>";
$sel="Texas";
while ($row=mysql_fetch_array($result)) {
  $r1=$row['state_id'];
  $r2=$row['state']. ", " .$row['state_abbrev'];
  echo "<option value='$r1'";
  echo ($row['state'] == $sel) ? " selected" : "";
  echo ">$r2</option>";
}
echo "</select>";

include 'library/closedb.php';
?>

 

Any thoughts?

// inside your loop, add the following...

while ($row=mysql_fetch_array($result)) {
  $r1=$row['state_id'];
  $r2=$row['state']. ", " .$row['state_abbrev'];
  echo "<option value='$r1'";
  echo ($row['state'] == $sel) ? " selected" : "";
  echo ">$r2</option>";
  echo "<color = 'red'>   {$row['state']} : $sel <br />"; //add this line here
}

 

post output

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.