Jump to content

[SOLVED] else statement not working


simcoweb

Recommended Posts

I'm running this query whereas IF there's no results then it should display an alternative message. However, when there's no results it just shows a blank page entirely...which means it's breaking somewhere but I can't see why it's not functioning properly. If I select in the search the two elements that I know produce results then it displays fine. It's only when there's no results that it breaks.

 

// query for extracting lenders from amember database
$category = strip_tags(trim($_POST['category']));
$loantype = strip_tags(trim($_POST['loan_type']));
//$loanamount = strip_tags(trim($_POST['loan_size']));

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($results) or die(mysql_error());

if(empty($results )){
include 'header.php';
echo "<h3>No Results Found</h3><br>
<p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>\n";
echo "<br><input type=button value=\"Return to Search\" onClick=\"history.go(-1)\">";
include 'footer.php';
} else {

Link to comment
Share on other sites

<?php

if(mysql_num_rows($numrows)< 0){
include 'header.php';
echo "<h3>No Results Found</h3><br>
<p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>\n";
echo "<br><input type=button value=\"Return to Search\" onClick=\"history.go(-1)\">";
include 'footer.php';
} else {

Link to comment
Share on other sites

$results will only end up being a resource identifier. This does not eval as empty since it has a "value".

 

You'll want to make use of mysql_num_rows($result) ( returns the number of rows found by that resource ID ).

 

if((mysql_num_rows($results) === 0 ))
{
   // empty.
}
else
{
    // not empty!
}

 

 

Link to comment
Share on other sites

Hmmm... still producing a blank page. I've tried SOOO many combos and methods. Right now here's what I have:

 

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($results) or die(mysql_error());

if (mysql_num_rows($results) === 0){
include 'header.php';
echo "<h3>No Results Found</h3><br>
<p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>\n";
echo "<br><input type=button value=\"Return to Search\" onClick=\"history.go(-1)\">";
include 'footer.php';
} else {

 

If you go to: http://www.lenderfish.com/search-test.php

 

select 'Stated' in the first drop down and anything in the next and you'll see what happens. Might help. Basically there are no results from the database for anything in the query related to Stated so it should produce the IF condition if that's the case. But it's not.

Link to comment
Share on other sites

try that

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($results) or die(mysql_error());

if (!$results){
include 'header.php';
echo "<h3>No Results Found</h3><br>
<p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>\n";
echo "<br><input type=button value=\"Return to Search\" onClick=\"history.go(-1)\">";
include 'footer.php';
} else {

Link to comment
Share on other sites

Here's the whole setup:

 

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
ini_set("display_startup_errors", 1);

include 'db_config.php';
/**
* @author rockmetal
* @copyright 2007
*/

// query for extracting lenders from amember database
$category = strip_tags(trim($_POST['category']));
$loantype = strip_tags(trim($_POST['loan_type']));
//$loanamount = strip_tags(trim($_POST['loan_size']));

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($results) or die(mysql_error());

if (mysql_num_rows($results) === 0){
include 'header.php';
echo "<h3>No Results Found</h3><br>
<p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>\n";
echo "<br><input type=button value=\"Return to Search\" onClick=\"history.go(-1)\">";
include 'footer.php';
} else {
include 'header.php';
echo "<h3>The following lenders meet your search criteria.</h3><br>\n";
// create display of results in abbreviated format
while ($row = mysql_fetch_array($results)) {
	echo "

	<div class='wrapper_out' onmouseover=\"this.className='wrapper'\"; onmouseout=\"this.className='wrapper_out'\";>
	<table width='500' border='0' align='center'>
	<tr ><td colspan='2' class='lendertitle'>".$row['company_name']."</td></tr>
	<tr><td colspan='2' class='lenderdesc'>Company phone: ".$row['company_phone']."</td></tr>
	<tr><td width='300' class='lenderdesc'>Company contact: ".$row['company_contact']."</td>
	<td width='200'><a class='lenderlink' href='loanrequest.php?lenderid=".$row['member_id']."'>Submit Loan Request</a></td></tr>
	<tr><td colspan='2'>";

	echo pullRating($row['member_id'],true,false,true);
	echo "</td></tr>
	</table></div><br />\n";
	echo "<hr style='border-top: 1px dotted #666666' color='#666666' width='400' size='1'>";
}
include 'footer.php';
}
?>

 

redarrow, I did try that if(!$results) { method and got the same thing. That's what's puzzling.

 

Ken2k7, the tables are set up fine. If you search for something you know is there then the results display perfectly. It's only when one of the conditions isn't met. In this case, as in the example I gave, if you choose Stated then whatever in the second drop down then the query should be empty and the IF condition should be met and HTML displayed. Not doing that.

 

 

Link to comment
Share on other sites

I didn't ask if the tables were set up correctly or not. I was asking to see how the data is stored. Do you have a separate column that have data for each value of the dropdown? Something like:

 

table:

 

dropdown1      |        dropdown 2

-----------------------------------

convensional    |          purchase

.                    |                .

.                    |                .

.                    |                .

 

or something else?

 

I'm only questioning about the category_1, category_2 etc in your query.

Link to comment
Share on other sites

give this ago only a stab in the dark ok

 

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
ini_set("display_startup_errors", 1);

include 'db_config.php';
/**
* @author rockmetal
* @copyright 2007
*/

// query for extracting lenders from amember database
$category = strip_tags(trim($_POST['category']));
$loantype = strip_tags(trim($_POST['loan_type']));
//$loanamount = strip_tags(trim($_POST['loan_size']));

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($results) > 0){

while($numrows = mysql_fetch_assoc($results)){

include 'header.php';

echo "<h3>The following lenders meet your search criteria.</h3><br>\n";
// create display of results in abbreviated format

	echo "

	<div class='wrapper_out' onmouseover=\"this.className='wrapper'\"; onmouseout=\"this.className='wrapper_out'\";>
	<table width='500' border='0' align='center'>
	<tr ><td colspan='2' class='lendertitle'>".$row['company_name']."</td></tr>
	<tr><td colspan='2' class='lenderdesc'>Company phone: ".$row['company_phone']."</td></tr>
	<tr><td width='300' class='lenderdesc'>Company contact: ".$row['company_contact']."</td>
	<td width='200'><a class='lenderlink' href='loanrequest.php?lenderid=".$row['member_id']."'>Submit Loan Request</a></td></tr>
	<tr><td colspan='2'>";

	echo pullRating($row['member_id'],true,false,true);
	echo "</td></tr>
	</table></div><br />\n";
	echo "<hr style='border-top: 1px dotted #666666' color='#666666' width='400' size='1'>";

	include 'footer.php';
}else{

include 'header.php';
echo "<h3>No Results Found</h3><br>
<p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>\n";
echo "<br><input type=button value=\"Return to Search\" onClick=\"history.go(-1)\">";
include 'footer.php';
}
}
?>

Link to comment
Share on other sites

Ken2K7, basically they're set up like your example. Two separate tables containing the items as shown in those dropdowns respectively. The two drop downs are populating perfectly with the selections.

 

redarrow, tried it and still produces a blank page when using the 'fail' search. Also, produces a slew of errors such as:

 

Notice: Undefined variable: row in /home2/wwwfish/public_html/search.php on line 35

 

Notice: Undefined variable: row in /home2/wwwfish/public_html/search.php on line 36

 

Notice: Undefined variable: row in /home2/wwwfish/public_html/search.php on line 37

 

Notice: Undefined variable: row in /home2/wwwfish/public_html/search.php on line 38

Company phone:

Company contact: Submit Loan Request

 

Notice: Undefined variable: row in /home2/wwwfish/public_html/search.php on line 41

 

Link to comment
Share on other sites

Well, perhaps I should explain. The dropdowns are populated from separate tables that are totally separate from the data for the results. The $category and $loantype variables are populated by the form that holds the two dropdowns. The data containing the content for the results is in a table called 'amember_members' as it's looking through the member's data to see what matches.

 

Now, the data is much more complex. There's not a single 'category' field. There's 5. (category_1, _2, etc.) and there's 5 different loan_type fields (_1, _2,_3,_4, _5). So, the idea is to match their two selections from the dropdown with the various fields in the data. When it's done it should display the results. If there aren't any that match the criteria then it should display the 'else' content. Unfortunately it's not.

 

 

Link to comment
Share on other sites

try this please cheers....

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
ini_set("display_startup_errors", 1);

include ('db_config.php');
/**
* @author rockmetal
* @copyright 2007
*/

// query for extracting lenders from amember database
$category = strip_tags(trim($_POST['category']));
$loantype = strip_tags(trim($_POST['loan_type']));
//$loanamount = strip_tags(trim($_POST['loan_size']));

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql) or die(mysql_error());


while($numrows=mysql_fetch_assoc($results)){


if(mysql_num_rows($results)==1){


include ('header.php');

echo "<h3>The following lenders meet your search criteria.</h3><br>";
// create display of results in abbreviated format

	echo " <div class='wrapper_out' onmouseover='this.className='wrapper''; onmouseout='this.className='wrapper_out'';>
	<table width='500' border='0' align='center'>
	<tr ><td colspan='2' class='lendertitle'>".$row['company_name']."</td></tr>
	<tr><td colspan='2' class='lenderdesc'>Company phone: ".$row['company_phone']."</td></tr>
	<tr><td width='300' class='lenderdesc'>Company contact: ".$row['company_contact']."</td>
	<td width='200'><a class='lenderlink' href='loanrequest.php?lenderid=".$row['member_id'].">Submit Loan Request</a></td></tr>
	<tr><td colspan='2'>";

    echo pullRating($row['member_id'],true,false,true);
	echo "</td></tr>
	</table></div><br>
	<hr style='border-top: 1px dotted #666666' color='#666666' width='400' size='1'>";

	include('footer.php');

}else{
include ('header.php');
echo "<h3>No Results Found</h3><br>
<p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>
<br><input type=button value='Return to Search' onClick='history.go(-1)'>";
include ('footer.php');
}
}
?>

Link to comment
Share on other sites

this better work lol going mad here

 

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
ini_set("display_startup_errors", 1);

include ('db_config.php');
/**
* @author rockmetal
* @copyright 2007
*/

// query for extracting lenders from amember database
$category = strip_tags(trim($_POST['category']));
$loantype = strip_tags(trim($_POST['loan_type']));
//$loanamount = strip_tags(trim($_POST['loan_size']));

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($results)==1){


$row=mysql_fetch_assoc($results);


include ('header.php');

echo "<h3>The following lenders meet your search criteria.</h3><br>";
// create display of results in abbreviated format

	echo " <div class='wrapper_out' onmouseover='this.className='wrapper''; onmouseout='this.className='wrapper_out'';>
	<table width='500' border='0' align='center'>
	<tr ><td colspan='2' class='lendertitle'>".$row['company_name']."</td></tr>
	<tr><td colspan='2' class='lenderdesc'>Company phone: ".$row['company_phone']."</td></tr>
	<tr><td width='300' class='lenderdesc'>Company contact: ".$row['company_contact']."</td>
	<td width='200'><a class='lenderlink' href='loanrequest.php?lenderid=".$row['member_id'].">Submit Loan Request</a></td></tr>
	<tr><td colspan='2'>";

    echo pullRating($row['member_id'],true,false,true);
	echo "</td></tr>
	</table></div><br>
	<hr style='border-top: 1px dotted #666666' color='#666666' width='400' size='1'>";

	include('footer.php');

}else{
include ('header.php');
echo "<h3>No Results Found</h3><br>
<p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>
<br><input type=button value='Return to Search' onClick='history.go(-1)'>";
include ('footer.php');
}

?>

Link to comment
Share on other sites

I'll try it..but wouldn't the absence of the 'while' loop limit me to one result? There would most likely be up to 10 lenders that would match the criteria for each category.

 

Let me give this a shot. Be right back.

 

Ok, gave it a whirl. Now, no matter what I select, it displays the 'No results match...' content. So, what's happening with this version is no results are being pulled.

Link to comment
Share on other sites

try this do we see anythink

 

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
ini_set("display_startup_errors", 1);

include ('db_config.php');
/**
* @author rockmetal
* @copyright 2007
*/

// query for extracting lenders from amember database
$category = strip_tags(trim($_POST['category']));
$loantype = strip_tags(trim($_POST['loan_type']));
//$loanamount = strip_tags(trim($_POST['loan_size']));

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql) or die(mysql_error());

while($row=mysql_fetch_assoc($results)){


include ('header.php');

echo "<h3>The following lenders meet your search criteria.</h3><br>";
// create display of results in abbreviated format

	echo " <div class='wrapper_out' onmouseover='this.className='wrapper''; onmouseout='this.className='wrapper_out'';>
	<table width='500' border='0' align='center'>
	<tr ><td colspan='2' class='lendertitle'>".$row['company_name']."</td></tr>
	<tr><td colspan='2' class='lenderdesc'>Company phone: ".$row['company_phone']."</td></tr>
	<tr><td width='300' class='lenderdesc'>Company contact: ".$row['company_contact']."</td>
	<td width='200'><a class='lenderlink' href='loanrequest.php?lenderid=".$row['member_id'].">Submit Loan Request</a></td></tr>
	<tr><td colspan='2'>";

    echo pullRating($row['member_id'],true,false,true);
	echo "</td></tr>
	</table></div><br>
	<hr style='border-top: 1px dotted #666666' color='#666666' width='400' size='1'>";

	include('footer.php');

}

?>

Link to comment
Share on other sites

<?php

include 'db_config.php';
/**
* @author rockmetal
* @copyright 2007
*/

// query for extracting lenders from amember database
$category = strip_tags(trim($_POST['category']));
$loantype = strip_tags(trim($_POST['loan_type']));
//$loanamount = strip_tags(trim($_POST['loan_size']));

$sql = "SELECT * FROM amember_members WHERE is_lender='Yes' AND '$category' IN (category_1, category_2, category_3, category_4, category_5) AND '$loantype' IN (loan_type, loan_type2, loan_type3, loan_type4, loan_type5) ORDER BY top_3 LIMIT 10";

$results = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($results) >= 1) // See the problem here? You're expecting more than one. The logic provided in the previous one only matched if there was -1- result.
{
include 'header.php';

echo "<h3>The following lenders meet your search criteria.</h3><br>";

while($row = mysql_fetch_assoc($results))
{
		echo "<div class='wrapper_out' onmouseover='this.className='wrapper''; onmouseout='this.className='wrapper_out'';><table width='500' border='0' align='center'><tr ><td colspan='2' class='lendertitle'>".$row['company_name']."</td></tr><tr><td colspan='2' class='lenderdesc'>Company phone: ".$row['company_phone']."</td></tr><tr><td width='300' class='lenderdesc'>Company contact: ".$row['company_contact']."</td><td width='200'><a class='lenderlink' href='loanrequest.php?lenderid=".$row['member_id'].">Submit Loan Request</a></td></tr><tr><td colspan='2'>";

	echo pullRating($row['member_id'], TRUE, FALSE, TRUE);
	echo "</td></tr></table></div><br><hr style='border-top: 1px dotted #666666' color='#666666' width='400' size='1'>";
}

include 'footer.php';

}
else
{
include 'header.php';
echo "<h3>No Results Found</h3><br>
<p class='bodytext'>No lenders matched your search criteria. Try modifying the search parameters for broader results.<br>
<br><input type=button value='Return to Search' onClick='history.go(-1)'>";
include 'footer.php';
}

