Populate default value of dropdown lists from database.


Please excuse noobiness on my part (only started writing php today)…

I am trying to write a football players database app for fantasy league football as a way of picking up php as quickly as possible. Am using php and mySQL.

Have got stuck on this bit though :(

I want to put a drop down list next to each player. In the list I want the default value to be set for each player, if it exists in the database, to the entry in the database. So if a player has previously been set as a defender then the listbox should have defender set as the default for the listbox.

Here is the bit that I am trying to get to do that (have stripped out the other fields other than position in order to keep this post succint):

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo("") { if ($row['Position'] = 'GK') { echo("GK"); } else { echo("GK "); } if ($row['Position'] = 'DEF') { echo("DEF"); } else { echo("DEF "); } if ($row['Position'] = 'MID') { echo("MID"); } else { echo("MID"); } if ($row['Position'] = 'FWD') { echo("FWD"); } else { echo("FWD"); } ");


I am pretty sure I am screwing up with syntax somewhere or perhaps php just doesn’t like where I have put that IF.

There are probably much easier ways of doing what I am trying to do (function?) but I am slightly lost with the syntax of this and why it is not working.

Any help/pointers/slap the noob appreciated…



I tried this a different way, a bit more html really and that didn’t work either. Despite a scan of the database showing up the right records in an echo test (second line), the “selected” option was applied to ALL of the option values in the page generated with this:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo($row["Position"]);
echo("<tr class='plrrow1' align='left'>");
echo("<td>" . $row["Surname"] . "</td>"); 
echo("<td>" . $row["TeamName"] . "</td>");


echo("<td>" . $row["Selected"] . "</td>");


This means the page defaults to “FWD” cos it is the last SELECTED selected…

Where am I going wrong? :(

(my second day in php world)



Gah! Seems I have to use == rather than = …





>GK [/php]

That is called a ternary operator. (http://www.php.net/language.operators) Look at the user comments.

(condition) ? (doIfTrue) : (doIfFalse);

The third group is the ternary operator: ?:. It should be used to select between two expressions depending on a third one, rather than to select two sentences or paths of execution. Surrounding ternary expressions with parentheses is a very good idea.