1084

Pagination query

Comments for “Pagination query”
 

Posted by [email protected] on Sunday 23rd January 2022 at 16:04 GMT

If I create a module using Trongate DA and access pagination options a 404 will appear if there are not enough records.

eg if you change the "records per page" 10 to 20.

How do we code around that please? Been stuck on that for a while.

Thank you

Darron

[email protected]

User Level: Guest

Date Joined: 23/01/2022

Posted by djnordeen on Sunday 23rd January 2022 at 18:26 GMT

Hey Darron,
Need more information.
What is the url you are trying to access when you get the error?

Dan
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by [email protected] on Monday 24th January 2022 at 17:47 GMT

Hi Dan

I have created a simple test module with fields lastname, firstname, company name

I have added three records. On the manage page there is pagination and you can choose to view 10, 20, 50, 100 records.

If I click any of those 404 will appear.

http://localhost:8080/page_test/page_tests/manage

Thanks

D

[email protected]

User Level: Guest

Date Joined: 23/01/2022

Posted by djnordeen on Monday 24th January 2022 at 19:17 GMT

Darron,
Did you copy all the functions ?
$pagination_data['limit'] = $this->_get_limit();

$data['rows'] = $this->_reduce_rows($all_rows);
$data['selected_per_page'] = $this->_get_selected_per_page();
$data['per_page_options'] = $this->per_page_options;
// This is set at the top of the controller file:
private $per_page_options = array(10, 20, 50, 100);
Need to add these function to the Controller file
function _get_limit() {
if (isset($_SESSION['selected_per_page'])) {
$limit = $this->per_page_options[$_SESSION['selected_per_page']];
} else {
$limit = $this->default_limit;
}
return $limit;
}

function _get_selected_per_page() {
if (!isset($_SESSION['selected_per_page'])) {
$selected_per_page = $this->per_page_options[1];
} else {
$selected_per_page = $_SESSION['selected_per_page'];
}

return $selected_per_page;
}

