Jump to content

search function


applebiz89

Recommended Posts

HI guys,

 

if anyone could point us in the right direction of how to do this, or provide some test code for a similar problem would be great!

 

Basically, I have user profiles on my site, and I have a search by for example firstnames will list all usernames with the first name 'bob'. With this list I want to turn the output of each name to a link to their profile.

 

the profile page is profile-view.php and I am using the statement "select username from members where firstname = $input";

 

and

$num_results = mysql_num_rows($result);

 

 

$username = $row['username'];

 

for ($i=0; $i <$num_results; $i++)

{

$num_found = $i ;

$row = mysql_fetch_assoc($result);

echo "<a href='profile-view.php?username = $username'>{$row['username']}</a><br/>";

}

 

I know the <a href> wont work, but is there anyway of being able to do this, all usernames are unique across the site too thats why I thought the link it by this.

 

Thanks in advance

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

<?php 
$username=$_GET['username'];

$result=mysql_query("SELECT * FROM musicians WHERE username='$username'");

while($row = mysql_fetch_array($result))
{
    
echo  $row['username'] ; 
echo  $row['firstname'] "; 
echo $row['lastname']; 
echo  $row['email'] "; 
echo $row['age'] ; 
echo $row['instrument'];
echo  $row['location'] "; 
echo$row['postcode'] ;
echo  $row['genre']; 
}

?>

 

This code does output what I want it to do when I call it by a specific username. But as i said i want it to work so that the username that appears in the search then links so their information is displayed by their username passed over.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/183166-search-function/#findComment-966966
Share on other sites

this

 

for ($i=0; $i <$num_results; $i++)
{
$num_found = $i ;
$row = mysql_fetch_assoc($result);
echo "<a href='profile-view.php?username = $username'>{$row['username']}</a><br/>";
}

 

should be

 

while ($row = mysql_fetch_assoc($result) 
{
$num_found = $i ;
echo "<a href='profile-view.php?username={$row['username']}'>{$row['username']}</a><br/>";
}

Link to comment
https://forums.phpfreaks.com/topic/183166-search-function/#findComment-967147
Share on other sites

Okay I'm confused.  This is how I understand the situation to be:

 

You have a search engine script.  Someone enters in something in the search field, and the search engine spits out a list of link-ified names.  You then want to click on one of those names, and it pulls up the rest of the info about them. 

 

You said that each username in the database is unique.  Therefore, clicking on this:

 

<a href='profile-view.php?username=tom'>tom</a>

 

And then in profile-view.php, having this:

 

<?php 
$username=$_GET['username'];

$result=mysql_query("SELECT * FROM musicians WHERE username='$username'");

while($row = mysql_fetch_array($result))
{
    
   echo  $row['username'] ; 
   echo  $row['firstname'] "; 
   echo $row['lastname']; 
   echo  $row['email'] "; 
   echo $row['age'] ; 
   echo $row['instrument'];
   echo  $row['location'] "; 
   echo$row['postcode'] ;
   echo  $row['genre']; 
}

?>

 

Since you said there is only 1 row in the database where username = 'tom', that will echo out tom's information.  That is what you are wanting to do, right?  So what is the problem?  What part of that scenario is not happening?

 

 

Link to comment
https://forums.phpfreaks.com/topic/183166-search-function/#findComment-967158
Share on other sites

yeah thats right, but Im confused as to how to make the <href ...> link, link to the usernames that are found to in the search.

 

So, say I was to search for 'David' and each person to have the name david came out with the list

 

applebiz

gary123

jimmy123

 

so all these usernames are members called david. I want to link to page-view.php automatically by their username to display all their information without physically writing 'page-view.php?username=applebiz'  because I don't know their username yet as it is in the search

 

Link to comment
https://forums.phpfreaks.com/topic/183166-search-function/#findComment-967185
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.