Seite 1 von 1

Ajax autocomplete searchbox und php array problem

Verfasst: Do 27. Jul 2006, 15:38
von djavet
Hello

I'm workin a ajax search box (my first module for 4.6.8 for the community) with a visible/hidden field with keywords. With a static array, it's work perfect, but when I try to make the same with a recordset I receive a error.
Bild

I wish convert my variable "$keywords" into a array like this:
$liste = array("abeille","abricot","acheter","acheteur","zèbre","zéro");

I try to do so:
$liste = $keywords;

But I receive a error:
Warning : Invalid argument supplied for foreach() in c:\documents and settings\web1\my documents\www\ajax\autocomplete\options.php on line 21.

Line 21 are:
foreach ($liste as $element) {

What is wrong?

A lot of thx for your help and time.

Regards,
Dominique


My php code:

Code: Alles auswählen

<?php include '../Connections/connect.php';

mysql_select_db($database_connect, $connect);
$keywords = "SELECT artist_name FROM artists";

header('Content-Type: text/xml;charset=utf-8');
echo(utf8_encode("<?xml version='1.0' encoding='UTF-8' ?><options>"));
if (isset($_GET['debut'])) {
    $debut = utf8_decode($_GET['debut']);
} else {
    $debut = "";
}
$debut = strtolower($debut);

//$liste = $keywords;
$liste = array("abeille","abricot","acheter","voisin","voisinage","vouloir","voyage","voyageur","zèbre","zéro");

function generateOptions($debut,$liste) {
    $MAX_RETURN = 10;
    $i = 0;
    foreach ($liste as $element) {
        if ($i<$MAX_RETURN && substr($element, 0, strlen($debut))==$debut) {
            echo(utf8_encode("<option>".$element."</option>"));
            $i++;
        }
    }
}

generateOptions($debut,$liste);

echo("</options>");
?>

Verfasst: Do 27. Jul 2006, 15:42
von i-fekt
Please submit the whole module.

Verfasst: Do 27. Jul 2006, 15:48
von stese
it seems that your variable $keywords is not an array. is it possible, that the variable is a comma seperated value?

please show us the code where you fill the $keywords variable.

Verfasst: Do 27. Jul 2006, 17:25
von HerrB
It isn't:

Code: Alles auswählen

$keywords = "SELECT artist_name FROM artists";
In general: Is it the same database? Than you should use the DB_Contenido object.

Everything else about getting the values from the database is missing some imprtant parts:
Change this

Code: Alles auswählen

$keywords = "SELECT artist_name FROM artists";
to

Code: Alles auswählen

$sql = "SELECT artist_name FROM artists";
// There should be a query...
$result = mysql_query($sql, $connect);
// And the result should be stored in your variable
$liste = array();
while ($row = mysql_fetch_assoc($result)) {
   $liste[] = $row["artist_name"];
}
Note, that this can only work, if the included script specifies $connect as link to a database...

Also, as you are getting the results from the database, you could replace the function just by using the while construct and putting the code from the function into the while loop...

See http://www.php.net about a documentation and many, many examples...

Regards,
HerrB

Verfasst: Fr 28. Jul 2006, 12:18
von djavet
Thx!

Here it's a zip with the whole script:
http://www.metadelic.com/upload/autocomplete_mysql.zip

I've just uncomment in "option.php", line 23 the $list variable.
I've the xml file, doesn't work with my form.

It's very static and only experiment and this time not include in Contenido synthax. So excuse me. I'm not a php/ajax expert. I learn.

Regards, Dom

Verfasst: Mo 31. Jul 2006, 13:17
von HerrB
You said, the static version is running.

If I understand anything correctly, you'd like to get the database version running. Is the table in the same database as Contenido?

Regards,

HerrB

Verfasst: Mo 31. Jul 2006, 17:15
von djavet
Hi

Thx for your reply. No the table are not for this test the same as Contenido. This will come later. I've made a simple table to understand the process.

Any idea?

Regards, Dom

Verfasst: Di 1. Aug 2006, 09:49
von HerrB
Sure, move the table to the Contenido database and we will get you up and running in no time.

Regards,

HerrB

Verfasst: Di 1. Aug 2006, 10:20
von djavet
Hi!

I've move the table to contenido:
http://web92.ypsilon.ibone.ch/upload/autocomplete.zip

And the dynamic example:
http://web92.ypsilon.ibone.ch/cms/autocomplete/

But it doesn't work.
when I add the config file for the connection for my query, it stop working...

Regards, Dom

Verfasst: Di 1. Aug 2006, 10:22
von HerrB
I didn't say, that it will work from scratch - there is still coding to do. I will take a look in the evening.

Regards,
HerrB

Verfasst: Di 1. Aug 2006, 10:23
von djavet
Hi

I understand what you're saying. I'm still learning. Thx for your help.
I think it maybe a confusion with the variable name.

Regards, Dom

Verfasst: Sa 19. Aug 2006, 21:51
von djavet
HerrB hat geschrieben:I didn't say, that it will work from scratch - there is still coding to do. I will take a look in the evening.

Regards,
HerrB
Hi!

Do you have find something?

Regards, Dominique

Verfasst: So 20. Aug 2006, 22:48
von HerrB
Err, to tell the truth: no, sorry. I won't today, Monday, Tuesday, Wednesday perhaps. Pease send me a PM as reminder.

Regards,

HerrB