Jump to content

Need Help with array


cdoyle

Recommended Posts

Hi,

I've been trying to create a live search using ajax in my game.

I found a tutorial that almost does what I want

http://www.w3schools.com/php/php_ajax_suggest.asp

 

I needed it to pull data from my 'players' table, and was able to modify it by doing this instead of manually adding the names to the array.

$getnames = $db->execute("SELECT `id`, `username` from `players`");
while ($getnames1 = $getnames->fetchrow())
  {
   $a[] = $getnames1[username];
  }

This worked fine for listing out the name, but I eventually want to change the output to be links to the players profile.

 

So I need to include the `id` field into my array.

I thought maybe something like this would work

$a[] = array('username' => $getnames1['username'], 'id' => $getnames1['id']);

 

but it stops working once I do that (says no suggestions),

 

I think it's because I need to reference the 'username' below? 

Here is the code I have so far,  if I'm on the right track with the array.  How do I make the rest of the code execute like it did with just the username?

 


$getnames = $db->execute("SELECT `id`, `username` from `players`");
while ($getnames1 = $getnames->fetchrow())
  {
   //$a[] = $getnames1[username]; << this works OK

$a[] = array('username' => $getnames1['username'], 'id' => $getnames1['id']);
  }
       //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." <br /> ".$a[$i];
        
        }
      }
    }
  }

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
  {
  $response="no suggestion";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;
        ?>

Link to comment
https://forums.phpfreaks.com/topic/189790-need-help-with-array/
Share on other sites

With what you tried, you are loading an array into an array element.

This is fine, provided you address it as such

 

so in your original you could address usernames as $a[$i] which would be a username

 

in your trial code, you would need to address $a[$i]['username'] to get the username

or $a[$i]['id'] to get the ID

With what you tried, you are loading an array into an array element.

This is fine, provided you address it as such

 

so in your original you could address usernames as $a[$i] which would be a username

 

in your trial code, you would need to address $a[$i]['username'] to get the username

or $a[$i]['id'] to get the ID

 

Ah!

OK, the $a[$i] was confusing me in the original code.  I wasn't sure how the $i worked., and thought I had to do something with that.

 

I tried your suggestion and it works now.

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.