Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    conditional statement help.

    Hi,
    I am adding a new section to each one of my products descriptive page that will suggest other products one might be intrested based off the product they are currently looking at.

    I have it working great!! but some products dont have suggested other products... SOOO in my code I would like to setup a conditional statment that basicly says hey if my $result is == to "" echo out nothing, else echo out suggested products.

    Problem I am having is where do I start my condition statement. Here is my code

    PHP Code:
                <?php 
                
    include("db_cnt.php");// CONNECT TO DATABASE
                
    $result mysql_query("SELECT * FROM mama_suggest WHERE product_name='$product_name' AND product_category='$product_category'"//TAKE PRE-EXSISTING VARIABLES AND SEARCH DB
                
    or die(mysql_error()); 
                
                if(
    $result == "")//START OF CONDITION STATEMENT
                
    {}
                else
                {
                    
    //BEGIN TO ECHO OUT PRODUCT SUGGESTIONS
                    
    echo '<div style="float:left; margin-top:25px; margin-left:15px; padding-top:10px; width:400px; font-size:12px">';

                   
    //ECHO OUT PRODUCT SUGGESTIONS TITLE        
                    
    echo "<strong>Mama also suggests</strong><br /><br />";
                    
                    
    //GRAB SUGGESTED PRODUCTS NAME/CATEGORY 
                    
    while($row mysql_fetch_array$result )) {
                    
    $suggest_name $row['suggest_name'];
                    
    $suggest_category $row['suggest_category'];
                    
                            
    //USE SUGGESTED PRODUCTS NAME/CATEGORY TO SEACH SECOND DATA TABLE
                            
    $result2 mysql_query("SELECT * FROM site_products WHERE product_name='$suggest_name' AND product_category='$suggest_category'"
                            or die(
    mysql_error());
                            while(
    $row mysql_fetch_array$result2 )) {
                            
    //GRAB PRODUCTS THUMB NAIL IMAGES
                            
    $thumb_nail $row['thumb_nail'];
                            
    $url $row['url'];

                            
    //BUILD PRODUCT SUGGESTION STRUCTURE            
                            
    echo '<div style="float:left; text-align:center; width:125px;">';
                            echo 
    '<a href="'.$url.'" title="'.$suggest_name.'" />';
                            echo 
    '<img src="'.$thumb_nail.'" alt="'.$suggest_name.'" style="border:none;"/>';
                            echo 
    '</a>';
                            }
                    echo 
    "<br />";
                    echo 
    $suggest_name;
                    echo 
    '</div>';
                    }
                    echo 
    '</div>';
                }
                
    ?>
    Ok the problem with this condition statement is that it is being ignored and if no product_name / product_category is found my product suggestions structure and title is still being echoed out... How would I go about setting this up to use my condition statement correctly??

    Thanks!!

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Check out the Return Values for mysql_query(). mysql_query() will not return a null value.

    Keep in mind that mysql_query() will return a 'true' value even if no records were found, as long as the query does not fail. You're going to want to look into mysql_num_rows().

  • Users who have thanked PappaJohn for this post:

    ubh (12-10-2008)

  • #3
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Excllent PappaJohn Thanks!!

    I always like to do a recap here in case others out on the net are having a similar problems or ha ha I forget and have to reference back to this.

    My condition statement now looks like this (bare bones):

    PHP Code:
    if(mysql_num_rows($result) == 0)
    {}
    else
    {} 


  •  

    Posting Permissions

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