1046

I need some help with pagination please

Comments for “I need some help with pagination please”
 

Posted by Dom on Monday 1st April 2024 at 16:15 GMT

Hi Folks

I'm trying to display data dynamically for tables, which may or may not have modules.

The view in which the table data is being displayed is as follows

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vtl_Generator_ShowData</title>
</head>
<body>
<h2 class="text-center">Vtl Data Generator: Show Data</h2>
<div class="flex">
    <?php echo anchor('vtl_gen/index', 'Back', array("class" => "button")); ?>
</div>
<div>
    <?php echo Pagination::display($pagination_data); ?>
</div>


<?php
// Check if rows data exists and has rows
if (!empty($rows)) {
    ?>
    <table>
        <thead>
        <tr>
            <?php foreach (array_keys((array)$data['rows'][0]) as $header): ?>
                <th><?= $header ?></th>
            <?php endforeach; ?>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($data['rows'] as $row): ?>
            <tr>
                <?php foreach ($row as $value): ?>
                    <td><?= $value ?></td>
                <?php endforeach; ?>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>


<?php
    if(count($rows)>9) {
        unset($pagination_data['include_showing_statement']);
        echo Pagination::display($pagination_data);
    }
} else {
    echo "There is no data in the table that you selected.";
}
?>

</body>
</html>


The function that populates the view is this

public function showData(): void
    {

        // Extract the selected table from the query parameters

        //this is currently throwing an error with pagination
         $selectedTable = $_GET['selectedTable'];

        $this->module('trongate_security');
        $this->trongate_security->_make_sure_allowed();
        $rows = $this->model->get(target_tbl:  $selectedTable);


        $pagination_data['total_rows'] = count($rows);
        $pagination_data['page_num_segment'] = 3;
        $pagination_data['limit'] = $this->_get_limit();
        $pagination_data['pagination_root'] = 'vtl_gen/showData';
        $pagination_data['record_name_plural'] =  $selectedTable;
        $pagination_data['include_showing_statement'] = true;



        $data['rows'] = $this -> _reduce_rows($rows);
        $data['pagination_data'] = $pagination_data;
        $data['selected_per_page'] =  $this->_get_selected_per_page();
        $data['per_page_options'] = $this->per_page_options;

        //finally pass this to a view.
        $data['view_module'] = 'vtl_gen';
        $data['view_file'] = 'showdata';
        $this->template('public', $data);
    }


when this displays the data for the first page the url is as follows:

http://localhost:8080/datagen/vtl_gen/showData?selectedTable=orders


If I click the next pagination page it goes to
http://localhost:8080/datagen/vtl_gen/showData/2


and throws this error
Warning: Undefined array key "selectedTable" in C:\xampp\htdocs\datagen\modules\vtl_gen\controllers\Vtl_gen.php on line 256

that's this line in the function
//this is currently throwing an error with pagination
$selectedTable = $_GET['selectedTable'];

However if the pagination were to create the following url
http://localhost:8080/datagen/vtl_gen/showData/2/?selectedTable=orders


it would just work.

So the question is, how do I get the pagination to produce a valid working url.

For those of you who watched today's live stream at the end DC came up with a suggestion but I can't get it to work, probably my fault!

All the code for this is in the data generator git hub repo in the show data branch. You'll need to add a new table and put data in it to test this, all pretty easy to do with the data generator.
Level One Member

Dom

User Level: Level One Member

Date Joined: 12/01/2024

Posted by djnordeen on Monday 1st April 2024 at 19:28 GMT

Hey Dom,
Have you tried
json($selectedTable, true)

To see what is in the $selected table?
Dan
Early Adopter

djnordeen

User Level: Early Adopter

Date Joined: 20/08/2021

Posted by DaFa on Tuesday 2nd April 2024 at 00:35 GMT

Hi Dom,

As you are selecting tables from the dropdown and redirecting to the showData() method you need to be able to save the currently selected table into a session variable on subsequent pagination requests.
//show table from Get request and set session variable on other pages
if (isset($_GET['selectedTable'])) {
    $selectedTable = $_GET['selectedTable'];
    $_SESSION['selectedTable'] = $selectedTable;
} else {
    $selectedTable = $_SESSION['selectedTable'];
}
Then if you want, clear the session variable in the index() method
unset($_SESSION['selectedTable']);
I've just created a pull request with the above code (sorry if my automatic code formatting by the Prettier VSCode plugin has messed with your `Feng shui` coding style 🤓)

This comment was edited by DaFa on Tuesday 2nd April 2024 at 00:35 GMT

Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by Dom on Tuesday 2nd April 2024 at 07:30 GMT

Thank you Simon

I'd tried that but missed the crucial $_SESSION bit.
I have incorporated your changes into the showdata branch which I will then merge back into the main branch.

I've had an idea to solve the potential problem with handling image columns with regard to fake data and I found a potential bug that causes issues with required fields which should be an easy fix.

Hopefully if I can get those two fixed it will be sufficiently robust to survive a release for the module itself.
Level One Member

Dom

User Level: Level One Member

Date Joined: 12/01/2024

×