Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.