• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Quick PHP problem regarding drop down box with default "selected"

ShoeLace

Gawd
Joined
Apr 5, 2006
Messages
650
I'm having trouble trying to select a specific drop down box option based on what's been saved to a database.

The code looks like this:

Code:
   while ($row = mysql_fetch_array($result_clientlist)) {
      $firstname = $row['firstname'];
      $lastname = $row['lastname'];
      
      if (($firstname + " " + $lastname) == $client)
      {
         echo "<OPTION VALUE=\"$firstname $lastname\" SELECTED=\"selected\">$firstname $lastname</OPTION>\n";
      }
      else
      {
         echo "<OPTION VALUE=\"$firstname $lastname\">$firstname $lastname</OPTION>\n";
      }
   }

The goal is simple. I have 2 tables inside of a database. "clients" and "jobs".

When adding/editing a job you can choose which client it is associated with. The drop down box is being populated with all client names.

What I'd like to do is have the specific client selected by default when someone edits an existing job.

ie. If they created a job and "Joe Blow" is the client, then when the 'edit job' page is loaded it will select "Joe Blow" by default.

I'm pretty much a PHP newbie but I just can't get this to work. When I do the $firstname + $lastname compare to $client it keeps returning true and the SELECTED option is being written for each client.

When I do an echo on $firstname + $lastname and $client it looks right.

An example of an echo result is this:

Joe Blow ... Jim Smith
Jim Smith ... Jim Smith
John Doe ... Jim Smith

To me this says all of the variables are correct. It should only output the SELECTED option for Jim Smith, yet the code is selecting them all. Why?
 
How about this?

Code:
   while ($row = mysql_fetch_array($result_clientlist)) {
      $firstname = $row['firstname'];
      $lastname = $row['lastname'];
      $wholename = $firstname . " " . $lastname;
      
      if ($wholename == $client)
      {
         echo "<OPTION VALUE=\"$firstname $lastname\" SELECTED=\"selected\">$firstname $lastname</OPTION>\n";
      }
      else
      {
         echo "<OPTION VALUE=\"$firstname $lastname\">$firstname $lastname</OPTION>\n";
      }
   }
 
That worked perfectly, thank you. So strange that both echo the same result.

I guess the PHP standard is to combine strings using periods instead of plus signs?
 
PHP doesnt use + for string concatenations anywhere, it is always the . syntax.

The reason that the conditional was always evaluating to true in your original code is because ($firstname + " " + $lastname) evaluates to integer 0. The interpreter makes a best guess and typecasts $firstname, $lastname and " " to integers and does addition. When you compare integer 0 to $client string, the interpreter makes another best guess and typecasts $client to an integer, which will be 0 again. (0 == 0) is true.

Since PHP variables are loosly typed, be very careful with this. Use === if you know you are going to be comparing two items with the same type.
 
Ah, that make sense.

I'm going to assume when you wrote:
Use === if you know you are going to be comparing two items with the same type.

You meant "==" yeah?
 
Oh. I went to w3's PHP site and they didn't list that operator.

php.net seems solid. This should answer most questions.
 
Back
Top