mysql - PHP pagination after concatonation
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.";
}
?>
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
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.