Friend search feature.

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

Post Reply
ryansstuff
Forum Newbie
Posts: 3
Joined: Sat Sep 10, 2011 8:41 am

Friend search feature.

Post by ryansstuff »

Right on my site park profile,
I have made up a search feature for my users to search for people
http://prntscr.com/2zpro

But the issue is that it only works if they put just the frist name or seconds name i need help on how i could get it to make it work for full names.
I used this site http://www.webreference.com/programming ... rch/2.html

The code on that page is,

Code: Select all

<?php include("auth.php"); ?>
<?php include("sections/database.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
		<link rel="icon" href="images/favicon.ico" type="image/x-icon" /> 
		<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
		<title>Search Friends</title>
		<link rel="stylesheet" type="text/css" href="styles.css" media="all" >
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

	</head>
	<body>
		<?php include("sections/header.php"); ?>
		<?php include("sections/advert.php"); ?>	
		<div id="container">
		<?php
			If ($loggedin == 'T')
			{
				//This code to run if the person is logged in.
				echo "<div id=\"headerbar\">Search For a friend! </div>"				
				;
				echo"<h3>Search Contacts Details</h3>
				<p>You may search either by first or last name</p>
				<form method=\"post\" action=\"searchfriends.php\" id=\"searchform\">
				<input type=\"text\" name=\"name\">
				<input type=\"submit\" name=\"submit\" value=\"Search\">
				</form>";
				//echo"<p><a href=\"?by=A\">A</a> | <a href=\"?by=B\">B</a> | <a href=\"?by=K\">K</a></p>";

				if(isset($_POST['submit'])){
					if(preg_match("/[A-Z | a-z]+/", $_POST['name'])){
						$name=$_POST['name'];



						//-query the database table
						$sql="SELECT uid, firstname, lastname FROM user WHERE firstname LIKE '%" . $name . "%' OR lastname LIKE '%" . $name ."%'";

						//-run the query against the mysql query function
						$result=mysql_query($sql);

						//-count results

						$numrows=mysql_num_rows($result);

						echo "<p>" .$numrows . " results found for " . stripslashes($name) . "</p>"; 

						//-create while loop and loop through result set
						while($row=mysql_fetch_array($result)){

							$FirstName =$row['firstname'];
							$LastName=$row['lastname'];
							$ID=$row['uid'];
								
						//-display the result of the array

						echo "<ul>\n"; 
						echo "<li>" . "<a href=\"profile.php?uid=$ID\">"  .$FirstName . " " . $LastName . "</a></li>\n";
						echo "</ul>";
						}
					}
					else
					{
						echo "<p>Please enter a search query</p>";
					}
				}
			}
			else
			{
				//The code below is for the people who are not logged in.
				echo "<div id=\"headerbar\"> Welcome to Park profile,</div> \r\n"
				."
					<p>
						On this website you are able to meet other people who are also fanatical about going to Theme Parks, and it gives you the chance to meet up with them at your favourite Parks and to share your experience. This site also has the advantage of allowing you to record how often you have been on each ride, so you can boast with accuracy to your friends. 
					</p>
					<p>
						Use our site to log onto our new feature system and input what day and Park you are going to, and click to see which of your friend’s will also be there that day so you can either meet up, or avoid them ;) 
					</p>
					<p>
						So what are you waiting for? Join the site, and let the fun begin.
					</p>
				"
				;
			}
			?>
		</div>
		<?php include("sections/footer.php"); ?>
		
	</body>
</html>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Friend search feature.

Post by Celauran »

Try exploding $name on spaces.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: Friend search feature.

Post by ok »

You can also include a search engine in your site, like Zend_Search_Lucene:
http://framework.zend.com/manual/en/zen ... ucene.html
ryansstuff
Forum Newbie
Posts: 3
Joined: Sat Sep 10, 2011 8:41 am

Re: Friend search feature.

Post by ryansstuff »

So like this?

Code: Select all

						$name=$_POST['name'];

						$name = explode(" ", $pizza);
						echo $name[0]; // piece1
						echo $name[1]; // piece2


						//-query the database table
						$sql="SELECT uid, firstname, lastname FROM user WHERE firstname LIKE '%" . $name[0] . "%' AND lastname LIKE '%" . $name[1] ."%'";

						//-run the query against the mysql query function
						$result=mysql_query($sql);
Because now i did that and everyone on this database showed up. http://prntscr.com/301re
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: Friend search feature.

Post by ok »

You searched for an empty string and it returned you everything? Am I correct?

By the way, you might want to check whether first and last names are given or just first or last. Plus, this method is not very accurate since the last name might have spaces in it...
ryansstuff
Forum Newbie
Posts: 3
Joined: Sat Sep 10, 2011 8:41 am

Re: Friend search feature.

Post by ryansstuff »

No it not a blank string i just removed the echo code as it was messing up as its an array.

Ok so this method not going to work. The issue is i am a newbie and only just learnign php mysql and things like that. this Zend_Search_Lucene:
looks like a lot of work for a simple search and i harsdly understand it. But if i use that dont all my website have to be under the zend framework?
Post Reply