show literal when column contains certain value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nathanwb
    New Member
    • Mar 2008
    • 39

    show literal when column contains certain value

    I have an index.php page that does a query from a database of users, in this database there is a field called isadmin.. I am wondering if it would be possible to do some type of IF statement based on that field having a certain value? Some how fetching the status of that field, like if field=1 then echo "this" and if it equals 0 then display nothing..?

    Thanks
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Originally posted by nathanwb
    I have an index.php page that does a query from a database of users, in this database there is a field called isadmin.. I am wondering if it would be possible to do some type of IF statement based on that field having a certain value? Some how fetching the status of that field, like if field=1 then echo "this" and if it equals 0 then display nothing..?

    Thanks
    That can be done by sql..
    Code:
    SELECT * FROM `[I]your_table[/I]` WHERE `[I]field_1[/I]`='[I]value_1[/I]' AND `[B]isadmin[/B]`='[I]required_value[/I]'
    If you want to try that with an if in PHP, do it like
    [PHP]
    while ($row = myqsl_fetch_arr ay($sql))
    {
    if ($row['isadmin']=="required_value")
    {
    echo "string to be printed";
    }
    }[/PHP]

    But the SQL solution is recommended.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Sorry for bursting in, but I think what OP means is that he/she wants to replace a selected column value using an IF function. As the following example: when field_1 equals 1 it will be shown as 'this'[code=sql]SELECT *, IF(field_1=1, 'this', field_1) AS field1_new FROM your_table;[/code]Ronald

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        This is a MySQL question so I will move it to the MySQL forum.

        moderator

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by ronverdonk
          Sorry for bursting in, but I think what OP means is that he/she wants to replace a selected column value using an IF function. As the following example: when field_1 equals 1 it will be shown as 'this'[code=sql]SELECT *, IF(field_1=1, 'this', field_1) AS field1_new FROM your_table;[/code]Ronald
          ohh... if that's the case, then I guess I misinterpreted the question.

          Harpreet

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by hsriat
            ohh... if that's the case, then I guess I misinterpreted the question.

            Harpreet
            Happens to me frequently. Sometimes it is hard to understand what the OP means, because the real question is more or less hidden in the stated question.

            Ronald

            Comment

            Working...