AJAX and PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AYYASH
Forum Newbie
Posts: 16
Joined: Sat Aug 14, 2004 2:33 am

AJAX and PHP

Post by AYYASH »

I'm using an AJAX script to do auto-complete text field and it takes values from DB.
The problem that the code below give only the last value when I type inside the text field but when I print the result (print $names;) it gives all the values. The solution is that the varuable that holds the values should look like this:

Code: Select all

$names = "'a', 'b', 'c', 'd'";
How can I make it using this code:

Code: Select all

$result = "SELECT ben_id, ben_name FROM beneficiaries";
	  $query = mysql_query($result);
	  $num = mysql_num_rows($query);
	  while ($row = mysql_fetch_array($query)) {
	  $name = $row['ben_name'];
	  $names = "'$name'";
	 }
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: AJAX and PHP

Post by Chris Corbyn »

AYYASH wrote:I'm using an AJAX script to do auto-complete text field and it takes values from DB.
The problem that the code below give only the last value when I type inside the text field but when I print the result (print $names;) it gives all the values. The solution is that the varuable that holds the values should look like this:

Code: Select all

$names = "'a', 'b', 'c', 'd'";
How can I make it using this code:

Code: Select all

$result = "SELECT ben_id, ben_name FROM beneficiaries";
	  $query = mysql_query($result);
	  $num = mysql_num_rows($query);
	  while ($row = mysql_fetch_array($query)) {
	  $name = $row['ben_name'];
	  $names = "'$name'";
	 }

Code: Select all

$result = "SELECT ben_id, ben_name FROM beneficiaries";
	  $query = mysql_query($result);
	  $num = mysql_num_rows($query);
         
         $loop = false;
          $names = '';
	  while ($row = mysql_fetch_array($query)) {
	  $name = $row['ben_name'];
	  if (!$loop) $names .= "'$name'";
          else $names .= ", '$name'";
           $loop = true;
	 }

echo $names;
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$result = "SELECT ben_id, ben_name FROM beneficiaries";
      $query = mysql_query($result);
      $num = mysql_num_rows($query);
         
      $names = '';
      while ($row = mysql_fetch_array($query)) {
          $name = $row['ben_name'];
          if (strlen($names)) 
              $names .= ",";
          $names .= "'$name'";
      }

      echo $names;
AYYASH
Forum Newbie
Posts: 16
Joined: Sat Aug 14, 2004 2:33 am

Post by AYYASH »

Tow different answers. I'm in heaven.
Thanks
I'll try and let you know
AYYASH
Forum Newbie
Posts: 16
Joined: Sat Aug 14, 2004 2:33 am

Post by AYYASH »

Both have worked just fine.
Thanks

I wish I could have a mind like yours but I think I'm too old to have one.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

AYYASH wrote:I wish I could have a mind like yours but I think I'm too old to have one.
Sorry, age doesn't prevent brilliance. Some would argue its the best way to accomplish it, in fact. :)
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post by Gambler »

This is the usual problem of separating comma.

Code: Select all

$result = mysql_query("SELECT ben_id, ben_name FROM beneficiaries");

$names = array();
while ($row = mysql_fetch_assoc($result)) {
    $names[] = "'$row[ben_name]'";
}
$names = join(', ', $names);
AYYASH
Forum Newbie
Posts: 16
Joined: Sat Aug 14, 2004 2:33 am

Post by AYYASH »

Thanks Gambler for adding a third option.
Sorry, age doesn't prevent brilliance. Some would argue its the best way to accomplish it, in fact.
Thanks Roja.
Well, age comes with huge responsibilities. If I'm 20 and all what I have is to study only. I would have bigger chance to learn than a 30 years old man who is married, have kids, have to work hard to feed them, pay all bills, etc...
AYYASH
Forum Newbie
Posts: 16
Joined: Sat Aug 14, 2004 2:33 am

another thing

Post by AYYASH »

Now I'm able to show the name in the text field but the trick now is how I can post the id instead of the name.
Ineed the name to appear in the text field and its id be sent to the db. T
his is how the text field looks like:

Code: Select all

<input name='names' type='text' title="Names" 
onfocus="do_arrayvalues('names')" onKeyUp="autoComplete(this,'names')" autocomplete="off" />
I've tried to set the value of the text field to use the id but it didn't work. I wonder if the hidden field would work but I don't know how to do it.
Post Reply