PHP multiple fields search with multiple operators - php

I am looking best solution in PHP for a search form which has multiple fields and person can also choose operation on that field. Here is how it will work for example. Also allow me to set what operations are possible on field without hard-coding. There will be many fields on page i am explaining on few on them.
Scenario examples :
Lets field "age" there can be three scenario. age can be greater than
or equal to or less than given field. So on page for a field person
will be able to select operation type from drop-down
"equal,greater,lesser" and then specify value.
Similarly for subject it can have following scenarios. subject equals
selected, or in multiple selected subjects, or not in selected
subjects.
I don't wanna hard-code solution for each field, i want something that's more flexible with which i can add or remove fields later without changing major part of code.

Was able to resolve it with collaborated code between PHP and Javascript. Created array in PHP with name of field, then what type of operations it support and other optined needed.
Then in jQuery hide display and restrict field options based on options that i output from PHP array.

Related

Yii Cgridview Dynamic Columns

I am looking at customising the yii cgridview. I want to be able to allow users to select which columns they wish to see. Currently I am selecting the exact columns which will be displayed.
I have had a look for information on this but do not seem to be getting very far, maybe I am not looking for the correct terms or their is a specific term for this. Ideally users can click a button and tick the boxes which will be seen. I have seen this implemented on x2crm
http://demo.x2engine.com/index.php/accounts/index
I also like the ability to move the columns around ie resort the order of the columns and the ability to resize the columns when more are added. I realise someone isn't going to come along and do this for me, but certainly if someone could provide me any information or similar requests, it would be greatly appreciated.
After a long gruelling search I have found something that may in fact be the solution to both of my requests. An extension for Yii exists that allow for you to chooser the columns you wish to display with a simple tick box selection, as well as allowing for reordering of columns.
http://ecolumns.demopage.ru/index.php
The link above takes you to the demo page for the extension and the link below is the link to the extension download page.
http://www.yiiframework.com/extension/ecolumns/
This is by far the easiest way to implement this functionality on your web app.
Start by reading the docs for CGridView,
The constructor for it takes in an array specifying which columns to display (and whether to allow sort on them, etc) so allowing users to select which columns they want to see is almost trivial:
Display a form with checkboxes, the values of which are the names of the columns. When the user submits the form, loop over the checkboxes and add each of the present fields to the array that is passed to CGridView.
It is a little more complicated if you want to have specific settings for the column (i.e. a specific column header, or formatting) however not too much - in that case you just define an array holding the settings for it, and add that array to the total array you submit to CGridView.
Allowing drag and drop of the columns is a far more challenging enterprise, and may not actually be possible without a custom implementation - this is because CGridView is inherently just a table i.e. you could drag and drop rows easily (as they are whole items), but dragging a dropping a row is in reality dragging and dropping a lot of separate cells. However, there are jQuery examples that could get you started - and it wouldnt be a huge issue to implement a CGridView that uses divs instead of a table, and uses cells inside columns, rather than cells inside rows.
I hope that helps a little.

How to catch different elements?

I'm using smarty for work with html and faced one problems: I have table of different elements (few select and many input). I need to check the values of all elements in the table. With input no problem ($('input[type="text"]')), but how to check in one time select and input values.the problem is that the smart does not provide any additional options for {html_options}. May be there is some AND structure for jQuery selectors?
I see two possible routes here:
Extend your selector with add, e.g.
$('input[type="text"]').add('select').doSomething()
Use a common class for all input and select elements, e.g.
$('.my_input_class').doSomething()
Of course this assumes that you are using common properties/methods on your selection afterwards. For instance, you'd be able to call val() to retrieve the values.
If you want to add the same functionality to two selectors you can this approach..
$('input[type=text] , select').addClass('classname')
You can use a comma to add the same functionality to multiple selectors..

How to generate FORM that reflects MySQL table?

I want to generate HTML form based on MySQL table field. The generated input should reflect the type of the MySQL column, e.g. VARCHAR = text, TEXT = textarea, ENUM = checkbox, SET = radio, etc.
I think this kind of Lib is not available
But ya you can do it by making one table in your database that conatains details about form and retrive it and make HTML code dynamically
This is a two step problem.
First you need to analyse the database table. You can get column names, type, etc. using SHOW COLUMNS.
You need to generate the form based on the retrieved information.
Generating form can be as simple as iterating the array received from SHOW COLUMNS query and building HTML along the iteration, or using existing form generation libraries, e.g. Zend_Form or Dora.
Depending on your needs, there is existing software for reflecting and manipulating the whole database, e.g. phpMyAdmin. Furthermore, some frameworks provide means of reflecting the whole model (i.e. database table) in a form for CRUD operations, e.g. Symfony Forms.

PHP To Add Multiple Values In One Entry Field

I am creating a very basic photo uploading site that will allow users to:
1) select a file to upload
2) choose an category from a drop down
3) tag the photo with names
Users will then be able to search using their name to find pictures.
Since there will be situations where the uploading user will want to tag multiple people who appear in a single photo, is there a simple way to allow them to type names separated by commas and then have PHP create unique entries in the database? If not, what's the best procedure for having multiple text fields to accomplish this?
Thanks
chance
If I understand you, there are a couple of ways.
First, you can, as you've said, have a single text field, comma separated. In this case, the PHP after the form is submitted can simply explode the string by ",".
Alternatively, you can have an array of input items using e.g. "inputName[]" as the input name. Once this is submitted, PHP will create it as an array of items which can be looped through.
Once that's done, you do whatever you need to with the data.
There is a php function called explode() that you can use to turn a comma delimted string into an array. For example, if the people to be tagged are separated by a comma you can use $people = explode(',',$names); Where $names is the string that the use typed. Then you will have an array where each item is a tagged person that you can iterate over and do what you need to with it.

How to use PHP to generate HTML form to let user do complex searches on a database?

Let's imagine I have a database and I want to programatically allow the user to build queries. I am coding in PHP and will output an HTML form to let the user specify his query.
I know the names of all fields, their types (int/string/date, etc) and maybe also min/max. How can I best present this visually to the user and let him select his queries?
At the moment, my logic is only AND, because I create a table with an entry for each DB field.
If the original input was free-form text (edit or memo), then I can put <field name><operation><text input field> where operation is a drop down list of
contains/doesn't contain/begins./ends/etc. So he can specify name begins "Mc", etc
If the original input was from a drop down list, the operation is just equal/no equal and he gets to choose again from that drop down list.
For integrs operation is = != > >= =< <
And so on, BUT while I can generate a table row for each and put a checkbox by it, I can't let user specify complex seraches like (name beings"Mc" and salary>50000) || (name !=smith && title!="CEO))
I hope that I explained that understandably. What I guess I am preferably looking for is a FOSS PHP compoinent to help me. Failing that, some advice .... Thanks
I use this to present my tables
http://datatables.net/
You can build the queries however you would like with JS, and MAKE SURE YOU VALIDATE THE DATA SERVER-SIDE
Note: There's a bit of a learning curve here if you're new to jQuery and Json, but IMO it's worth it.

Categories