SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    show price if not null or 0.00

    hi all

    the price field is set as float

    In my database there are some prices fields which have either "0.00" or "NULL" as value

    i wanted to display price if the value is not "0.00" or not "NULL"

    PHP Code:
    if( ($row['price'] != "0.00") || ($row['price'] != "NULL") ){
    // DISPLAY PRICE

    But the price is being shown even if its "0.00".

    Is my code correct or not ?

    vineet

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    AND (&&), not OR (||).

    if the price is 0.00:
    if(false OR true) = true.
    If the price is null
    if(true OR false) = true.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thanks starlion

    vineet

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    To be more precise, the way to logically construct the description of what you want is:

    If the price is Neither "0.00" Nor Null, show price.
    "Neither A Nor B" translates to logic as "Not A AND Not B"
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by StarLion View Post
    "Neither A Nor B" translates to logic as "Not A AND Not B"
    "Neither A Nor B" translates to logic as "Not ( A OR B )"

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

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    An SQL SELECT statement returns these rows, would it be better if it did not return those rows whose `price` was NULL or "0.00"?

  7. #7
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    vinpkl, also remember that the database value NULL when retrieved by PHP becomes the PHP NULL value and not the "NULL" string. Therefore your condition as you presented will not work:
    Code:
    $row['price'] != "NULL"
    Instead, you should write it like this:
    Code:
    $row['price'] !== NULL
    This way you will strictly exlude NULL values.

    But in PHP you can achieve your goal in a much simpler syntax because of default type casting of values:
    PHP Code:
    if($row['price'] != 0) {
      
    // DISPLAY PRICE

    This will display the price if it is not "0.00" or if it is not NULL, because in PHP using weak comparison (!= and ==) NULL equals 0, and string "0.00" equals 0. Only when you use strong comparison (!== or ===) then NULL does not equal 0 and string "0.00" does not equal 0.


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
  •