Fenhopi Posted November 11, 2010 Share Posted November 11, 2010 Hi, This is a part of a script that suggests firstname+lastname when a user tries to search. What I'm trying to do is that the suggestions become links to the users profile, but then I have to get the username from the same row as each of the names it suggests. Here's my code so far: $GetNames = "SELECT firstname, lastname, username FROM users"; $GetNamesConnect = $database->query($GetNames); While($row = mysql_fetch_array($GetNamesConnect)) { $a[] = $row['firstname'] . " " . $row['lastname']; } //get the q parameter from URL $q=$_GET["q"]; //lookup all hints from array if length of q>0 if (strlen($q) > 0) { $hint=""; for($i=0; $i<count($a); $i++) { if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) { if ($hint=="") { $hint=$a[$i]; } else { $hint=$hint.", ".$a[$i]; } } } } // Set output to "no suggestion" if no hint were found // or to the correct values if ($hint == "") { $response="no suggestion"; } else { $response= "<a href=\"profile.php?user=(THIS IS WHERE I NEED THE USERNAME)\">$hint</a>"; } //output the response echo $response; ?> Quote Link to comment https://forums.phpfreaks.com/topic/218433-get-username-from-same-row-as-firstname-and-lastname/ Share on other sites More sharing options...
BlueSkyIS Posted November 11, 2010 Share Posted November 11, 2010 in your query, also grab the record id and use that to identify the record $GetNames = "SELECT id, firstname, lastname, username FROM users"; $GetNamesConnect = $database->query($GetNames); While($row = mysql_fetch_array($GetNamesConnect)) { $a[$row['id']] = $row['firstname'] . " " . $row['lastname']; } // then, later.... $response= "<a href='profile.php?user=$id'>$hint</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/218433-get-username-from-same-row-as-firstname-and-lastname/#findComment-1133254 Share on other sites More sharing options...
Fenhopi Posted November 12, 2010 Author Share Posted November 12, 2010 Thank you for replying! That messes up my script though, as in it doesn't get what's going on in the loop. I should post the whole script: This is the gethint.php: <?php include("include/session.php"); $GetNames = "SELECT firstname, lastname, username FROM users"; $GetNamesConnect = $database->query($GetNames); While($row = mysql_fetch_array($GetNamesConnect)) { $a[$row['username']] = $row['firstname'] . " " . $row['lastname']; } //get the q parameter from URL $q=$_GET["q"]; //lookup all hints from array if length of q>0 if (strlen($q) > 0) { $hint=""; for($i=0; $i<count($a); $i++) { if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) { if ($hint=="") { $hint=$a[$i]; } else { $hint=$hint.", ".$a[$i]; } } } } // Set output to "no suggestion" if no hint were found // or to the correct values if ($hint == "") { $response="no suggestion"; } else { $id = $row['username']; $response= "<a href='profile.php?user=$row['username']'>$hint</a>"; } //output the response echo $response; ?> And this is the form that retrieves the suggestions: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <script type="text/javascript"> function showHint(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","gethint.php?q="+str,true); xmlhttp.send(); } </script> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/218433-get-username-from-same-row-as-firstname-and-lastname/#findComment-1133365 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.