Home » Php » php – How to make paging data from mysql database

php – How to make paging data from mysql database

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have links to photographs in the database
sample table structure

id name link_pics year place

Photos are in the database a little more unspecified variable quantity, little of this pics are added to the database in different order. The first item may be a photo from 2020 but there may also be a photo from 1996

I want to do paging in such a way that if I choose, say, holidays (saved in the “place” column), it will display photos from holidays sorted for years
I want it to look something like this (photo links)

             photo_name 1996

             other_name 1996

          else_other _ photo 1996

earlier vacation – 1996 – next vacation
How to do it someone tried this
I will add that the entries in the “year” column are as follows
r1995
R2012
all photos have the year marking in the “year” column
code below

try{
 $co = 'wakacje';
 $tytul = 'lato';
 $tytul_nowy = 'lato nad morzem';
 require_once('./model/polaczenie.php');

      $pokaz= $db1->query("SELECT COUNT(*) FROM tabela where co='$co' and (tytul='$tytul_nowy' or tytul='$tytul') group by rok")->fetchColumn();
      echo $pokaz;
      $limit = $pokaz;
       $strony = ceil($pokaz/ $limit);
        $strony = ceil($pokaz/ $limit);

    // What page are we currently on?
    $strona = min($strony, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
        'options' => array(
            'default'   => 1,
            'min_range' => 1,
        ),
    )));

    // Calculate the offset for the query
    $offset = ($strona - 1)  * $limit;

    // Some information to display to the user
    $start = $offset + 1;
    $end = min(($offset + $limit), $pokaz);

    // The "back" link
    $prevlink = ($strona > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($strona - 1) . '" title="Pierwsza strona">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

    // The "forward" link
    $nextlink = ($strona < $strony) ? '<a href="?page=' . ($strona + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $strony . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

    // Display the paging information


    $pokaz1= $db1->prepare("SELECT * FROM table where co='$co' and (tytul='$tytul_nowy' or tytul='$tytul')   LIMIT :limit OFFSET :offset");

    // Bind the query params
    $pokaz1->bindParam(':limit', $limit, PDO::PARAM_INT);
    $pokaz1->bindParam(':offset', $offset, PDO::PARAM_INT);
    $pokaz1->execute();

    // Do we have any results?
    if ($pokaz1->rowCount() > 0) {
        // Define how we want to fetch the results
        $pokaz1->setFetchMode(PDO::FETCH_ASSOC);
        $wynik = new IteratorIterator($pokaz1);

        // Display the results
        foreach ($wynik as $row) {
            echo '<p>', $row['link'], '</p>';
        }

    } else {
        echo '<p>No results could be displayed.</p>';
    }

} catch (Exception $e) {
    echo '<p>', $e->getMessage(), '</p>';
}


          $db1=null;
          echo '<div id="paging"><p>', $prevlink, ' Strona ', $strona, ' z ', $strony, ' stron, wyświetlanie ', $start, '-', $end, ' z ', $film, ' wyników ', $nextlink, ' </p></div>';
How to&Answers: