1

I appplied autoComplete function to search box but when i key in some value in the search box, it always gv me the same result.
I realized i did not loop through the database, that's why i keep getting the same result.
I changed my query so that i can get what i want but the result still the same.
here is my ajax for autocomplete, and i'm not sure is it the right way to do it? But the search function is working except it do not display all the data.

function autoComplete(){

        $('#keywords').autocomplete({
        source: 'autoComplete.php',
        minLength : 3,
        select: function(event, ui) {
            console.log(ui);
            $('#chosenEvent').append(ui.item.value + "\n"); 
        }
    });

Here is the php code

<?php
// include the file for the database connection
include_once("database_conn_getOffers.php");

function autoC($conn){

    $sql = "select eventTitle from te_events_special_offers eventTitle ORDER BY eventTitle";

    $rsOffer = mysqli_query($conn, $sql);
    //$offer = mysqli_fetch_all($rsOffer, MYSQLI_ASSOC);

    $titles = array();

    while($title = mysqli_fetch_assoc($rsOffer)){
        $titles[] = $title;
    }
     foreach ($titles as $title)

    return json_encode($title);
}
echo autoC($conn)

?>

Here is the link that i refer to click here

Thanks for your help!
So now, i changed the ajax method with the following code and it works but i still have no idea what wrong with my previous ajax code. I also modified the php code by remove the foreach and added implode method

$.ajax({
        method :"get",
        url :"autoComplete.php"
    })
    .done(function(data, status, jqxhr){
        var eventList;
        console.log(data);
        eventList = data.split(',');
        $("#keywords").autocomplete({
        minLength :2 ,
        source : eventList,
        select: function(event,ui){
            console.log(ui);
            }
        //end autocompoete  
        });
    });
Community
  • 1
  • 1
ccs
  • 151
  • 2
  • 15

1 Answers1

1

Please try first to have results to encode, then we'll used them. Run this code along no other one at the same time, and tell us what you get (I assumed that you have columns ID + TITLE, if not, correct the code before using). Also, you original query seems weird -> $sql = "SELECT eventTitle FROM te_events_special_offers eventTitle ORDER BY eventTitle"; /* check bold part of it */

Plus : you should really think about prepared statements and error_reporting

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

include"config.inc.php";

$mysqli = mysqli_connect("$host", "$user", "$mdp", "$db");

if (mysqli_connect_errno()) { echo "Error connecting : " . mysqli_connect_error($mysqli); }

$query = " SELECT idTitle, eventTitle FROM te_events_special_offers ORDER BY eventTitle "; /* check names used here and adapt to yours */
$stmt = $mysqli->prepare($query);

$results = $stmt->execute();
$stmt->bind_result($idTitle, $eventTitle);
$stmt->store_result();

  if ($stmt->num_rows > 0) {
  $events = array();
  $event = array();
   while($stmt->fetch()){
    echo"[ $idTitle -> $eventTitle ]<br />";

    $event["id"] = "$idTitle";
    $event["title"] = "$eventTitle";

    array_push($events, $event);
 }
}
else
{ echo"[ no data ]"; }

print_r($events);
echo json_encode($events);

?>
OldPadawan
  • 1,247
  • 3
  • 16
  • 25