www.webdeveloper.com
Results 1 to 4 of 4

Thread: Is it possible to do an if statement while retrieving data from database?

  1. #1
    Join Date
    Jan 2004
    Posts
    312

    Is it possible to do an if statement while retrieving data from database?

    Is it possible to do an if statement inside the echo while retrieving data from the database. Here is what I need:

    PHP Code:
    if('.$view_records['ID'].' == $Quick_Employee_Lookup) { selected } else { } 
    I need this inside the "option" so if the Form field is = to the id displayed from the database, write selected so that value is selected after they hit submit.

    PHP Code:
    while($view_records odbc_fetch_array($employee_lookup_uw_admin_result)) { echo ('<option value="'.$view_records['ID'].'">'.$view_records['Display_Name'].'</option>
    '
    );

    So kind like this, of course this doesn't work:

    PHP Code:
    while($view_records odbc_fetch_array($employee_lookup_uw_admin_result)) { echo ('<option value="'.$view_records['ID'].'" if('.$view_records['ID'].' == $Quick_Employee_Lookup) { selected } else { }>'.$view_records['Display_Name'].'</option>
    '
    );


  2. #2
    Join Date
    Aug 2003
    Location
    In my house
    Posts
    4,004
    You could do something like this:
    PHP Code:
    while($view_records =  odbc_fetch_array($employee_lookup_uw_admin_result)) {
        
    $selected = ($view_records['ID'] == $Quick_Employee_Lookup) ? ' selected="selected"' '';
        echo (
    '<option value="'.$view_records['ID'].'"'.$selected.'>'.$view_records['Display_Name'].'</option>');

    I use the ternary operator to run a sort of condensed if...else statement. If $view_records['ID'] is equal to $Quick_Employee_Lookup then the value of $selected is ' selected="selected"', otherwise it's just blank.

    So if $Quick_Employee_Lookup did equal $selected, then the output would look like this:
    Code:
    <option value="..." selected="selected">...</otion>
    Otherwise, it would look like this:
    Code:
    <option value="...">...</option>
    You could make it a bit easier to read and use a normal if...else statement, but the ternary operator seems to fit nicely in this situation.

    You could put the ternary operator inside the echo (which is pretty much putting an if...else statement inside the echo), but that would be a bit more complex:
    PHP Code:
    echo ('<option value="'.$view_records['ID'].'"'. ( ($view_records['ID'] == $Quick_Employee_Lookup) ? ' selected="selected"' '') .'>'.$view_records['Display_Name'].'</option>'); 
    That's quite a long line (179 characters), and pretty hard to make sense of unless you really look at it.
    Last edited by Paul Jr; 11-19-2004 at 11:46 PM.
    Thousand different paths
    So many sterile ends
    I chose the Devil's path

    Never shall the sun kiss my face
    And caress me with it's burning light
    For I dwell in the shadows
    And sleep side by side with death

  3. #3
    Join Date
    Jan 2004
    Posts
    312

    Talking Resolved

    That did the trick! Thanks so much, absolutely perfect!

  4. #4
    Join Date
    Aug 2003
    Location
    In my house
    Posts
    4,004
    You're welcome.
    Thousand different paths
    So many sterile ends
    I chose the Devil's path

    Never shall the sun kiss my face
    And caress me with it's burning light
    For I dwell in the shadows
    And sleep side by side with death

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles