Autocomplete MySql PHP

Autocomplete MySql PHP

aungkoheinaungkohein Posts: 7Questions: 3Answers: 0

Hi!

Could you provide me with the syntax for loading autocomplete suggestion from MySQL table? Here is my example code and I could not understand how to convert it:

// The below is using local data variables. I do not want to hard code here but load from database.
var editor;

$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax: 'php/table.cars.php',
table: '#cargoes',
fields: [
{
"label": "Date:",
"name": "date",
"type": "datetime",
"format": "DD\/MM\/YY"
},
{
"label": "Status:",
"name": "status",
"type": "autoComplete",
"opts": {
"source":
[
"Open",
"Home",
"Parked",

                        ]
            }
        },

Answers

  • aungkoheinaungkohein Posts: 7Questions: 3Answers: 0

    Found the Solution!

    Follow this video: https://www.youtube.com/watch?v=UBs6wKVSCmU

    Step 1: Create a data.php

    <?php //connection $dbhost = 'localhost'; $dbname = 'example'; $dbuser = 'root'; $dbpass = ''; try{ $dbcon = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass); $dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $ex){ die($ex->getMessage()); } $stmt=$dbcon->prepare('SELECT example_name FROM example'); $stmt->execute(); $array = array(); while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { extract($row); $array[]= $example; } echo json_encode($array); ?>

    Step 2: Then change your table.example.js

    var editor;

    $(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
    ajax: 'php/table.example.php',
    table: '#example',
    fields: [
    {
    "label": "Status:",
    "name": "status",
    "type": "autoComplete",
    "opts": {
    "source": "autocomplete/data.php" //redirect to your PHP file
    }
    },

Sign In or Register to comment.