...

View Full Version : Resolved conditional statement help.



ubh
12-10-2008, 10:03 PM
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
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!!

PappaJohn
12-10-2008, 10:19 PM
Check out the Return Values (http://dev.mysql.com/doc/refman/5.1/en/mysql-query.html) 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().

ubh
12-10-2008, 10:49 PM
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):


if(mysql_num_rows($result) == 0)
{}
else
{}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum