How to loop through ALL and JUST searched Items?


Playing around with a faceted search plugin (based on, I’ve got a problem with how to correctly reference to Items already filtered by the search (ideally facets should adapt to already existing parameters, ignoring not present values).

The approach of the original developers was to retrieve a list of Items’ ID and to add that to the facets query (not sure it’s the best way: if the Items number is very big, issues could raise; maybe a different approach, where a subquery is used, could be better in the long run… this triggers another question: is there an easy way to get/create a SQL string from the Advanced Search parameters?), by using

$allItems = get_db()->getTable('Item')->findAll();
foreach (loop('items') as $item) {
	$itemsArray[] = $item->id;

As a result, they would see all Items available in the database.

If I tried, instead,

foreach (loop('items') as $item) {
	$itemsArray[] = $item->id;

I would obtain only the Items shown in a particular paginated page. So, if my search would find 42 Items, but pagination is set to 10, I would only get 10 results.

Is there a way to get all and just the search resulting Items, without pagination to break them down in smaller chunks (so, in the aforementioned example, 42 Items)?


I’ve come up with a workaround:

$params = array('advanced' => $_GET['advanced']);
$facetsItems = get_records('item', $params, null);

It works, but I’m not completely satisfied with it, as it seems to me I’m reinventing the wheel: the list of Items should be already available, so I should be able to retrieve it somehow. Any idea for improvement on this?

Besides: any suggestion for the alternative method, i.e. create/retrieve the corresponding SQL Select statement to be used as a subselect?

You can get a “Select” object with the filters applied and turn that into SQL:

$select = get_db()->getTable('Item')->getSelectForFindBy($params);
$sql = (string) $select;

Very good, thanks John, it works like a charm.

This topic was automatically closed 250 days after the last reply. New replies are no longer allowed.