Page 1 of 1

AJAX and PHP

Posted: Wed Mar 15, 2006 10:58 am
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'";
	 }

Re: AJAX and PHP

Posted: Wed Mar 15, 2006 11:22 am
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;

Posted: Wed Mar 15, 2006 11:26 am
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;

Posted: Wed Mar 15, 2006 11:45 am
by AYYASH
Tow different answers. I'm in heaven.
Thanks
I'll try and let you know

Posted: Wed Mar 15, 2006 11:57 am
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.

Posted: Wed Mar 15, 2006 1:44 pm
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. :)

Posted: Wed Mar 15, 2006 2:36 pm
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);

Posted: Wed Mar 15, 2006 11:02 pm
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...

another thing

Posted: Thu Mar 16, 2006 1:15 am
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.