SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Simple IF conditional not working

    Is there a reason why this won't work?

    PHP Code:
        if($Filename NULL)
        {
        echo 
    "Message here";
        }
        else

    echo 
    "<p><strong><a href='setups/".$Filename."' target='_blank'>$ModelName</a></strong> \n";  
    echo 
    "$EventDetail</p>\n"
    When there is no $Filename, I want the message to appear, but instead the area is left blank.

    Here is the context:

    PHP Code:
    <?php

    $sqlq 
    mysql_query 
        
    ("SELECT setups.ID, setups.EventDetail, setups.Filename, setups.ModelName
      FROM setups
    INNER JOIN reportlinksetup
        ON reportlinksetup.Setup = setups.Filename 
    INNER JOIN racereports
        ON racereports.ID = reportlinksetup.ReportID
        WHERE racereports.ID = '1'
    "
    );

        if (!
    $sqlq
        { echo
    "Message here"; }

    while (
    $pr mysql_fetch_array($sqlq)) 

        
    $EventDetail =    $pr['EventDetail']; 
        
    $Filename =        $pr['Filename']; 
        
    $ModelName =    $pr['ModelName'];  

        
    $EventDetail =    htmlspecialchars($EventDetail);
        
    $Filename =        htmlspecialchars($Filename);
        
    $ModelName =    htmlspecialchars($ModelName);

        if(
    $Filename NULL)
        {
        echo 
    "Message here";
        }
        else

    echo 
    "<p><strong><a href='setups/".$Filename."' target='_blank'>$ModelName</a></strong> \n";  
    echo 
    "$EventDetail</p>\n"

    }
    ?>
    The MySQL works perfectly. I also tried two = signs ($Filename == NULL) to no avail.

    Thanks!

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1 = is an assignation
    2 = is a test without check of the type
    3 = is a test with a check of the type

    http://www.php.net/manual/en/languag...assignment.php
    http://www.php.net/manual/en/languag...comparison.php
    Code:
    Example  	Name  	Result
    $a == $b 	Equal 	TRUE if $a is equal to $b.
    $a === $b 	Identical 	TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
    $a != $b 	Not equal 	TRUE if $a is not equal to $b.
    $a <> $b 	Not equal 	TRUE if $a is not equal to $b.
    $a !== $b 	Not identical 	TRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4)
    $a < $b 	Less than 	TRUE if $a is strictly less than $b.
    $a > $b 	Greater than 	TRUE if $a is strictly greater than $b.
    $a <= $b 	Less than or equal to 	TRUE if $a is less than or equal to $b.
    $a >= $b 	Greater than or equal to
    PHP Code:
    if($a=1){echo "True!";}
    else{echo 
    "False!";} 
    will always be true, because the assignation of the value 1 to the variable $a succeeded.

    PHP Code:
    $a=1;
    if(
    $a=='1'){echo "True!"}
    else{echo 
    "False!";} 
    Here, you compare the value of $a, which is the integer 1 is compared with a string.
    PHP tries to convert the string to an integer, and as both seems the same value, returns true

    PHP Code:
    $a=1;
    if(
    $a==='1'){echo "True!"}
    else{echo 
    "False!";} 
    Here, the result is false.
    Because === test that the same type of datas are in both side of the if.
    Here, we still have an integer and a string. Even if the casted values are the same, the types are different, so the result is false.

    So, in your case, the if always goes in, because the assignation of the value NULL to $Filename is always successful.
    Beside, if you don't use the { and } after an else or an if, it means that only the first line after the else or if will be used in the condition.
    In your code, the "else" do not have those delimiters, and have a blank line following it.
    Either you forgot to copy something, or there is something wrong.
    I recommend you to always use those delimiters, they will help you find an error many times.

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Thank you for your in-depth explanation. However, I thought I understood about conditionals. That's why I posted. As I mentioned, == did not work either.

    This doesn't work either:

    if(!$Filename)
    {
    echo "Message here";
    }
    else

    If the $Filename field is empty, the message should echo. The message does not echo.

  4. #4
    SitePoint Member dgleeson's Avatar
    Join Date
    Feb 2009
    Location
    Wollongong, AU
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe just use empty ??
    Code:
    if(empty($Filename))

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think there are other functions aswell. you want to try is_null? also you have only one = sign in the condition. one equal sign is assigning $filename to null, and null is not a boolean. if statement need a boolean argument.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var_dump($Filename);

    You can see the contents of your variable, as well as the "type" of data it contains(probably string)

    Read "Converting to boolean"
    http://www.php.net/boolean

    It should now be very clear why you have this problem.

  7. #7
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StevenHu View Post
    Is there a reason why this won't work?

    PHP Code:
        if($Filename NULL)
        {
        echo 
    "Message here";
        }
        else

    echo 
    "<p><strong><a href='setups/".$Filename."' target='_blank'>$ModelName</a></strong> \n";  
    echo 
    "$EventDetail</p>\n"
    When there is no $Filename, I want the message to appear, but instead the area is left blank.

    Here is the context:

    PHP Code:
    <?php

    $sqlq 
    mysql_query 
        
    ("SELECT setups.ID, setups.EventDetail, setups.Filename, setups.ModelName
      FROM setups
    INNER JOIN reportlinksetup
        ON reportlinksetup.Setup = setups.Filename 
    INNER JOIN racereports
        ON racereports.ID = reportlinksetup.ReportID
        WHERE racereports.ID = '1'
    "
    );

        if (!
    $sqlq
        { echo
    "Message here"; }

    while (
    $pr mysql_fetch_array($sqlq)) 

        
    $EventDetail =    $pr['EventDetail']; 
        
    $Filename =        $pr['Filename']; 
        
    $ModelName =    $pr['ModelName'];  

        
    $EventDetail =    htmlspecialchars($EventDetail);
        
    $Filename =        htmlspecialchars($Filename);
        
    $ModelName =    htmlspecialchars($ModelName);

        if(
    $Filename NULL)
        {
        echo 
    "Message here";
        }
        else

    echo 
    "<p><strong><a href='setups/".$Filename."' target='_blank'>$ModelName</a></strong> \n";  
    echo 
    "$EventDetail</p>\n"

    }
    ?>
    The MySQL works perfectly. I also tried two = signs ($Filename == NULL) to no avail.

    Thanks!
    You are using assignment operator = instead of ==, Please try with == and also try with if($Filename=="");

  8. #8
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    adstiger, as you can see at the bottom of the quoted piece, I mentioned that == NULL doesn't work either.

  9. #9
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by crmalibu View Post
    var_dump($Filename);

    You can see the contents of your variable, as well as the "type" of data it contains(probably string)

    Read "Converting to boolean"
    http://www.php.net/boolean

    It should now be very clear why you have this problem.
    I added the var_dump and no results are returned. Here's how I set it up:
    PHP Code:
        $EventDetail =    htmlspecialchars($EventDetail);
        
    $Filename =        htmlspecialchars($Filename);
        
    $ModelName =    htmlspecialchars($ModelName);

    var_dump($Filename);

        if(!
    $Filename)
        { echo 
    "<p>No setups for this race</p>"; }
        else
        {
    echo 
    "<p><strong><a href='setups/".$Filename."' target='_blank'>$ModelName</a></strong> \n";  
    echo 
    "$EventDetail</p>\n"
        } 
    When I change the ID to a row with a known value in the Filename field, the data was successfully displayed on the screen, and the var_dump gave the correct string value. When I change the ID to a row with a known empty Filename field, then the echo message does not appear and a var_dump message does not appear. If the value is NULL, shouldn't the var_dump return a NULL value? Not sure.

    The results are the same whether with !$Filename or $Filename == NULL. I've used similar coding on several other pages and they've always worked. That's what makes this so puzzling.

    I opened up View Source when the Filename was empty and found the area blank - just like the echo. No SQL error message.

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, var dump should always output something, even if it's just NULL.

    Change that part to
    PHP Code:
    echo 'var dumping';
    var_dump($Filename); 
    If you don't at least get the echo, then you're probably editing/uploading the wrong file. It's also possible there's no rows returned from the database, which means none of the code in your while loop executes. Maybe theres another part of your script which produces similar output to the following and you're confused.
    PHP Code:
    echo "<p><strong><a href='setups/".$Filename."' target='_blank'>$ModelName</a></strong> \n";   
    echo 
    "$EventDetail</p>\n"
    echo out some messages in various parts of you code. You can see which conditional parts of your code are running by using this technique.

  11. #11
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Changing the var_dump code as you advised produced the same results as before. Not sure where to echo other parts of the script when all of them are operating correctly.

    Elsewhere on the page I have this operating correctly:

    PHP Code:
        if (!$PhotoCredit)
        { 
    $credit NULL; }
        else
        { 
    $credit "Thanks to $PhotoCredit for these photos."; }
    $credit 
    Perhaps I should recast the other statement in the same form. However, when I tried, I got bogged down trying to figure out where to put double and single quotes a produced a mess!

    Thanks!

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code inside of a loop will never execute if the loop condition is not met.
    Your while loop will run once for every row the database produces. Guess what happens when the database produces no rows?

    see mysql_num_rows() to determine how many rows a result set contains.

  13. #13
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StevenHu View Post
    adstiger, as you can see at the bottom of the quoted piece, I mentioned that == NULL doesn't work either.
    instead of NULL try using blank quotes ""

  14. #14
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ( $variable == NULL ) {
        echo 
    'The variable is empty';
    } else {
        echo 
    "The variable is $variable";

    If $variable is '' (no string between the quotes) or 0 it will tell you that the variable is empty. To reverse:

    PHP Code:
    if ( !$variable == NULL ) {
        echo 
    'The variable is empty';
    } else {
        echo 
    "The variable is $variable";

    Just swap $variable for $Filename and change the messages to suit.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  15. #15
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by crmalibu View Post
    The code inside of a loop will never execute if the loop condition is not met.
    Your while loop will run once for every row the database produces. Guess what happens when the database produces no rows?

    see mysql_num_rows() to determine how many rows a result set contains.
    Therefore after using mysql_num_rows() I could say "if ($row ==0) etc..." That may be a better way to do it.

  16. #16
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by crmalibu View Post
    The code inside of a loop will never execute if the loop condition is not met.
    Your while loop will run once for every row the database produces. Guess what happens when the database produces no rows?

    see mysql_num_rows() to determine how many rows a result set contains.
    If the database produces no rows, I expect this code to execute:
    if(!$Filename)
    { echo "<p>No setups for this race</p>"; }

    !$Filename tells me that there were no results (rows) returned. Am I thinking of this all wrong?

    Thanks!

  17. #17
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you may try if($Filename!=0)

  18. #18
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Again, the code inside of your loop will never get executed unless there is at least one row from the database. You need to test for this condition outside of the loop.

    Adstiger doesn't understand the problem here, don't let his suggestions confuse you.

  19. #19
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,388
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I think I understand what you mean. Since no rows are being returned, then the script cannot check if the following is true or not. If it can't evaluate, then it can't return the message.

    if(!$Filename)
    { echo "<p>message here</p>"; }

    Thanks!


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
  •