Jump to content

Search results


anevins

Recommended Posts

Hi,

I have a problem if users search something which doesn't match the data in the table I'm selecting from.

 

At the moment I'm trying this code:

<?php 
require_once('inc/global.inc.php');

# search.inc.php

/* 
*	This is the search content module.
*	This page is included by index.php.
*	This page expects to receive $_GET['terms'].
*/

// Redirect if this page was accessed directly:
if (!defined('BASE_URL')) {

// Need the BASE_URL, defined in the config file:
require_once ('../includes/config.inc.php');

// Redirect to the index page:
$url = BASE_URL . 'index.php?p=search';

// Pass along search terms?
if (isset($_GET['terms'])) {
	$url .= '&terms=' . urlencode($_GET['terms']);
}

header ("Location: $url");
exit;

} // End of defined() IF.

// Print a caption:
echo '<h2>Search Results</h2>';

// Display the search results if the form
// has been submitted.
if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) {

$terms = $_GET['terms'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server');
// Query the database.
$query = "SELECT * FROM product WHERE title LIKE '%$terms%'";

// Fetch the results.
//$row = mysqli_fetch_array($result);

// Print the results:

	$result=mysqli_query($dbc,$query);

	if ($result == 0){
	  echo "<h3>Sorry,</h3>";
	  echo "<p>your search: "" .$terms. "" returned zero results</p>";
	}

	while($row=mysqli_fetch_assoc($result)) {

			$output[] = '<ul>';
			$output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>';
			$output[] = '</ul>';
	}
	echo join('',$output);
}

else { // Tell them to use the search form.
echo '<p class="error">Please use the search form at the top of the window to search this site.</p>';
}	



?>

 

But I'm getting this error:

Object of class mysqli_result could not be converted to int

 

So that little if statement here doesn't work:

	if ($result == 0){
	  echo "<h3>Sorry,</h3>";
	  echo "<p>your search: "" .$terms. "" returned zero results</p>";
	}

 

Is there another way I can display no search matched results?

 

Thank you for reading,

Andrew.

Link to comment
https://forums.phpfreaks.com/topic/230242-search-results/
Share on other sites

That gets rid of one error; for that particular if statement, but now I have another error which I think is greater than this if statement.

 

Here's the new error:

Undefined variable: output

 

So the new code is:

<?php 
require_once('inc/global.inc.php');

# search.inc.php

/* 
*	This is the search content module.
*	This page is included by index.php.
*	This page expects to receive $_GET['terms'].
*/

// Redirect if this page was accessed directly:
if (!defined('BASE_URL')) {

// Need the BASE_URL, defined in the config file:
require_once ('../includes/config.inc.php');

// Redirect to the index page:
$url = BASE_URL . 'index.php?p=search';

// Pass along search terms?
if (isset($_GET['terms'])) {
	$url .= '&terms=' . urlencode($_GET['terms']);
}

header ("Location: $url");
exit;

} // End of defined() IF.

// Print a caption:
echo '<h2>Search Results</h2>';

// Display the search results if the form
// has been submitted.
if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) {

$terms = $_GET['terms'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server');
// Query the database.
$query = "SELECT * FROM product WHERE title LIKE '%$terms%'";

// Fetch the results.
//$row = mysqli_fetch_array($result);

// Print the results:

	$result=mysqli_query($dbc,$query);

	if ($result == false){
	  echo "<h3>Sorry,</h3>";
	  echo "<p>your search: "" .$terms. "" returned zero results</p>";
	}

	while($row=mysqli_fetch_assoc($result)) {

			$output[] = '<ul>';
			$output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>';
			$output[] = '</ul>';
	}
	echo join('',$output);
}

else { // Tell them to use the search form.
echo '<p class="error">Please use the search form at the top of the window to search this site.</p>';
}	

?>

Link to comment
https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185713
Share on other sites

It looks like you didn't create the output array before you started using it. Try this:

<?php 
require_once('inc/global.inc.php');

# search.inc.php

/* 
*	This is the search content module.
*	This page is included by index.php.
*	This page expects to receive $_GET['terms'].
*/

// Redirect if this page was accessed directly:
if (!defined('BASE_URL')) {

// Need the BASE_URL, defined in the config file:
require_once ('../includes/config.inc.php');

// Redirect to the index page:
$url = BASE_URL . 'index.php?p=search';

// Pass along search terms?
if (isset($_GET['terms'])) {
	$url .= '&terms=' . urlencode($_GET['terms']);
}

header ("Location: $url");
exit;

} // End of defined() IF.

// Print a caption:
echo '<h2>Search Results</h2>';

// Display the search results if the form
// has been submitted.
if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) {

$terms = $_GET['terms'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server');
// Query the database.
$query = "SELECT * FROM product WHERE title LIKE '%$terms%'";

// Fetch the results.
//$row = mysqli_fetch_array($result);

// Print the results:

	$result=mysqli_query($dbc,$query);

	if ($result === false){
	  echo "<h3>Sorry,</h3>";
	  echo "<p>your search: "" .$terms. "" returned zero results</p>";
	}

	$output = Array();
	while($row=mysqli_fetch_assoc($result)) {

			$output[] = '<ul>';
			$output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>';
			$output[] = '</ul>';
	}
	echo join('',$output);
}

else { // Tell them to use the search form.
echo '<p class="error">Please use the search form at the top of the window to search this site.</p>';
}	

?>

 

Link to comment
https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185715
Share on other sites

Brilliant, thank you, I'm almost there.

I'm still getting a blank page where there should be "Sorry, your search ...".

 

Have I positioned this if statement in the wrong place?

if ($result == false){
	  echo "<h3>Sorry,</h3>";
	  echo "<p>your search: "" .$terms. "" returned zero results</p>";
	}

Link to comment
https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185718
Share on other sites

Are you using the three equals signs - "===" - like I suggested or just the two - "==" - like I saw in your code snippet?

 

If you're using just the two then try replacing "false" with "null".

However if you use the three then you should be able to use "0", "false" or "null".

Link to comment
https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185719
Share on other sites

Right, I've got a final idea on this - but unfortunately I'm not very good at php mySql related queries. I've looked at the php manual and your query should be returning false.

 

Here is the code I think may work for you:

<?php 
require_once('inc/global.inc.php');

# search.inc.php

/* 
*	This is the search content module.
*	This page is included by index.php.
*	This page expects to receive $_GET['terms'].
*/

// Redirect if this page was accessed directly:
if (!defined('BASE_URL')) {

// Need the BASE_URL, defined in the config file:
require_once ('../includes/config.inc.php');

// Redirect to the index page:
$url = BASE_URL . 'index.php?p=search';

// Pass along search terms?
if (isset($_GET['terms'])) {
	$url .= '&terms=' . urlencode($_GET['terms']);
}

header ("Location: $url");
exit;

} // End of defined() IF.

// Print a caption:
echo '<h2>Search Results</h2>';

// Display the search results if the form
// has been submitted.
if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) {

$terms = $_GET['terms'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server');
// Query the database.
$query = "SELECT * FROM product WHERE title LIKE '%$terms%'";

// Fetch the results.
//$row = mysqli_fetch_array($result);

// Print the results:

	if($result=mysqli_query($dbc,$query)){
			$output = Array();
			while($row=mysqli_fetch_assoc($result)) {

					$output[] = '<ul>';
					$output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>';
					$output[] = '</ul>';
			}
			echo join('',$output);
	} else {
			echo "<h3>Sorry,</h3>";
			echo "<p>your search: "" .$terms. "" returned zero results</p>";
	}

}

else { // Tell them to use the search form.
echo '<p class="error">Please use the search form at the top of the window to search this site.</p>';
}	

?>

 

 

If this does not work then I'm not quite sure what you could do to fix it.

 

You may try tracing the result variable to see the contents.

 

I hope you're able to get this sorted.

Link to comment
https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185729
Share on other sites

Well I'm back again with another idea, try using this script:

<?php 
require_once('inc/global.inc.php');

# search.inc.php

/* 
*	This is the search content module.
*	This page is included by index.php.
*	This page expects to receive $_GET['terms'].
*/

// Redirect if this page was accessed directly:
if (!defined('BASE_URL')) {

// Need the BASE_URL, defined in the config file:
require_once ('../includes/config.inc.php');

// Redirect to the index page:
$url = BASE_URL . 'index.php?p=search';

// Pass along search terms?
if (isset($_GET['terms'])) {
	$url .= '&terms=' . urlencode($_GET['terms']);
}

header ("Location: $url");
exit;

} // End of defined() IF.

// Print a caption:
echo '<h2>Search Results</h2>';

// Display the search results if the form
// has been submitted.
if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) {

$terms = $_GET['terms'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server');
// Query the database.
$query = "SELECT * FROM product WHERE title LIKE '%$terms%'";
// Fetch the results.
//$row = mysqli_fetch_array($result);
// Print the results:
$result=mysqli_query($dbc,$query);	
if (!$result){
	echo "<h3>Sorry,</h3>";
	echo "<p>your search: "" .$terms. "" returned zero results</p>";
} else {		
	$output = Array();
	while($row=mysqli_fetch_assoc($result)) {
		$output[] = '<ul>';
		$output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>';
		$output[] = '</ul>';
	}
	echo join('',$output);
}
} else { // Tell them to use the search form.
echo '<p class="error">Please use the search form at the top of the window to search this site.</p>';
}	

?>

 

Link to comment
https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185836
Share on other sites

Have you tried printing the "$result" variable to see what content it holds?

 

Also, you could try placing several traces over the script, just as simple as something like echo "1"; - where you increment the number.

This will give you a good idea of where the code is going.

Link to comment
https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185860
Share on other sites

I want to use the num rows mysqli function so, if there are results greater than 0, do the query, else output 'no results etc.'

 

I don't know how to implement this through my code, could anyone help me out here?

 

<?php 
  require_once('./includes/connectvars.php');

# search.inc.php

/* 
*	This is the search content module.
*	This page is included by index.php.
*	This page expects to receive $_GET['terms'].
*/

// Redirect if this page was accessed directly:
if (!defined('BASE_URL')) {

// Need the BASE_URL, defined in the config file:
require_once ('../includes/config.inc.php');

// Redirect to the index page:
$url = BASE_URL . 'index.php?p=search';

// Pass along search terms?
if (isset($_GET['terms'])) {
	$url .= '&terms=' . urlencode($_GET['terms']);
}

header ("Location: $url");
exit;

} // End of defined() IF.

// Print a caption:
echo '<h2>Search Results</h2>';

// Display the search results if the form
// has been submitted.
if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) {

$terms = $_GET['terms'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server');
// Query the database.
$query = "SELECT * FROM product WHERE title LIKE '%$terms%'";
// Fetch the results.
//$row = mysqli_fetch_array($result);
// Print the results:
$result=mysqli_query($dbc,$query);	
if (!$result){
	echo "<h3>Sorry,</h3>";
	echo "<p>your search: "" .$terms. "" returned zero results</p>";
} else {		
	$output = Array();
	while($row=mysqli_fetch_assoc($result)) {
		$output[] = '<ul>';
		$output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>';
		$output[] = '</ul>';
	}
	echo join('',$output);
}
} else { // Tell them to use the search form.
echo '<p class="error">Please use the search form at the top of the window to search this site.</p>';
}	

?>

Link to comment
https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1186184
Share on other sites

I'm trying to output a message when there are no results for the search,

this is my code:

 

<?php 
  require_once('./includes/connectvars.php');

# search.inc.php

/* 
*	This is the search content module.
*	This page is included by index.php.
*	This page expects to receive $_GET['terms'].
*/

// Redirect if this page was accessed directly:
if (!defined('BASE_URL')) {

// Need the BASE_URL, defined in the config file:
require_once ('../includes/config.inc.php');

// Redirect to the index page:
$url = BASE_URL . 'index.php?p=search';

// Pass along search terms?
if (isset($_GET['terms'])) {
	$url .= '&terms=' . urlencode($_GET['terms']);
}

header ("Location: $url");
exit;

} // End of defined() IF.

// Print a caption:
echo '<h2>Search Results</h2>';

// Display the search results if the form
// has been submitted.
if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) {

$terms = $_GET['terms'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server');
// Query the database.
$query = "SELECT * FROM product WHERE title LIKE '%$terms%'";
// Fetch the results.
$result=mysqli_query($dbc,$query);	
// Print the results:
$num_rows = mysql_num_rows($result);

	if ($num_rows > 0){

		$output = Array();
		$output[] = '<ul>';
		$output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>';
		$output[] = '</ul>';

		echo join('',$output);
	}

	else {
		echo "<h3>Sorry,</h3>";
		echo "<p>your search: "" .$terms. "" returned zero results</p>";
	}

} 

else { // Tell them to use the search form.
echo '<p class="error">Please use the search form at the top of the window to search this site.</p>';
}	

?>

 

I think there's some conflict with the mysql and mysqli funcitons, got any ideas anyone?

Link to comment
https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1186306
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.