function _reduce_rows($all_rows) {
$rows = [];
$start_index = $this->_get_offset();
$limit = $this->_get_limit();
$end_index = $start_index + $limit;
$count = -1;
foreach ($all_rows as $row) {
$count++;
if (($count>=$start_index) && ($count
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by [email protected] on Monday 24th January 2022 at 20:41 GMT

Hi Dan

Everything is present here in the code. This is all generated by the Desktop App.

I checked everything from your reply in the controller file., the functions are here and the calls to them are in the manage function.

Thanks

D

                            

[email protected]

User Level: Guest

Date Joined: 23/01/2022

Posted by djnordeen on Tuesday 25th January 2022 at 00:32 GMT

It seems like the OnClick is where the error occurs, in the view file.
What I would try is to rename the module.
Then use the desktop app to add a new module to your app.
Add some records and then try the pagination.
See what happens.

The only other thing I can think of is to change the License version and update your app

Dan
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by DaFa on Friday 15th July 2022 at 00:54 GMT

Hi Darron,

I think Dan is correct in suggesting to rename your old module and create a new one, making sure you are on the current version, but it's not the Onclick you need to look at, it's the onchange
<div>Records Per Page: <select name="per_page" onchange="setPerPage()">
<option value="3" selected>100</option>
<option value="0">10</option>
<option value="1">20</option>
<option value="2">50</option>
</select></div>

which calls setPerPage() function in admin.js
function setPerPage() {
    var perPageSelector = document.querySelector('#results-tbl select');
    var lastSegment = window.location.pathname.split('/').pop();
    var selectedIndex = perPageSelector.value;
    var targetUrl = window.location.protocol + '//' + window.location.hostname + window.location.pathname;
    targetUrl = targetUrl.replace('/manage/', '/set_per_page/' + selectedIndex + '/');
    targetUrl = targetUrl.replace('/manage', '/set_per_page/' + selectedIndex + '/');
    window.location.href = targetUrl;
}


I just created a a module called page_tests with the same 3 input fields, added 3 records and pagination is working fine on all 10, 20, 50 & 100 settings.

Here is the manage.php view file:
<h1><?= $headline ?></h1>
<?php
flashdata();
echo '<p>'.anchor('page_tests/create', 'Create New Page Test Record', array("class" => "button")).'</p>'; 
echo Pagination::display($pagination_data);
if (count($rows)>0) { ?>
    <table id="results-tbl">
        <thead>
            <tr>
                <th colspan="4">
                    <div>
                        <div><?php
                        echo form_open('page_tests/manage/1/', array("method" => "get"));
                        echo form_input('searchphrase', '', array("placeholder" => "Search records..."));
                        echo form_submit('submit', 'Search', array("class" => "alt"));
                        echo form_close();
                        ?></div>
                        <div>Records Per Page: <?php
                        $dropdown_attr['onchange'] = 'setPerPage()';
                        echo form_dropdown('per_page', $per_page_options, $selected_per_page, $dropdown_attr); 
                        ?></div>

                    </div>                    
                </th>
            </tr>
            <tr>
                <th>Last Name</th>
                <th>First Name</th>
                <th>Company Name</th>
                <th style="width: 20px;">Action</th>            
            </tr>
        </thead>
        <tbody>
            <?php 
            $attr['class'] = 'button alt';
            foreach($rows as $row) { ?>
            <tr>
                <td><?= $row->last_name ?></td>
                <td><?= $row->first_name ?></td>
                <td><?= $row->company_name ?></td>
                <td><?= anchor('page_tests/show/'.$row->id, 'View', $attr) ?></td>        
            </tr>
            <?php
            }
            ?>
        </tbody>
    </table>
<?php 
    if(count($rows)>9) {
        unset($pagination_data['include_showing_statement']);
        echo Pagination::display($pagination_data);
    }
}
?>


And here is the Page_test.php controller that was generated with the Desktop app:
<?php
class Page_tests extends Trongate {

    private $default_limit = 20;
    private $per_page_options = array(10, 20, 50, 100);    

    function create() {
        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();

        $update_id = segment(3);
        $submit = post('submit');

        if (($submit == '') && (is_numeric($update_id))) {
            $data = $this->_get_data_from_db($update_id);
        } else {
            $data = $this->_get_data_from_post();
        }

        if (is_numeric($update_id)) {
            $data['headline'] = 'Update Page Test Record';
            $data['cancel_url'] = BASE_URL.'page_tests/show/'.$update_id;
        } else {
            $data['headline'] = 'Create New Page Test Record';
            $data['cancel_url'] = BASE_URL.'page_tests/manage';
        }

        $data['form_location'] = BASE_URL.'page_tests/submit/'.$update_id;
        $data['view_file'] = 'create';
        $this->template('admin', $data);
    }

    function manage() {
        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();

        if (segment(4) !== '') {
            $data['headline'] = 'Search Results';
            $searchphrase = trim($_GET['searchphrase']);
            $params['last_name'] = '%'.$searchphrase.'%';
            $params['first_name'] = '%'.$searchphrase.'%';
            $params['company_name'] = '%'.$searchphrase.'%';
            $sql = 'select * from page_tests
            WHERE last_name LIKE :last_name
            OR first_name LIKE :first_name
            OR company_name LIKE :company_name
            ORDER BY id';
            $all_rows = $this->model->query_bind($sql, $params, 'object');
        } else {
            $data['headline'] = 'Manage Page Tests';
            $all_rows = $this->model->get('id');
        }

        $pagination_data['total_rows'] = count($all_rows);
        $pagination_data['page_num_segment'] = 3;
        $pagination_data['limit'] = $this->_get_limit();
        $pagination_data['pagination_root'] = 'page_tests/manage';
        $pagination_data['record_name_plural'] = 'page tests';
        $pagination_data['include_showing_statement'] = true;
        $data['pagination_data'] = $pagination_data;

        $data['rows'] = $this->_reduce_rows($all_rows);
        $data['selected_per_page'] = $this->_get_selected_per_page();
        $data['per_page_options'] = $this->per_page_options;
        $data['view_module'] = 'page_tests';
        $data['view_file'] = 'manage';
        $this->template('admin', $data);
    }

    function show() {
        $this->module('trongate_security');
        $token = $this->trongate_security->_make_sure_allowed();
        $update_id = segment(3);

        if ((!is_numeric($update_id)) && ($update_id != '')) {
            redirect('page_tests/manage');
        }

        $data = $this->_get_data_from_db($update_id);
        $data['token'] = $token;

        if ($data == false) {
            redirect('page_tests/manage');
        } else {
            $data['update_id'] = $update_id;
            $data['headline'] = 'Page Test Information';
            $data['view_file'] = 'show';
            $this->template('admin', $data);
        }
    }
    
    function _reduce_rows($all_rows) {
        $rows = [];
        $start_index = $this->_get_offset();
        $limit = $this->_get_limit();
        $end_index = $start_index + $limit;

        $count = -1;
        foreach ($all_rows as $row) {
            $count++;
            if (($count>=$start_index) && ($count<$end_index)) {
                $rows[] = $row;
            }
        }

        return $rows;
    }

    function submit() {
        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();

        $submit = post('submit', true);

        if ($submit == 'Submit') {

            $this->validation_helper->set_rules('last_name', 'Last Name', 'required|min_length[2]|max_length[255]');
            $this->validation_helper->set_rules('first_name', 'First Name', 'required|min_length[2]|max_length[255]');
            $this->validation_helper->set_rules('company_name', 'Company Name', 'required|min_length[2]|max_length[255]');

            $result = $this->validation_helper->run();

            if ($result == true) {

                $update_id = segment(3);
                $data = $this->_get_data_from_post();

                if (is_numeric($update_id)) {
                    //update an existing record
                    $this->model->update($update_id, $data, 'page_tests');
                    $flash_msg = 'The record was successfully updated';
                } else {
                    //insert the new record
                    $update_id = $this->model->insert($data, 'page_tests');
                    $flash_msg = 'The record was successfully created';
                }

                set_flashdata($flash_msg);
                redirect('page_tests/show/'.$update_id);

            } else {
                //form submission error
                $this->create();
            }

        }

    }

    function submit_delete() {
        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();

        $submit = post('submit');
        $params['update_id'] = segment(3);

        if (($submit == 'Yes - Delete Now') && (is_numeric($params['update_id']))) {
            //delete all of the comments associated with this record
            $sql = 'delete from trongate_comments where target_table = :module and update_id = :update_id';
            $params['module'] = 'page_tests';
            $this->model->query_bind($sql, $params);

            //delete the record
            $this->model->delete($params['update_id'], 'page_tests');

            //set the flashdata
            $flash_msg = 'The record was successfully deleted';
            set_flashdata($flash_msg);

            //redirect to the manage page
            redirect('page_tests/manage');
        }
    }

    function _get_limit() {
        if (isset($_SESSION['selected_per_page'])) {
            $limit = $this->per_page_options[$_SESSION['selected_per_page']];
        } else {
            $limit = $this->default_limit;
        }

        return $limit;
    }

    function _get_offset() {
        $page_num = segment(3);

        if (!is_numeric($page_num)) {
            $page_num = 0;
        }

        if ($page_num>1) {
            $offset = ($page_num-1)*$this->_get_limit();
        } else {
            $offset = 0;
        }

        return $offset;
    }

    function _get_selected_per_page() {
        if (!isset($_SESSION['selected_per_page'])) {
            $selected_per_page = $this->per_page_options[1];
        } else {
            $selected_per_page = $_SESSION['selected_per_page'];
        }

        return $selected_per_page;
    }

    function set_per_page($selected_index) {
        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();

        if (!is_numeric($selected_index)) {
            $selected_index = $this->per_page_options[1];
        }

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

    function _get_data_from_db($update_id) {
        $record_obj = $this->model->get_where($update_id, 'page_tests');

        if ($record_obj == false) {
            $this->template('error_404');
            die();
        } else {
            $data = (array) $record_obj;
            return $data;        
        }
    }

    function _get_data_from_post() {
        $data['last_name'] = post('last_name', true);
        $data['first_name'] = post('first_name', true);
        $data['company_name'] = post('company_name', true);        
        return $data;
    }

}

This comment was edited by DaFa on Friday 15th July 2022 at 01:10 GMT

Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

×