Jump to content

Get username from same row as firstname and lastname


Fenhopi

Recommended Posts

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;
?>

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>";

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>

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.