mysql - PHP pagination after concatonation

543

I'm new to pagination and am looking to display 10 topics per view_category.php page.

On the index.php there are links to each category, leading to category.php?cid=1 (or whatever the category (cid) number is).

How would I go about displaying 10 topics per page under each cid?

This is the code I've experimented with thus far:

<?php
include('connect.php'); // Connects to the DB

if(isset($_GET['page']))
{
    $page = $_GET['page'];
    $page = mysql_real_escape_string($page);
} else { 
   $page = 1;
}
$cid = $_GET['cid'];
$result = mysql_query("SELECT * FROM topics");
$rows = mysql_num_rows($result);

$per_page = 5;

$total_pages = ceil($rows/$per_page);

echo "You are on page $page of $total_pages<br>";

if($page !=1)
{
    echo "<a href='view_catagory.php?cid=".$cid."?page=1'>First</a> "." ";
    $previous = $page-1;
    echo "<a href='view_catagory.php?cid=".$cid."?page=$previous'>Previous</a> "." ";
}

if(($page != 1) && ($page != $total_pages))
echo " | ";

if ($page != $total_pages)
{
    $next = $page+1;
    echo "<view_catagory.php?cid=".$cid."?=$next'>First</a> "." ";
    echo "<view_catagory.php?cid=".$cid."?=$total_pages'>Last</a> "." ";
}
echo "<hr />";

?>

.. this is how the categories are concatenated in index.php (all working fine, unsure if I'd need to do anything in here though.):

<?php
    $sql = "SELECT * FROM categories ORDER BY category_title ASC";
    $res = mysql_query($sql) or die(mysql_error());
    $categories = "";
    if (mysql_num_rows($res) > 0){
        while ($row = mysql_fetch_assoc($res)){
            $id = $row['id'];
            $title = $row['category_title'];
            $description = $row['category_description'];
    $categories .="<center><a href='view_catagory.php?cid=".$id."' class='cat_links'>".$title." - <font size'-1>".$description."</font></a></center>";
        }
        echo $categories;
    } else {
    echo "There are no categories.";
    }
    ?>
595

Answer

Solution:

This question should be posted under SQL since that is where the problem is. You must specify the offset and limit of the result set in your query. Each time you are selecting the entire set. Add something such as

    $page_number = isset($_GET['page']) ? $_GET['page'] : 1;
    $offset = 10 * ($page - 1);
    $sql = "SELECT * FROM categories ORDER BY category_title ASC LIMIT $offset, 10";

All that said, you are using old mysql functions that are no longer available in newer version of PHP. You should look into using the mysqli or pdo objects if you have any OOP experience. If you don't, you'll want to look into replacing the mysql functions (e.g. mysql_real_escape_string, mysql_query) with their mysqli counterparts.

Also, you are missing the parameter name "page" in the next and last links in your first code block.

People are also looking for solutions to the problem: php - Laravel 5 validation error

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.