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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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