A
A
Andrei St2020-02-12 12:13:35
PHP
Andrei St, 2020-02-12 12:13:35

How to paginate data from a database?

There is a code to display data from the database in the form of announcements with restrictions, but you need to display full data from the database with pagination page by page, how to do this?

<?php
  mysqli_set_charset($link, 'utf8');
  $sql = mysqli_query($link, 'SELECT `id`, `title`, `content`, `created_at` FROM `mcraft_news` ORDER BY created_at DESC LIMIT 4');
  while ($result = mysqli_fetch_array($sql)) {
      if(strlen($result['content'])>$numchar) $srttmpend = "...";
      else $strtmpend = "";
    echo "{$result['created_at']}<br><br>";
    echo "{$result['title']}<br>";
    echo "<p class=newsblock>".substr($result['content'], 0, $numchar+$pos).$srttmpend;
    echo "<br><a target='_blank' href=//shop.mcraft.pw/news/".$result['id'].">подробнее</a></p>";
  }
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton R., 2020-02-12
@anton_reut

1) Determine the number on one page
2) Select all content units from the database, count
3) Divide the number of content units by the number on one page, round up. We get the number of pages
4) We form links of pages. Each link manages a query to the database by sending the number of content units to be displayed and the so-called "indentation" - how many content units to skip. For example, you have 4 products per page, so the "indent" on the second page will be -4, on the third -8, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question