display no results found

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

display no results found

Post by jonnyfortis »

I have a search script that works fine, however when no searches are found i want to display " no results found" i want it to be displayed in the main body

the header php code showing from the SQL is below ( i have added what i thought would work)

if(count($results) > 0){
echo implode($results);
}
else {
echo "<p>$noresults</p>";
}

but that doesnt work

mysql_select_db($database_hostprop, $hostprop);
$query_Recordset1 = sprintf("SELECT ID, userid, FirstName, Surname, SalaryReq, PositionReq, location, otherComments, skills_offered FROM think_signup WHERE SalaryReq LIKE %s OR PositionReq LIKE %s OR location LIKE %s OR skills_offered LIKE %s", GetSQLValueString("%" . $var_SalaryReq_Recordset1 . "%", "text"),GetSQLValueString("%" . $var_PositionReq_Recordset1 . "%", "text"),GetSQLValueString("%" . $var_location_Recordset1 . "%", "text"),GetSQLValueString("%" . $var_skills_offered_Recordset1 . "%", "text"));
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $hostprop) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if(count($results) > 0){
echo implode($results);
}
else {
echo "<p>$noresults</p>";
}
if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_Recordset1") == false &&
stristr($param, "totalRows_Recordset1") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);

?>


and this is what i added to the body

<?php $noresults = "No results were found!";?>


thanks in advance
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: display no results found

Post by Robert07 »

It sounds like you might be getting a database error. Does it show any rows if there are results from the query? You aren't printing mysql errors if $all_Recordset1 doesn't exist, like you are with $Recordset1. Maybe you should try that to see if the error is from that query.
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: display no results found

Post by jonnyfortis »

as it is above it doesnt show errors, even if there are results found it still shows no record found. so it seems that it isnt passing the correct information and the php i have in the main body is static. i have made a change in the code by removing

if(count($results) > 0){
echo implode($results);
}
else {
echo "<p>$noresults</p>";

from the header and adding

$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
$result = $totalRows_Recordset1
}
to the bottom of the header code

but again this hasnt worked

i have also added
if(count($results) > 0){
echo implode($results);
}
else {
echo "<p>$noresults</p>";

to the body and removed what was there previously

thanks
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: display no results found

Post by Robert07 »

Try adding this to the top of your php file and see if you get any errors then:

#Show errors
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', true);
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: display no results found

Post by jonnyfortis »

added to the top of the page but when i run it no error is shown
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: display no results found

Post by x_mutatis_mutandis_x »

your $results variable is not declared or assigned to any value/variable, so count($results) is always 0
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: display no results found

Post by jonnyfortis »

ok so what do i do then

thanks in advance
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: display no results found

Post by x_mutatis_mutandis_x »

Code: Select all

//...your code
$Recordset1 = mysql_query($query_limit_Recordset1, $hostprop) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

//assign $results to the $row_Recordset1
$results = $row_Recordset1;

if(count($results) > 0){
 echo implode($results);
 }
 else {
 echo "<p>$noresults</p>";
 }
//...your code
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: display no results found

Post by jonnyfortis »

ok i added your code to my code

thanks

in the main body i have the following

<?php
$noresults = "No results were found!";
if(count($results) > 0){
echo implode($results);
}
else {
echo "<p>$noresults</p>";
}
?>

when i search and no results are shown it now gives the error

Warning: implode() [function.implode]: Argument to implode must be an array. in /usr/users1/jonfort/public_html/think/beta/candidate-search-results.php on line 409

line 409

and line 406 -414 are

<?php
$noresults = "No results were found!";
if(count($results) > 0){
echo implode($results);
}
else {
echo "<p>$noresults</p>";
}
?>


i think this doesnt need to be hear?
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: display no results found

Post by x_mutatis_mutandis_x »

Can you please post the entire code in your php file so that we can better understand the logic flow?
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: display no results found

Post by jonnyfortis »

<?php require_once('Connections/hostprop.php'); ?>
<?php
#Show errors
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', true);
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['userid'])) {
$loginUsername=$_POST['userid'];
$password=$_POST['password'];
$MM_fldUserAuthorization = "";
$MM_redirectLoginSuccess = "signup/signup-complete.php";
$MM_redirectLoginFailed = "failed.php";
$MM_redirecttoReferrer = false;
mysql_select_db($database_hostprop, $hostprop);

$LoginRS__query=sprintf("SELECT userid, password FROM think_signup WHERE userid=%s AND password=%s",
GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

$LoginRS = mysql_query($LoginRS__query, $hostprop) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
$loginStrGroup = "";

if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;

if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_Recordset1 = 5;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

$var_SalaryReq_Recordset1 = "xxxxx";
if (isset($_GET['SalaryReq'])) {
$var_SalaryReq_Recordset1 = $_GET['SalaryReq'];
}
$var_skills_offered_Recordset1 = "xxxxx";
if (isset($_GET['skills_offered'])) {
$var_skills_offered_Recordset1 = $_GET['skills_offered'];
}
$var_location_Recordset1 = "xxxxx";
if (isset($_GET['location'])) {
$var_location_Recordset1 = $_GET['location'];
}
$var_PositionReq_Recordset1 = "xxxxx";
if (isset($_GET['PositionReq'])) {
$var_PositionReq_Recordset1 = $_GET['PositionReq'];
}
mysql_select_db($database_hostprop, $hostprop);
$query_Recordset1 = sprintf("SELECT ID, userid, FirstName, Surname, SalaryReq, PositionReq, location, otherComments, skills_offered FROM think_signup WHERE SalaryReq LIKE %s OR PositionReq LIKE %s OR location LIKE %s OR skills_offered LIKE %s", GetSQLValueString("%" . $var_SalaryReq_Recordset1 . "%", "text"),GetSQLValueString("%" . $var_PositionReq_Recordset1 . "%", "text"),GetSQLValueString("%" . $var_location_Recordset1 . "%", "text"),GetSQLValueString("%" . $var_skills_offered_Recordset1 . "%", "text"));
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $hostprop) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

$results = $row_Recordset1;

if(count($results) > 0){
echo implode($results);
}
else {
echo "<p>$noresults</p>";
}


if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_Recordset1") == false &&
stristr($param, "totalRows_Recordset1") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);


