applebiz89 Posted November 27, 2009 Share Posted November 27, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/ Share on other sites More sharing options...
.josh Posted November 28, 2009 Share Posted November 28, 2009 only thing I see "wrong" with that code is you have spaces between your variable and value in your href. should be profile-view.php?username=$username and on profile-view.php that value will be in $_GET['username'] Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-966725 Share on other sites More sharing options...
applebiz89 Posted November 28, 2009 Author Share Posted November 28, 2009 Ive put the spaces together and it opens the page but the select statements in the profile-view.php arnt picking up the $username variable. Anything else anyone can suggest? Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-966944 Share on other sites More sharing options...
.josh Posted November 28, 2009 Share Posted November 28, 2009 okay, on profile-view.php show what you're trying to do. You said "the $username variable'. Are you trying to use $username or $_GET['username']? Show your code, we can't help if we can't see. Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-966961 Share on other sites More sharing options...
applebiz89 Posted November 28, 2009 Author Share Posted November 28, 2009 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-966966 Share on other sites More sharing options...
blueman378 Posted November 28, 2009 Share Posted November 28, 2009 try mypage.php?username=testuser Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-967142 Share on other sites More sharing options...
applebiz89 Posted November 28, 2009 Author Share Posted November 28, 2009 yes, it works when i for instance use 'profile-view.php?username=gary' . So i know it works, but how can i parse the usernames that come up in the search so it uses that usernames information Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-967145 Share on other sites More sharing options...
rajivgonsalves Posted November 28, 2009 Share Posted November 28, 2009 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/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-967147 Share on other sites More sharing options...
.josh Posted November 28, 2009 Share Posted November 28, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-967158 Share on other sites More sharing options...
applebiz89 Posted November 28, 2009 Author Share Posted November 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-967185 Share on other sites More sharing options...
.josh Posted November 28, 2009 Share Posted November 28, 2009 Your OP code suggests you already know how to do that... your search query returns the results. You loop through the results, echoing out links, with the variable instead of hardcoded names... Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-967192 Share on other sites More sharing options...
applebiz89 Posted November 29, 2009 Author Share Posted November 29, 2009 ive got it working! thanks guys, i was just getting confused a bit there, my mistake cheers! Quote Link to comment https://forums.phpfreaks.com/topic/183166-search-function/#findComment-967218 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.