Only first page works

My problem is that only the first page has pagination. The others are empty. I have tried to make it work but have not succeeded. Does anyone have an idea.

<?php
$datatable = "component_value"; // MySQL table name
$results_per_page = 10; // number of results per page
$foodid = $_SESSION['foodid'] ;
 
echo $foodid ;
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $results_per_page;

$sql = "SELECT EUFDNAME, BESTLOC FROM $datatable WHERE FOODID = '".$_SESSION['foodid']."' ORDER BY EUFDNAME ASC LIMIT $start_from, ".$results_per_page;
$rs_result = $conn->query($sql);
$foodid = $_SESSION['foodid'] ;
?> 
<table border="1" cellpadding="4">
<tr>
    <td bgcolor="#CCCCCC"><strong>Livamedel</strong></td>
    <td bgcolor="#CCCCCC"><strong>Innehåll</strong></td>
<?php 
 while($row = $rs_result->fetch_assoc()) {
?> 
            <tr>
            <td><? echo $row["EUFDNAME"]; ?></td>
            <td><? echo $row["BESTLOC"]; ?></td>
          
            </tr>
<?php 
}; 
?> 
</table>

<?php 
$sql = "SELECT COUNT(EUFDNAME) AS total FROM  $datatable WHERE FOODID = '".$_SESSION['foodid']."'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total_pages = ceil($row["total"] / $results_per_page); // calculate total pages with results
  
for ($i=1; $i<=$total_pages; $i++) {  // print links for all pages
            echo "<a href='index.php?page=".$i."'";
            if ($i==$page)  echo " class='curPage'";
            echo ">".$i."</a> "; 
}; 
?>

You first need three things to do pagination.

    if (isset($_GET['page']) && !empty($_GET['page'])) { // The current page
        $current_page = urldecode($_GET['page']);
    } else {
        $current_page = 1;
    }

    $per_page = 3; // Total number of records to be displayed:
    $total_count = CMS::countAllPage('blog'); // Total Records in the db table:

Then you need to figure out total pages to paginate and the offset

public function offset(): float|int
{
    return $this->per_page * ($this->current_page - 1);
}

#[Pure] public function total_pages(): float|bool
{
    return ceil($this->total_count / $this->per_page);
}

then you fetch the current page(s) to return

/*
 * Pagination static function/method to limit
 * the number of records per page. This is
 * useful for tables that contain a lot of
 * records (data).
 */
public static function page($perPage, $offset, $loc = 'index'): array
{
    $sql = 'SELECT * FROM ' . static::$table . ' WHERE page=:page ORDER BY date_updated DESC LIMIT :perPage OFFSET :blogOffset';
    $stmt = Database::pdo()->prepare($sql); // Prepare the query:
    $stmt->execute(['perPage' => $perPage, 'blogOffset' => $offset, 'page' => $loc]); // Execute the query with the supplied data:
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

Obviously the code won’t work as I was just trying to get you the steps you need to do in order for it to work. You will probably be asking for more help, but the above should give you a good idea where to start.

You want to see the whole working code I have a Github repository at https://github.com/Strider64/Miniature01282021

Does the echo statement for $foodid show the expected value? Do you have a session_start() statement so that the session variable will work after the page where it is set?

echo statement for $foodid is ok and I have a session_start().
But if I write WHERE FOODID = ‘any number’ the other pages is ok.

If I remove .WHERE FOODID =’ I get 4100 pages and they are all ok.

There’s probably a data type conversion problem or a white-space character in the value, where only some of the values ‘that look okay’ actually match the foodid value, but that’s just a guess, since we don’t know what your data is, what you are seeing in front of you, and what result you are getting.

If the session variable is the result of some select/option, check-box, or radio-button search, why aren’t you propagating the value in the URL? The main point of the internet is if someone finds a web page with information on it, they can bookmark the URL or share it with someone else, and they can return to that page by using that URL. By using a session variable, this no longer works and search engines cannot index your content because they don’t propagate session variables.

It would take having enough of your data for two pages and having all the code needed to reproduce the problem, from the point where the foodid values are displayed to be selected, stored in the session variable, through to the code displaying the result.

BTW - the ; (semicolons) after the closing }; <— here, are not need, though they aren’t causing the current problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service