We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Auto Complete

Hi, How will I implement autocomplete in a text field using php Phalcon. The data source for this auto complete will be from a database table. Regards, Devan

Phalcon is php, auto complete is js.



1.1k

Hi, What I am trying to ask is. Is there any better way to write the below code in Phalcon MVC framework. I mean php script.

    • javascript
    • <script>
    • $(function() {
    • $( "#skills" ).autocomplete({
    • source: 'search.php'
    • });
    • });
    • </script>
    • search.php
    • <?php
    • $dbHost = 'localhost';
    • $dbUsername = 'root';
    • $dbPassword = '';
    • $dbName = 'codexworld';
    • //connect with the database
    • $db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
    • //get search term
    • $searchTerm = $_GET['term'];
    • //get matched data from skills table
    • $query = $db->query("SELECT * FROM skills WHERE skill LIKE '%".$searchTerm."%' ORDER BY skill ASC");
    • while ($row = $query->fetch_assoc()) {
    • $data[] = $row['skill'];
    • }
    • //return json data
    • echo json_encode($data);
    • ?>

Thanks

edited Jun '16

Maybe check documentation first ? Phalcon is full-stack framework for making whole apllication. Doing just auto complete on it is waste of resources.

There is documentation: https://docs.phalcon.io/en/latest/index.html

First he need to check documentation and learn what phalcon can do. He didn't even use it now.

Hello

You can take a look my project, first you need add a tag input like this https://github.com/phanbook/phanbook/blob/master/core/modules/backend/views/posts/item.volt#L125, then you can see a file js to understand it https://github.com/phanbook/phanbook/blob/master/core/modules/backend/assets/js/tags-suggestion.js#L34

edited Jun '16

First this is a really poor format for posting code. use tripple tilda to open code tags. You need to have a space before and after the tildas

    ```javascript
        // code goes here
Second if you are using raw PHP mysqlI why are you using phalcon? If you are going to be using raw php. Use PDO its mucho better. 

Side Bar... Sanitize your [inputs](https://xkcd.com/327/)

'; DROP TABLE skills;

FInally if you are using phalcon it woudl look something liek this.

```php

class ThingController extends BaseController
{
  public function autocompleteAction($input = null){
      $skills = Skills::find(array(
          'conditions' => 'skill LIKE %:input:%', 
          'bind' => array('input' => $input),
          'order' => array('skill ASC')
      ));
      if ($skills){
      //    this code assumes that you have the ajax json_encoding handled elsewhere. 
          return $skills;
      }
  }
}

You shoudl really go and look at the documenation or tutorials before you post questions. You asked if their si a better way to write your function in the framework, but then you didn't do anything in phalcon. you search.php is a flat php file...

I suggest you got look up MVC frameworks.

then download the demo apps and isntall them. and then dig through the code and you can learn it really fast.

Also i have no idea how your autocomplete library is posting the date to its "source" so i can't speak on how you would catch the data... does it PUT? POST? GET?

Hi, What I am trying to ask is. Is there any better way to write the below code in Phalcon MVC framework. I mean php script.

    • javascript
    • <script>
    • $(function() {
    • $( "#skills" ).autocomplete({
    • source: 'search.php'
    • });
    • });
    • </script>
    • search.php
    • <?php
    • $dbHost = 'localhost';
    • $dbUsername = 'root';
    • $dbPassword = '';
    • $dbName = 'codexworld';
    • //connect with the database
    • $db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
    • //get search term
    • $searchTerm = $_GET['term'];
    • //get matched data from skills table
    • $query = $db->query("SELECT * FROM skills WHERE skill LIKE '%".$searchTerm."%' ORDER BY skill ASC");
    • while ($row = $query->fetch_assoc()) {
    • $data[] = $row['skill'];
    • }
    • //return json data
    • echo json_encode($data);
    • ?>

Thanks