SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A quick IF statement query

    I have a field that will always be 0 or 1. How do I create a query where the IF statement is IF any fields are showing as 1, ELSE if there are no fields with a value of 1. So, if there was at least 1 field with the value as 1 it would show all fields with that value, and if they all had value of 0 it would show something else entirely. Does that make sense?

    Thanks
    Russ

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...and if they all had value of 0 it would show something else entirely
    What does this mean? Are you looking for this in SQL query itself or you want to control it with PHP if conditions?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe, I should make myself more clear. All I need is a SQL IF/ELSE statement, such as IF (flagged === "1") {ECHO "Something"; }ELSE {ECHO "Something else;}

    But the thing I don't understand is how I would get the SQL query, instead of asking IF (flagged === "1") to ask IF flag equals "1" if any entry in the database, to echo "Something", otherwise run the IF statement. Hopefully this is more clearer!

    Thanks

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Theres a lot of ways to do this. You can do it in sql or in php. Here's one way
    PHP Code:
    $row mysql_fetch_assoc($res);
    if (
    array_sum($row) > 0) {


    That won't work if one of the fields might have a negative value.

    You could also use use the logical or operator
    PHP Code:
    if ($row['foo'] == || $row['bar'] == || $row['baz'] == 1) {



  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,246
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by slaterino View Post
    So, if there was at least 1 field with the value as 1 it would show all fields with that value
    this part doesn't make sense

    if your table had 42 rows with 0s and 17 rows with 1s, you want to show 17 1s? but if there were 59 rows with 0s and no rows with 1s you don't want to show any of the 0s but something else entirely?

    i r confusled

    perhaps you could mock up a little sample table and show what you want the query to do
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The annoying thing is I'm not trying to describe anything complex, i think it's being assumed I am, and unfortunately I don't know the sql lingo! Which makes it hard to explain things.

    But anyway, I will try again. I'm referring to one field in my table. The value of this will either be 1 or 0. I only want to show entries that have a value of 1 in this field. If there are no entries with a value of 1, then I need to show a different message (this is the ELSE part of the statement). Therefore I need my query to look through the entries, if there is at least 1 entry with a value of 1 then it will run the first part of the query (following the IF statement), and if not would run the ELSE statement.

    I'm having a look at the array_sum function at the moment and it looks like that could work, but please let me know if there are any better ways of doing this.

    Thanks!

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,246
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    what you want is ~not~ some kind of SQL if statement, you want a php if statement

    the SQL should simply use WHERE daField = 1 and this will return all rows that have a 1

    if there are no rows returned by the query (i don't do php but i think there is a mysql_num_rows function), that's when you would display the different message

    the "annoying" part, if there was any, and i'm not saying there was, is that you said you wanted to do the if with SQL, where this is more properly done with php

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Implemented rudy's algorithm here into PHP:
    PHP Code:
    $result mysql_query("SELECT * FROM tablename WHERE fieldname=1") or die(mysql_error());
    if(
    mysql_num_rows($result) >= 1){ // if the rows are found having the fieldname = 1
        // do some thing here
    }
    else{
        
    // show up a message here which is needed in ELSE part

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •