Jump to content

Archived

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

onthespot

php search engine

Recommended Posts

for my site, i want users to be able to search the names of other users and get on their profile pages! any ideas how i would go about doing this?

Share this post


Link to post
Share on other sites
dear friend,
if you are using mySQL to store the profiles of your registered users then you can execute an mysql query using PHP to display the users registered with you. but it depends what database or technology you are using to store the profiles of your users. and the same is what you have not mentioned.

Share this post


Link to post
Share on other sites
what exactly u need to know? i got the tables on the DB mate, with the profiles, all i want is a search box on my site, where if u type the name/ close to the name in the box and submit, the users profile (already avaliable) will be displayed!

Share this post


Link to post
Share on other sites
I think this is the third time 3 or 4 days I've posted the link to the same tutorial on searching using php and mysql..........

http://www.phpfreaks.com/tutorials/129/0.php

Share this post


Link to post
Share on other sites
also tho, say my profiles are on the end of the address so

http://mysite.com/username1
http://mysite.com/username2
http://mysite.com/username3

amd when i search i want the search to search for the profiles using the usernames!

is there anyway it can literally search the profiles on the end of addresses?

Share this post


Link to post
Share on other sites
ok sorry its bad grammar by myself

basically i want a search engine on my site where u can type in a users username and when u search u will get a list of usernames that are identical or similar to what u typed in! you can then select them and go to their profile page

Share this post


Link to post
Share on other sites
Assuming the usernames are stored in a db, then you can use a query like:

[code]SELECT * FROM users WHERE username LIKE '%$searchterm%';[/code]

Then just loop through the result and echo them out with links to the respective profiles.

Share this post


Link to post
Share on other sites
dear friend,
the problem is simple and you are making it complicated. just goto a website that offers tutorials for PHP and look for a tutorial that refers to searching through databases. thats it, you will surely find your answer there.
what i asked you initially was about the database you use. if it is MySQL then you can place a form tag for search'ing purpose and put action="abc.php".
then in abc.php you can put the code that produces the result from mySQL database.
suppose that the visitor to your website is looking for a username similar to $username.
then you can put the following in your abc.php file

[quote]
// connect to the server
if (!($link=mysql_pconnect($dbhostname,$dbusername,$dbpassword))) {
echo ("error connecting to the server");
exit();
}

// select the database
if (!mysql_select_db($database,$link)) {
echo ("error selecting the database");
exit();
}

// search for the requested/required username
$details = "SELECT * FROM dbtablename WHERE username LIKE '%$username%';
if (!($result = mysql_query($details,$link))) {
echo ("no match found".mysql_error());
exit();
}

// print the result if match is found
while ($myrow = mysql_fetch_array($result)) {
echo $myrow['username'];    // here you can print all the profiles you have. $myrow[] is an array and you have to place in [ here ] the values of entities placed in your db table.
}

[/quote]

i hope that i have made it easier for you.

Share this post


Link to post
Share on other sites
got a problem
ive installed that script, this is the form

<form action='profilesearch.php' method=get>
<input name=search type=text value='Enter username' />
<input type=submit name='user' value='Find Player' class=button />
</form></p>

to go with that, ive put all the details into the script about! then when i enter the username into the form, i get a blank screen, and in the url bar, /profilesearch.php?search=Enter+username&user=Find+Player, when i search ! whats wrong?

Share this post


Link to post
Share on other sites
I noticed one error straight away, on this line:

$details = "SELECT * FROM dbtablename WHERE username LIKE '%$username%';

Replace it with this

$details = "SELECT * FROM dbtablename WHERE username LIKE '%$username%'";

Share this post


Link to post
Share on other sites
using that code he gave you, just change this:
[code]

// search for the requested/required username
$details = "SELECT * FROM dbtablename WHERE username LIKE '%$username%';
if (!($result = mysql_query($details,$link))) {
  echo ("no match found".mysql_error());
  exit();
}
[/code]
to tihs
[code]

// search for the requested/required username
$username = $_GET['search'];
$details = "SELECT * FROM dbtablename WHERE username LIKE '%$username%';
if (!($result = mysql_query($details,$link))) {
  echo ("no match found".mysql_error());
  exit();
}
[/code]

maybe that'll help

Share this post


Link to post
Share on other sites
it would now appear im getting the username, so thankyou mate, however, there is no message saying, no profile found if there is no profile/error

Share this post


Link to post
Share on other sites
also how can i make the results be links to the actual profile pages?? this would be really good if you could provide me with the information!

Share this post


Link to post
Share on other sites
ok here, here ya go:
[code]
// search for the requested/required username
$username = $_GET['search'];
$details = "SELECT * FROM dbtablename WHERE username LIKE '%$username%';
if (!($result = mysql_query($details,$link))) {
  echo ("no match found".mysql_error());
  exit();
}

// print the result if match is found
while ($myrow = mysql_fetch_array($result)) {
echo "<a href='http://yoursite.com/" .$myrow['username'] . "'>" . $myrow['username'] . "</a>";    // here you can print all the profiles you have. $myrow[] is an array and you have to place in [ here ] the values of entities placed in your db table.
}
[/code]
be sure to replace yoursite.com with your site.. you did see it finds profiles like http://whatever.com/username right?

Share this post


Link to post
Share on other sites

×

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.