[jQuery] jquery autocomplete and mysql

[jQuery] jquery autocomplete and mysql

This is really a PHP question...
Your foreach loop is redefining the $returnData variable with every
iteration, so you'll only end up returning the last country in the
array. It looks like your mysql_fetch_array loop is also flawed in that
it writes to the same variables with each loop, effectively saving only
the last one. I think you might be looking for something more like this:
---
$rows = array();
while ( $row = mysql_fetch_array($result) )
{
    $rows[] = $row;
}
$returnXML = "<?xml version=\"1.0\"?>";
$returnXML.= "<ajaxresponse>";
foreach ($rows as $row)
{
    $returnXML .= " <item>";
    $returnXML .= "
<text><![CDATA[<strong>{$row['iso2']}</strong>
$name]]></text>";
    $returnXML .= " <value><![CDATA[{$row['iso2']}]]></value>";
    $returnXML .= "
<district><![CDATA[{$row['continent']}]]></district>";
    $returnXML .= "
<population><![CDATA[{$row['population']}]]></population>";
    $returnXML .= " </item>";
}
$returnXML.= "</ajaxresponse>";
echo $returnXML;
---
Let me know if I missed something or if this works out for you.
m.
-----Original Message-----
From: discuss-bounces@jquery.com [mailto:discuss-bounces@jquery.com] On
Behalf Of eugene33
Sent: Thursday, December 14, 2006 7:41 AM
To: discuss@jquery.com
Subject: Re: [jQuery] jquery autocomplete and mysql
Thanks Matt
I'm trying to build this PHP backend, again my skills are not so good so
if
anyone could point me in the right direction ;)
Here is what I come with, expecting to extract the data from my db and
then
sort them under a xml skeleton
So now it's working but I have only one country (the first one). The
foreach
function must be the problem, but I really can't figure why
<?php
header('Content-type: text/xml'); // output as XML
// $q = strtolower($_GET["q"]);
// if (!$q) return;
// connect to MySQL & load database
mysql_connect('localhost','xxx','xxx');
if (!@mysql_select_db('xxx')) { exit('

Unable to locate the '
.
$database . ' database at this time.

'); }

// retrieve customer data
$query="SELECT iso_alpha2, name, population, continent FROM
countryinfo ORDER BY iso_alpha2 DESC";
$result=mysql_query($query);
if (!$result) { exit('

Error performing query: ' .
mysql_error() .
'

'); }

// close db
mysql_close();

while ($row = mysql_fetch_array($result)) {
        $iso2 = $row['iso_alpha2'];
        $name = $row['name'];
        $population = $row['population'];
        $continent = $row['continent'];
// etc. etc.
    }
$rows = array('iso_alpha2');
foreach ($rows as $row) {
$returnData = " <item>";
$returnData.= " <text><![CDATA[<strong>$iso2</strong><br