How to list and search a database table using a collection #506
-
A client asked me to develop a simple database table with a search module. Is this something can be done by Pages? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yes, you can do that very easily. Pages can connect to a database table; it's smart enough to understand the table structure and offers filtering and search out of the box, either through the PHP API, or the build in REST API. To work with data Pages uses collections and specifically for database tables, there is a base database collection you can use. To set things up that all you need is to define a database collection. For this example, I will use the Joomla Rendering the users listCreate the file ----
collection:
model: database?table=users
state:
sort: name
--- <ul>
<? foreach(collection() as $user): ?>
<li><?= $user->name ?> (<?= $user->email ?>)</li>
<? endforeach ?>
</ul> You can now call this page using: Searching for usersTo be able to search you need to define the data properties searching is allowed on, in case of a database table, this means the database columns you wish to search. For example to allow to search on the ----
collection:
model: database?table=users
state:
sort: name
config:
search: [name, email]
--- Searching through PHPAnywhere in your code, you can now re-use the Searching on all defined properties
Searching on a specific propertyIt's also possible to search, on a specific property only, by prepending the search word with the property name separated by a colon
or
Searching through the URLYou can also use the URL to list a subset of the users by searching it. Searching on all defined properties
Searching on a specific propertyIt's also possible to search on a specific property only by prepending the search word with the property name separated by a colon
or
|
Beta Was this translation helpful? Give feedback.
Yes, you can do that very easily. Pages can connect to a database table; it's smart enough to understand the table structure and offers filtering and search out of the box, either through the PHP API, or the build in REST API.
To work with data Pages uses collections and specifically for database tables, there is a base database collection you can use. To set things up that all you need is to define a database collection.
For this example, I will use the Joomla
users
table, but it works with any database table, and generate a list of the users byname
, then show you how to search based onname
andemail
Rendering the users list
Create the file
/joomlatools-pages/pages/users.html.php
and a…