Jump to content

Search functions


Gyohdon
 Share

Recommended Posts

Right now I own a website, which is still in test mode. The only real functional page I have is a page where people can leave comments. If they are logged in, their username is stored. If not, they're called Guest.I'm almost done, but I'm trying to create a search page.Right now, I'm testing it to search to specific comments, or comments by a specific user.It works, for one flaw: If nothing is inserted into my two text fields, all posts from Guests are shown.Here is my code:

if ($_POST['searchtype'] == 'Comments')	{	//Search for comments		if (isset($_POST['realname']))		{		if ($_POST['realname'] == 'Admin')			{			$realname = 'Thinkbin.net';			}		elseif ($_POST['realname'] != 'Admin')			{			$realname = mysql_real_escape_string($_POST['realname']);			}		}		if (isset($_POST['keyword']))		{		if (trim($_POST['keyword']) != "")			{			$keyword = mysql_real_escape_string($_POST['keyword']);			}		}		if (isset($realname) && isset($keyword))		{		$search = mysql_query("SELECT * FROM `test_table` WHERE Name LIKE '%$realname%' 			AND Comment LIKE '%$keyword%'");		}	elseif (isset($realname) && !isset($keyword))		{		$search = mysql_query("SELECT * FROM `test_table` WHERE Name LIKE '%$realname%'");		}	elseif (!isset($realname) && isset($keyword))		{		$search = mysql_query("SELECT * FROM `test_table` WHERE Comment LIKE '%$keyword%'");		}		if (isset($search))		{		if (mysql_num_rows($search) == 0)			{			echo '<div style="font-weight:bold;font-family:verdana;font-size:20px;text-align:center;">';			echo "Nothing was found. Please try again.";			echo '</div>';			}		elseif (mysql_num_rows($search) >= 1)			{			while ($result = mysql_fetch_array($search))				{				echo '<div class="comment">					' . $result['Comment'] . '					<br />					<i><b>Written by ' . $result['Name'] . '</b></i>									<br />								</div>								<br />';				}			}		}	else		{		echo '<div style="font-weight:bold;font-family:verdana;font-size:20px;text-align:center;">';		echo "Nothing was found. Please try again.";		echo '</div>';		}	}

Edited by Gyohdon
Link to comment
Share on other sites

add:
if(empty($_POST['realname']) && empty($_POST['keyword'])){/* handle if both values are empty or not set */}else{/* rest of code */}

Bedankt ;DI'll try it right now.I've got it work.Thanks! Edited by Gyohdon
Link to comment
Share on other sites

if i am not missundersatnding...if a blank space has passed in $_POST['realname']empty will evaulate FALSE but in%$realname%'it will try to match all character which have space in beetwwen.and proabably it will show you up the all data.i think using regexp will be more apropiate.

Edited by birbal
Link to comment
Share on other sites

if i am not missundersatnding...if a blank space has passed in $_POST['realname']empty will evaulate FALSE but in%$realname%'it will try to match all character which have space in beetwwen.and proabably it will show you up the all data.i think using regexp will be more apropiate.
I've trimmed before checking if it is empty.It works now.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...