So I have several form elements which are currently acting as filters - I plan to use this as a basis for a search feature on my website.
I was thinking about linking a set of on change events to dyanmically build an AJAX query that selects fields from a set of tables but I'm not too sure how I can go about doing it.
On page load I have the following: http://jsfiddle.net/uVhzZ/. The pseudo SQL for this setup would be: SELECT * from ts_rooms WHERE capacity >= 50 (Any means no WHERE condition has been set)
However when the user starts to make changes, I would like this additions to be dynamically added to the SQL query - for instance if Lecture Style is set to Lab and capacity changes to 75 then pseudo SQL would change to SELECT * from ts_rooms WHERE capacity >= 75 AND lectureStyle="Lab";
How do I go about doing this?
First you gotta prepare your AJAX request, it can be done by creating an array with all selects you wanna use (you can look on ther .val() and if it has any value you add this value to that array using the select id as array key) then converting it to JSON.
PHP will receive this JSON string and convert it to array. Then you can look thru this array, and for each element you go building your where statement.
Then it's just a matter of taking query resultset and build another array from it, converting to JSON and responding to JS.
Put all your elements in a form, post it to your php search file. Created a sql query with the relevant constraints and return data in json and display it.
Related
So there is one specific DataTable I am working with which handles large amounts of data, so all the logic is in the PHP code at the back end. When sorting is toggled at the table, it sends an API call which returns the data in specific order, so each time you click to toggle sorting by a specific value or order, it sends that data through POST, and at the back end there is logic like following for all the columns
ORDER BY $colname $direction
The problem is that some of the data rendered in the table are not contained in a specific table in DB, but in a json, in a column called 'other'. Is there an easy way, or 1 line query that can modify ORDER BY command to work with a specific element in JSON?
can someone please explain to me how to approach this issue?
I have a table with 4000 records, a select option and a search field to filter out the table
however I'm trying to figure out the best way to set up pagination.
1st Approach:
Should I make one query against the database and populate the table with all records and then set up the pagination with javascript so I can be able to search for records that are not only shown on the current page?
2nd Approach
Set up the pagination with PHP, make a query via ajax for each page? however I'm afraid that this approach would not allow me to filter out the table if I need to search for a record that is not on the current page.
1st approach: definitely not, loading large number of results for nothing is not recommended.
2nd approach makes more sense. You can use the LIMIT statement inside your SELECT statement to decide which records you want. Ex. if you paginate on 10 elements perpage, you could do
SELECT * FROM Table LIMIT 10
then
SELECT * FROM Table LIMIT 10,10
and so on. Indexes start at 0.
Note that you don't even need Ajax to do that, your next button can specify the offset for the next load of the page. It's a choice based on your knowledge, the size of the rest of the page (minimize load time), ...
I have a query with Laravel
$query = Work::inRandomOrder()->where('orientation', $request->get('orientation')->paginate(11);
And I have a "load more" button who call 11 others works with each click.
But, I would like display my data in a random order. But with this query, he repeats to me several data. So in my list of works, I have 2 or 3 times the same and obviously, it's not good. Is there a way with RandomOrder () to avoid duplicates?
Thank you
I believe data is randomized each time the script is loaded, including each time loading dynamicaly 11 more data fields. One way to achieve this it to randomize, store the data in a user's session variable and then get next elements from it. However this might be very heavy.
I would go to a some easily predictable mathemtics, which will look 'random' to the end-user. For example you can get each N-th entry of a query like 1-st, 5-th, 9-th, 13-th ... if N=4 and store just the number N in the user's session.
One of the fields in my seller db has following data:
{"delivery area":"delivery amount"}
An example of this data in the db is as follows:
{"1":"0","2":"0","3":"0","4":"200","5":"1"}
Now I want to write a query that picks all sellers that deliver to a particular delivery area.
How can I do it using SQL? What do I specify in the WHERE clause?
I am working on Magento so an equivalent in addFieldToFilter will also help.
I just realized that this value is nothing but a json encoded value. All I had to do was to decode it upon reading and extract the values.
I have a table that contains a user_id, and an items field. The user_id is just an int with the user's id, and the items is an xml structured object in a 'text' field. I want to be able to see statistics about the player items. i.e. who has the most of some item, the average wealth of everyone, etc.
I currently have to loop through each row and then again create a SimpleXMLElement and loop thru that and filter given a specific criteria.
The structure is like this:
inventory
if I want to do a query to count all of the items with item id 332 for example, this query takes like 3-4 seconds. We expect there to be 50k+ rows(currently 28k), so if there is any other way I can speed this process up, it would be great.
what about using mysql like ?
for example
SELECT * FROM table WHERE inventory like '%<itemid>332</itemid>%';
Depending on how much you need to query this data, storing it as XML might not be the best approach; assuming that you've already decided that it is, many databases support some form of XPath queries which can be used to extract data out of XML fields. MySQL provides some support in the form of the ExtractValue function, which can be used to extract the criteria that you need in a more reliable way than simply using LIKE (e.g. in deefactorial's answer; what if there was more than one itemid in your XML?).
An example can be seen here on SO, in How to use XPATH in MySQL select?.