In many
cases, you'd like users accessing your tables to see filtered data
directly instead of forcing them to manually define filters. This is
specially applicable if your users are novice and feel uncomfortable with
the advanced filters provided by PHPMagic.
Note: Assuming that you have a good knowledge of HTML and how HTML forms work.
If not, you may wish first to take some HTML lessons. You may try the
excellent tutorials on HTMLGoodies.com
So, we'll try here to
highlight how to create a default filter that automatically applies when
users access a table. Let's first see how PHPMagic filters work. Below, you
can see the filters page where users define filters.
Empty
filters page as generated by PHPMagic edition.
You can note that PHPMagic
offers up to 12 filters divided into 3 groups -- each group containing up
to 4 conditions. Simply speaking, filters and grouping work in a manner
similar to this:
Show me all records
that satisfy (F1 and F2 and F3 and F4) and (F5 or F6 or F7 or F8) and (F9
or F10 or F11 or F12)
The above expression simply means
that we want to see all records that satisfy ALL of the first 4 conditions
and ANY of the next 4 conditions and ANY of the last 4 conditions. Of
course, users don't have to define all 12 conditions. Even if they define
one single condition, it will apply once they click the "Apply Filters"
button (the sunglasses button at the bottom of the filters page).
Let's define and apply some filters then view the source code of
the filters page to see the inner works of PHPMagic filters. Here are some
filters that we applied to a table:
We have applied some filters to our table. The
filters here mean "Show me all records where (author begins with the
letter 'A' and doesn't contain the letter 's') or (year_born is
after 1925)"
If we view the
HTML source of the above filters page (right-click your browser window,
and select "View Source"), we can get a very good idea about how filters
are defined. And we can can accordingly mimic the filter definitions to
create our own default filters. These are the important parts of the code
we'll see:
The
above code defines a form that posts data to the file called
"tablename_view.php", where tablename is the name of the table you want to
define filters for.
Next, we see the code that allows users to
select the field name used in the first condition in the filter:
Notice the name of the above <select>: FilterField[1] The selected option here is the third
one, which has a value authors.Author.
Next, we see the HTML code of the "comparison operator" for the
first condition:
<selectname='FilterOperator[1]' class='Option' style=''>
<optionvalue='' class=Option> </option>
<optionvalue='<=>' class=Option>Equal to</option>
<optionvalue='!=' class=Option>Not equal to</option>
<optionvalue='>' class=Option>Greater than</option>
<optionvalue='>=' class=Option>Greater than or equal to</option>
<optionvalue='<' class=Option>Less than</option>
<optionvalue='<=' class=Option>Less than or equal to</option>
<optionvalue='like' selectedclass='SelectedOption'>Like</option>
<optionvalue='not like' class=Option>Not like</option>
</select>
Again, the name of the above <select> is FilterOperator[1]. The selected value is like. The last part of the first condition is the
"comparison value". The HTML code for this part is:
As
of the second condition, we notice that every condition thereafter begins
with a <select> for choosing "And" or "Or". For example, the second
condition contains the following code:
It's obvious now that the filter contains several
conditions. Each condition is defined using 4 parts:
Name
Value
FilterAnd[i]
And, Or
FilterField[i]
table_name.field_name
FilterOperator[i]
<=>, !=, >, >=, <, <=, like, not like
FilterValue[i]
Any numeric, text, or wild-card
value
In the above table, [i] represents
the condition number. If you're defining the first condition, replace i
with 1. If you're defining the second condition, replace i with 2, and so
on. Notice that for the first condition, there is no need to define
FilterAnd[1].
Finally, we are ready now to create our own default
filters page. To do so, open your Windows NotePad (or any other text
editor), and paste the code below into it:
The
above code creates a form containing several hidden variables defining the
filters, and one button that submits the form:
When
users click that button, they see the table with all the predefined
filters applied to it. Notice in the above code that we didn't have to
define all the 12 conditions. We need only define the variables for the
conditions that we'll use.
Of course, you don't have to submit the
form using a submit button. You can use an image submit:
<inputtype="image" src="myimage.gif">
Or
you can use a normal hyperlink:
<ahref="#" onclick="myform.submit();">Show me filtered authors</a>