1084

set_per_page error when selecting 10 per page

Comments for “set_per_page error when selecting 10 per page”
 

Posted by mjim on Friday 19th January 2024 at 17:33 GMT

I created a new app today and ran into this error when setting Records Per Page to 10 under Manage Pages.

Uncaught TypeError: Trongate_pages::set_per_page(): Argument #1 ($selected_index) must be of type int, string given


The value of that option is zero but it is empty when sent to the set_per_page function.
Level One Member

mjim

User Level: Level One Member

Date Joined: 6/01/2022

Posted by DaFa on Monday 22nd January 2024 at 02:44 GMT

Hi mjim,

This is a simple fix to the method 'set_per_page' in "Trongate_pages.php'

The type hinting was causing the issue when we had an index of 0. Also, the check for:
if (!is_numeric($selected_index)) {
        $selected_index = $this->per_page_options[1];
    }
is not correct as it would set the value of 20 to the index.

The solution is to change the method to this:
/**
     * Set the number of items per page based on the selected index.
     *
     * @return void
     */
    function set_per_page($selected_index): void {
        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();

        $_SESSION['selected_per_page'] = (int)$selected_index;
        redirect('trongate_pages/manage');
    }


Happy coding!
Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by mjim on Monday 22nd January 2024 at 21:10 GMT

Hi Simon

Thank you for this update!

I still ran into a few issues and realized that $_SESSION['selected_per_page'] needs to be a number from the options array, not the index.

It's working for me now. Here are the two small changes I made:

/**
     * Set the number of items per page based on the selected index.
     *
     * @return void
     */
    function set_per_page($selected_index): void {
        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();
        $_SESSION['selected_per_page'] = $this->per_page_options[(int)$selected_index];
        redirect('trongate_pages/manage');
    }


To fetch the number of records per page it requires the index for those options:
/**
     * Get the number of records selected per page.
     *
     * @return int The number of records selected per page.
     */
    function _get_selected_per_page(): int {
        $selected_per_page = (isset($_SESSION['selected_per_page'])) ? array_search($_SESSION['selected_per_page'], $this->per_page_options) : 1;
        return $selected_per_page;
    }
Level One Member

mjim

User Level: Level One Member

Date Joined: 6/01/2022

Posted by mjim on Tuesday 23rd January 2024 at 19:31 GMT

Hey Simon

I added the fix you posted in Github to my Core.php and everything is working fine now without making any changes to Trongate_pages.php.

Thank you!
Jim
Level One Member

mjim

User Level: Level One Member

Date Joined: 6/01/2022

×