Open to all! Ask other shopowners for help.
LeeFoster
Contributor
Posts: 263 Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times
Post
by LeeFoster » Mon Aug 19, 2024 2:10 pm
I'm working on an info pages module to show on the index page and I would like to limit the number of pages returned. How would I do that using info_pages::getContainer?
I know I can use the below to get from a specific folder but not how I would limit it. Just adding the LIMIT 3 to the end doesn't work.
Code: Select all
$container = info_pages::getContainer(['pd.languages_id' => $_SESSION['languages_id'], 'p.pages_status' => '1', 'p.folder' => '3']);
ecartz
Core Team
Posts: 3084 Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times
Post
by ecartz » Mon Aug 19, 2024 2:52 pm
Not sure if Burt created a limit parameter. If not, try the PHP function array_slice after getting the pages.
LeeFoster
Contributor
Posts: 263 Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times
Post
by LeeFoster » Mon Aug 19, 2024 2:58 pm
ecartz wrote: ↑ Mon Aug 19, 2024 2:52 pm
Not sure if Burt created a limit parameter. If not, try the PHP function array_slice after getting the pages.
array_slice worked. Do you know if there is a parameter to order_by?
ecartz
Core Team
Posts: 3084 Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times
Post
by ecartz » Mon Aug 19, 2024 3:16 pm
I do not know, but you could sort between get_pages and array_slice.
burt
Core Team
Posts: 4551 Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
: Buy Me A Beverage
Has thanked: 252 times
Been thanked: 412 times
Post
by burt » Tue Aug 20, 2024 7:36 am
@LeeFoster
There is an example order by in the class file.
There is no limit by clause.
If the class file does not do everything you need, override it and change the new file to your needs.
Therefore, find the class file and learn from it!
I am not here to build for you.
I am here to build with you. Let's help each other.
LeeFoster
Contributor
Posts: 263 Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times
Post
by LeeFoster » Tue Aug 20, 2024 7:54 am
burt wrote: ↑ Tue Aug 20, 2024 7:36 am
If the class file does not do everything you need, override it and change the new file to your needs.
Therefore, find the class file and learn from it!
This is what I did in the end, was just hoping there was another way.
burt
Core Team
Posts: 4551 Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
: Buy Me A Beverage
Has thanked: 252 times
Been thanked: 412 times
Post
by burt » Wed Aug 21, 2024 8:05 am
Did you try (taken from the example and added LIMIT 3);
Code: Select all
$pages = info_pages::getContainer(['pd.languages_id' => '1', 'p.pages_status' => '1'], 'p.slug LIMIT 3');
Makes array of pages in english (1) where the page status is active (1), ordered by slug, limited to 3
I am not here to build for you.
I am here to build with you. Let's help each other.
LeeFoster
Contributor
Posts: 263 Joined: Sun Feb 28, 2021 9:41 pm
Phoenix Version: v1.0.8.20
Has thanked: 1 time
Been thanked: 5 times
Post
by LeeFoster » Wed Aug 21, 2024 9:00 am
burt wrote: ↑ Wed Aug 21, 2024 8:05 am
Did you try (taken from the example and added LIMIT 3);
Code: Select all
$pages = info_pages::getContainer(['pd.languages_id' => '1', 'p.pages_status' => '1'], 'p.slug LIMIT 3');
Makes array of pages in english (1) where the page status is active (1), ordered by slug, limited to 3
I have since you posted this and it unfortunately doesn't work. This is because the getContainer function doesn't have anyway to process it.
Code: Select all
public static function getContainer($container = []) {
$pages_query_raw = "select * from pages p left join pages_description pd on p.pages_id = pd.pages_id where 1=1 ";
if ( count($container) > 0 ) {
foreach ($container as $k => $v) {
$pages_query_raw .= "AND $k = '$v' ";
}
}
$pages_query_raw .= "order by p.sort_order";
return $GLOBALS['db']->fetch_all($pages_query_raw);
}
It would need to be amended to allow the processing of the element to allow this. It would need to look like the below.
Code: Select all
public static function getContainer($container = [], $element = 'p.sort_order') {
$pages_query_raw = "select * from pages p left join pages_description pd on p.pages_id = pd.pages_id where 1=1 ";
if ( count($container) > 0 ) {
foreach ($container as $k => $v) {
$pages_query_raw .= "AND $k = '$v' ";
}
}
if ( (!Text::is_empty($element ?? '')) ) {
$pages_query_raw .= "order by $element";
}
return $GLOBALS['db']->fetch_all($pages_query_raw);
}
This allows me to add the sort by and limit but if it is not used it will resort to the default sort of p.sort_orde.