Ajax live search with data from a database - php

I've seen some (well-explained) examples where the available results of a live search are found in an xml file. But only a few (badly-explained) ones where the results come from a database. I'm interested in the second case.
One solution could be to prefetch the database data and put them in an xml object and the rest could work the same way. And this is something I can easily implement.
What I'm looking for is for another way, where the user types a letter (e.g. 'c') and the suggestions are generated on the spot (e.g. 'carrot', 'cabbage', 'celery', 'citrus'). Would this be achieved with a call to the database like
"SELECT name FROM vegetables WHERE name LIKE c%"?
And what if the user then presses another letter? (e.g. 'ca', so the recommended results should be 'carrot', 'cabbage'). Every key pressed should initiate a new SELECT? For some reason it doesn't sound like a good solution.
Is there a way to achieve this second method?

You can achieve this by sending ajax requests on every key up function in jquery. In response send back the results in json format so as to display them in a unordered list below the text field. On clicking of the li of this fill the textbox with the value in the text field.

Related

How to autoload data from a database in laravel PHP

I have a question on how to achieve something from a development standpoint in Laravel. I am looking for ways to use autoload to return data from a database by just supplying a few character entry. For example, if I need to search for the word 'Johnson' which is already stored in a table in the database, supplying the word: 'Joh' in the search bar should bring up Johnson and other names like Johnson. I am very new to PHP and laravel, does anyone have any suggestions?
You are going to have to use Ajax and php to do this.
Basically the logistics will be as such:
a key is typed into an input which will load a javascript function that does a post to a php file. That php file will do a query for "Joh%". All records will come back matching Joh%. Print them out with php and it will be delivered back to the javascript function which called the php file in the first place. Now that javascript function will have to manage adding and subtracting entries from the dropdown on the input box you are typing in.
The question your asking is basically a lot of code and you should take it step by step. First start by getting a php query to work where you get records matching "Jon%", then continue on to another step.

Filter Table format view - Drupal 7

I'm looking for some help about "Custom View". I looked throw the internet but can't find it (maybe cause of my bad key words).
I created a custom view with a Table format. The goal is to display content (based on a content type) in a table.
I already have my content showing, I can reorganize rows by client/sector.. by clicking on the column header but now I'd like to :
Filter result depending on the string in an input textfield
and
Filter result using a dropdown menu
I guess It's client side, but I'm a beginner in drupal so it's a bit hard to find out.
Here is what I'd like :
http://hpics.li/175e64e
For the select filter, you should try using exposed filters in your view. In the filter section, add filter on the fields and expose them. If these fields are taxonomy reference fields it should work right away. Otherwise it depends : with entity reference I think Better Exposed Filters can be usefull.
With plain text fields it will be more difficult to get what you want (personnaly I give up on exposed filters when it becomes to complicated), but still possible with this approach and a bit of client side work.
The general idea is to create JSON view wich gets all differents values for a text field across the nodes, using Views Data Source (or get all nodes with fields values then fetch unique values for each fields in javascript).
On client side, on the page load make an ajax call to this view to get an array of all possible values, then build your select list using this array, and then do a client side filtering (using for example the excellent Isotope).
But in my opinion you need to take side : all with views and exposed filters (server side, can be hard and frustrating...) or all in JS (client side), mixing the two should result in a big mess...
For the plain text search box I would choose to work client side, Views won't be of any help I'm afraid.
You can also find good javascript plugins for table sorting / filtering like Datatables.
Good luck.

How do I implement an Angular-like filter function in php?

I have a page that is full of tables (generated by a foreach loop) and every row displays, among other things, a month.
Now I want to give the user the ability to choose a month and have only the table rows for that current month displayed (or no table at all if that month is missing from the table). A bit like the filtering function in Angular JS.
I'm still a bit new to php and the only solution I can up with is to handle the whole thing somewhat clumsily with modifying CSS classes and having the superfluous rows hidden by CSS.
What would be a more efficient way to do this?
Your problem is related with the filter alternate of angular into php.
As you know php is server side. So, if you want to get the filter after a reload or by ajax method, you need a $_GET variable.
Use a different query string in your url for each click of each button. Then retrieve the GET variable and use it to get data from database for relevant category.

Processing json output as array

I've created a search while typing method using this demo:
https://codeforgeek.com/2014/09/ajax-search-box-php-mysql/
But now i'm stuck with processing the results.
I want to receive back a list of data (that works) but also per dataline an id which should not be visible, but
which i can use as post data in the submit.
I probably have to change the json array and put the id in it, but i then i don't know how to seperate the data in the post form and use the id as hidden id.
To clearify my example: I have a table with strings (can be duplicate) and unique number id's.
I want the list to contain the strings and i want to submit with just the id.
I also found a lot of simular projects all looking a bit different, i especially like the looks of this one, creating a hovering results box and not changing anything to my form.
use json_decode on result
json_decode($json) will give you object in return
json_decode($json,true) vill give you array in return

Yet Another world city auto complete field question

I know that this question has maybe been asked several times but I have search several days without getting any satisfying answer.
Some sites, like eventful.com, etc. have a autosuggest city field with cities from around the world (even small cities in small countries from Tunisia).
I'm wondering how they achieve it. I've a text list of country + city from around the world but it's 250Mo so I guess the data is not contained in any javascript. Even a database call will be too ressource consuming I guess
Do you have an idea on how they achieve that?
They are using AJAX to send a shorten list of possible matches when you've typed the first 2-3 letters of the city you want.
AJAX queries the server, the server searches the database (I don't see how 250Mo is expensive), and then returns the results to the browser, which then displays it to the user (via JavaScript).
If you want to save the cost of going to the database all the time cache this data as a flat file in JSON format and load it via an AJAX request. Update the file from the db whenever it changes or daily or something like that. Populate your dropdown from the JSON data. You can loop through the array and add/remove suggestions as the user types.

Categories