?>



and below is the bosy code

<?php
$noresults = "No results were found!";
?>

<?php do { ?>
<table width="655" border="0" cellspacing="5" cellpadding="0">
<tr>
<td colspan="2" class="Titlegreen"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left"><a href="candidate-information.php?ID=<?php echo $row_Recordset1['ID']; ?>" class="joblinks"><?php echo $row_Recordset1['PositionReq']; ?></a></td>
<td>&nbsp;</td>
<td align="right"><?php echo $row_Recordset1['skills_offered']; ?></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2" class="textblack"><?php echo $row_Recordset1['otherComments']; ?></td>
</tr>
<tr>
<td width="250"><a href="candidate-information.php?ID=<?php echo $row_Recordset1['ID']; ?>" class="smallTXTlinksBlack">more info</a></td>
<td width="405"><div align="right"><span class="pos_salary"></span><span class="pos_location"> <?php echo $row_Recordset1['SalaryReq']; ?>/<?php echo $row_Recordset1['location']; ?></span></div></td>
</tr>
</table>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<p>&nbsp;
<table border="0">
<tr>
<td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>" class="smallTXTlinksBlack">First</a>
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>" class="smallTXTlinksBlack">Previous</a>
<?php } // Show if not first page ?></td>
<td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>" class="smallTXTlinksBlack">Next</a>
<?php } // Show if not last page ?></td>
<td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>" class="smallTXTlinksBlack">Last</a>
<?php } // Show if not last page ?></td>
</tr>
</table>
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: display no results found

Post by x_mutatis_mutandis_x »

Replace this block

Code: Select all


$results = $row_Recordset1;

if(count($results) > 0){
 echo implode($results);
 }
 else {
 echo "<p>$noresults</p>";
 }
with

Code: Select all


$results = mysql_num_rows($Recordset1);

if($results == 0) {
  echo "<p>$noresults</p>";
 }
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: display no results found

Post by jonnyfortis »

error in the at the top of the page

Notice: Undefined variable: noresults in /usr/users1/jonfort/public_html/think/beta/candidate-search-results.php on line 178

line 178 is

echo "<p>$noresults</p>";
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: display no results found

Post by x_mutatis_mutandis_x »

jonnyfortis wrote:error in the at the top of the page

Notice: Undefined variable: noresults in /usr/users1/jonfort/public_html/think/beta/candidate-search-results.php on line 178

line 178 is

echo "<p>$noresults</p>";

Code: Select all

$results = mysql_num_rows($Recordset1);
$noresults = 'No results found';
if($results == 0) {
   echo "<p>$noresults</p>";
  }
 
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: display no results found

Post by jonnyfortis »

ok thats great but..the No results found is at the top of the document and not in the body?
Post Reply