D
D
Daniil Sukhikh2018-04-27 10:42:03
PHP
Daniil Sukhikh, 2018-04-27 10:42:03

How to paginate posts in php?

There is a script for displaying news, but they are all displayed at once, how to make them appear on pages, for example, so that there would be no more than 10 news on each page.
output script:

$page = $_GET['page'];
if (!isset($page)) {
    $command = mysqli_query($connection, "SELECT * FROM `news` ORDER BY ID DESC");
    require 'pages/news.php';
  }

news.php:
<div class="newsName">Последние новости</div>
    <?php
    $empn = mysqli_num_rows($command);
    if ($empn == 0) {
      echo "<div class='empn'>Новостей на данный момент нет! Следите за обновлениями.</div>";
    } else {
foreach ($command as $key => $itm):
  $valdesc = iconv_strlen($itm['desc']);
  $text = mb_substr($itm['desc'],0,1069, 'UTF-8');
  $textt = $text . "...";
?>
  <div class="news">
    <div class="newsZak"><a href="?page=news&id=<?php echo $itm['id']; ?>"><?php echo $itm['name']; ?></a></div>
    <div class="newsDate"><?php echo $itm['date']; ?></div>
    <div class="newsLine"></div>
    <div class="newsDesc"><?php if ($valdesc > 1069) {
      echo $textt;
    } else {
      echo $text;
    } ?></div>
    <?php
    if ($valdesc > 1069) {
    ?>
    <a class="NewsPod" href="?page=news&id=<?php echo $itm['id']; ?>">Открыть полностью</a>
    <?php
    }
    ?>
  </div>
<?php
endforeach;
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-04-27
@danchiksux

If you need to display 10 records, why get everything from the database? Google Mysql limit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question