?>

 

I don't understand why people are continuing without a loop. He expects >1 result, so always loop.

 

I cleaned up the code a bit since it was a bit of a mess. You may need to adjust the output of the echo in the loop.

Link to comment
Share on other sites

lol... dude, BELIEVE ME.. i'm totally appreciative of your help. :)

 

Now, just tried the latest version. STILL fails when one of the search criteria isn't met. I've been scratching my head on this till it bleeds.

 

Ok, more info:

 

What i'm doing is omitting the 'Stated' loan_type from a member's profile. Then, i'm searching for that loan type in the 2nd drop down along with a category in the first drop down that I know is listed in his profile. IF I select both criteria knowing full well  that they ARE in his profile then the results display fine.

 

SO...the way the query is written is 'AND' this 'AND' that so that it should meet both requirements in order to display in the results. IF both conditions aren't met (along with the 'is_lender' condition which specifies that their membership status as a lender) then it should display ' Sorry but there's no results '. IF both conditions are met then it should display the results.

 

It's so simple in concept but I can't figure out why it won't work.

 

IF $results DISPLAY $results.. IF NO $results DISPLAY this -> message

 

 

Ken2K7, the query works as is. What's not happening is the display of the 'else' content IF the search doesn't produce results. I don't think the single quotes would make a diff since the query is working without errors. Something is either breaking OR it's possible.... possible... that the reason the page is totally blank is because either PHP or MySQL is timing out on the query for some reason which is causing the script to die.

Link to comment
Share on other sites

redarrow, i'm using your code right now. Your latest post.

 

Go to: http://www.lenderfish.com/search-test.php

 

select Purchase in the 1st box and Rate and Term Refinance in the other. You'll see that there's results for that.

 

Go back to the search page and in just the second box select Stated while leaving the first box the same. There's no match for that. See what happens.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.