A
A
Anastasia2020-05-07 13:08:43
PHP
Anastasia, 2020-05-07 13:08:43

How to do pagination?

I have a database where projects are located, and there is navigation through these projects on the page using two buttons "Forward" and "Back"

<?php
$nextpost = next_post($post_id);
$prevpost = prev_post($post_id);
?>


<div>
<a href="/project?post_id=<?= $prevpost['id'] ?>"><button>Prev</button></a>
<a href="/project?post_id=<?= $nextpost['id'] ?>"><button">Next</button></a>
</div>


function next_post($post_id)
{
    global $link;
    $sql = "SELECT * FROM projects WHERE id > " . $post_id . " ORDER BY id ASC LIMIT 1";
    $result = mysqli_query($link, $sql);
    $nextpost = mysqli_fetch_assoc($result);
    return $nextpost;
}

function prev_post($post_id)
{
    global $link;
    $sql = "SELECT * FROM projects WHERE id < " . $post_id . " ORDER BY id DESC LIMIT 1";
    $result = mysqli_query($link, $sql);
    $prevpost = mysqli_fetch_assoc($result);
    return $prevpost;
}


The problem is that if, for example, I am on a project with id=5 (and id=6 does not yet exist) and at the same time I click on the "Forward" button, then I am transferred to a page with id=6, which does not exist and i just get an error.

How to make it so that when I click on the "Next" button, provided that I do not have the next project, I will be thrown to the page with the id of the very first project in the database. (Same with the back button)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Entelis, 2020-05-07
@DmitriyEntelis

It's not clear from the code below what happens.

if, for example, I am on a project with id=5 (and id=6 does not yet exist) and at the same time I click on the "Forward" button, then I am transferred to a page with id=6, which does not exist and I just get an error.

The general idea of ​​pagination: see what data you have in the database, based on this you form links to possible pages.
If you want to make it cyclic - well, do it, add a check "id greater than the current one is not found" to your logic and process it correctly.

I
Ildar Saribzhanov, 2020-05-07
@Bluz

Gods of clean code forgive me.

/**
 * Следующий пост
 *
 * @param $post_id
 *
 * @return string[]|null
 */
function next_post($post_id)
{
    global $link;
    $sql      = "SELECT * FROM projects WHERE id > " . $post_id . " ORDER BY id ASC LIMIT 1";
    $result   = mysqli_query($link, $sql);
    $nextpost = mysqli_fetch_assoc($result);

    if (!$nextpost) {
        return first_post();
    }

    return $nextpost;
}

/**
 * Предыдущий пост
 *
 * @param $post_id
 *
 * @return string[]|null
 */
function prev_post($post_id)
{
    global $link;
    $sql      = "SELECT * FROM projects WHERE id < " . $post_id . " ORDER BY id DESC LIMIT 1";
    $result   = mysqli_query($link, $sql);
    $prevpost = mysqli_fetch_assoc($result);

    if (!$prevpost) {
        return last_post();
    }

    return $prevpost;
}

/**
 * Первый пост
 *
 * @return string[]|null
 */
function first_post()
{
    global $link;
    $sql    = "SELECT * FROM projects WHERE ORDER BY id ASC LIMIT 1";
    $result = mysqli_query($link, $sql);
    $post   = mysqli_fetch_assoc($result);

    return $post;
}

/**
 * Последний пост
 *
 * @return string[]|null
 */
function last_post()
{
    global $link;
    $sql    = "SELECT * FROM projects WHERE ORDER BY id DESC LIMIT 1";
    $result = mysqli_query($link, $sql);
    $post   = mysqli_fetch_assoc($result);

    return $post;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question