Page 1 of 1

Info Pages Index Module

Posted: Mon Aug 19, 2024 2:10 pm
by LeeFoster
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']);

Re: Info Pages Index Module

Posted: Mon Aug 19, 2024 2:52 pm
by ecartz
Not sure if Burt created a limit parameter. If not, try the PHP function array_slice after getting the pages.

Re: Info Pages Index Module

Posted: Mon Aug 19, 2024 2:58 pm
by LeeFoster
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?

Re: Info Pages Index Module

Posted: Mon Aug 19, 2024 3:16 pm
by ecartz
I do not know, but you could sort between get_pages and array_slice.

Re: Info Pages Index Module

Posted: Tue Aug 20, 2024 7:36 am
by burt
@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!

Re: Info Pages Index Module

Posted: Tue Aug 20, 2024 7:54 am
by LeeFoster
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.

Re: Info Pages Index Module

Posted: Wed Aug 21, 2024 8:05 am
by burt
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

Re: Info Pages Index Module

Posted: Wed Aug 21, 2024 9:00 am
by LeeFoster
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.