IDs Required?

This might be a dumb question but I’m wondering if IDs are required for pagination? What is Limit and Offset looking at? Does Mysql have internal IDs? If Data does not need to be ordered by ID they can be left out without impacting pagination?

An artificial ID would not be required for pagination (or database derived “pagination”), as long as the result order is guaranteed for the query; so there would need to be some sort of index or sorting on the data, to establish that.

no

it’s looking at the rows as though they were numbered, so these are like “virtual” row numbers

yes

1 Like

Pagination is pretty simple here’s in broken down in PHP →

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    if (isset($_GET['category'])) {
        $category = $_GET['category'];
    } else {
        error_log('Category is not set in the GET data');
        $category = 'general';
    }
    $total_count = $cms->countAllPage($category);
} else {
    try {
        $category = 'general';
        $total_count = $cms->countAllPage($category);
    } catch (Exception $e) {
        error_log('Error while counting all pages: ' . $e->getMessage());
    }
}

// Grab the current page the user is on
if (isset($_GET['page']) && !empty($_GET['page'])) {
    $current_page = urldecode($_GET['page']);
} else {
    $current_page = 1;
}

$per_page = 2; // Total number of records to be displayed:


// Grab Total Pages
$total_pages = $cms->total_pages($total_count, $per_page);


/* Grab the offset (page) location from using the offset method */
/* $per_page * ($current_page - 1) */
$offset = $cms->offset($per_page, $current_page);

// Figure out the Links that you want the display to look like
$links = new Links($current_page, $per_page, $total_count, $category);

// Finally grab the records that are actually going to be displayed on the page
$records = $cms->page($per_page, $offset, 'cms', $category);
1 Like

Thanks guys. I’m learning normalization and table efficiency. Trying to understand fast databases. Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.