SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Conditional within a string?

  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Conditional within a string?

    Hi,

    I'm looping through some database rows to display the content in a table. Basically, it's for some professionals who may or may not have credentials after their name. So some will be John Smith and some will be John Smith, MA. If they list credentials, I need to insert a comma before the credentials. I tried putting an if clause in my td but I'm getting an error. Is it my syntax or my logic that's wrong? Is there a better way to do this? Here's the code:

    PHP Code:
                    <td>" . $row['f_name'] . " " . $row['mi'] . " " . $row['l_name'] . 
                        if (
    $row['credentials']) {
                            " 
    " . $row['credentials'];
                        }
                    
                    "
    </td

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,485
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, you can do this, post a bit more code, as its hard to see your coding method you use from the bit you've given.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If those without credentials have a NULL (as opposed to it just being empty) in the database then you could do it via the query.
    Code:
    SELECT f_name, l_name, CONCAT(', ', COALESCE(credentials)) AS credentials FROM usertable

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,485
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Split your echo statement into two halves, and put the if in the middle, like this..
    PHP Code:
    ..............<td>" . $row['f_name'] . " " . $row['mi'] . " " . $row['l_name'] ; 
    if (
    $row['credentials']) { 
        echo " 
    " . $row['credentials']; 

    echo "
    </td> ................. 
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,485
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    .............. then you could do it via the query.
    But only if using SQL ver > 4.1

    Actually, ignore that, I'm thinking of GROUP_CONCAT, sorry
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  6. #6
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    COALESCE() was added in MySQL 3.23.3

  7. #7
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,485
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markl999
    COALESCE() was added in MySQL 3.23.3
    Yeah, sorry I updated my post just as you corrected me
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  8. #8
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, sorry I updated my post just as you corrected me
    s'k, i had to go check myself once you'd mentioned it as i had no idea when COALESCE() was introduced

  9. #9
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent. I got it to work. And, for the record, I'd never even heard of COALESCE() so I learned some extra goodies.

Bookmarks

Posting Permissions

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