1

I have the following code, which creates a dropdown list based on the contents of a mysql database. I also have a variable $ir1text which is the users current selection from that database. How can make the drop down list pre-select the value the user already has. The idea being I want to show them what they have and have the dropdown list right there for them to select a new value.

<?php
include("config.php");
$sql1 = "SELECT * FROM iRadios";
$result1 = mysql_query($sql1);

echo "<select name='Station1'>";
while ($row = mysql_fetch_array($result1)) {
    echo "<option value='" . $row['StationName'] ."'>" . $row['StationName'] ."</option>";
}
echo "</select>";
?>
RhinoCarl
  • 33
  • 4
  • Arent that `mysql_*` functions deprecated? – HddnTHA Mar 20 '15 at 16:54
  • 1
    @HddnTHA yes - and one day I might even be able to close the tab I **always** have open on Stack Overflow because I need to copy/paste the URL so often : http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php ... I wonder if I could just set it up as a keybinding in Opera... – CD001 Mar 20 '15 at 16:55
  • Thanks guys, yes it's deprecated and yes its old code that in the future won't work. But right now that is what I have - in the system doing it's thing so this is what I need to update. In the not too distant someone will undoubtedly waive a piece of paper at me asking for it to be moved to mysqli or dbo - but right now, no, they just want the current selection to show up as selected with all the other options in the list that could be changed to. – RhinoCarl Mar 20 '15 at 17:37
  • Switching from mysql to mysqli is not such a big deal. – Strawberry Mar 20 '15 at 18:25
  • hopefully this isn't new code.. – Ryan Mar 20 '15 at 21:31

3 Answers3

1

Here is the solution

<?php
include ("config.php");

$sql1 = "SELECT * FROM iRadios";
$result1 = mysql_query($sql1);
echo "<select name='Station1'>";
while ($row = mysql_fetch_array($result1)) {
    if ($row['StationName'] == trim($ir1text)) {
        echo "<option value='" . $row['StationName'] . "' selected>" . $row['StationName'] . "</option>";
    }
    else {
        echo "<option value='" . $row['StationName'] . "'>" . $row['StationName'] . "</option>";
    }
}
echo "</select>";
?>
Istiak Tridip
  • 199
  • 1
  • 14
  • If my answer worked for you don't forget to mark as correct. Because that will help other to solve there problem. Thank you. – Istiak Tridip Mar 24 '15 at 06:00
0

Well you can put that user selected value in the first option. if you dont hyave that value in the same table then you can make another query and store that value and put it in the first option of your tag.

-1

If $row[]== $ir1tex show this option selected

while ($row = mysql_fetch_array($result1)) {
    if($row['StationName']==  $ir1text){
        echo "<option value='" . $row['StationName'] .selected"'>" . $row['StationName'] ."</option>";
    }else{
    echo "<option value='" . $row['StationName'] ."'>" . $row['StationName'] ."</option>";
    }

}
david strachan
  • 7,174
  • 2
  • 23
  • 33