<?php
$table = "replies";
$sql_count = "SELECT COUNT(id) FROM $table WHERE nid = '$_GET[id]'";	// Count the number of posts
$res_count = mysql_query($sql_count);
$total_posts = mysql_result($res_count, 0);

$posts = isset($_GET['posts']) ? $_GET['posts'] : 0;
$per_page = 5;   // Set the amount of posts per page

$sql_replies = "SELECT * FROM $table WHERE nid = '$_GET[id]' ORDER BY id DESC LIMIT $posts, $per_page";   // Select posts where the limit is set using variables
$res_replies = mysql_query($sql_replies);
if (mysql_num_rows($res_replies) >= 1) {
	while ($row_replies = mysql_fetch_array($res_replies)) {
		echo "<p>".$row_replies['message']."</p>";
		}
	echo pagenumbers($total_posts, $per_page);
	}

function pagenumbers($total_posts, $per_page) {
	$pages = 0;   // Set initial amount of pages
	$posts = 0;   // Set initial amount of posts
	$num = $total_posts / $per_page;   // Get amount of pages
	$num = ceil($num);
	if ($num >= 2) {   // Set the amount of pages at wich to start numbering
		echo "<b>";
		while ($pages < $num) {   // Check if current page is less as amount of $pages
			$pages ++;   // Add 1 to page
			if ($posts == $_GET['posts']) {   // Check if current page equals $posts so it isn't a link
				$links = $pages." ";
				}
			else {   // Set pagenumber as a link
				$links = "<a href=\"$_SERVER[PHP_SELF]?page=$_GET[page]&id=$_GET[id]&posts=$posts\">".$pages." </a>";
				}
			echo $links;   // Display link
			if ($pages < $num) {   // Check if current page is less as amount of $pages
				echo " : ";   // Set the divider between the pagenumbers
				}
			$posts = $posts + $per_page;   // Set the modifier for $posts
			}
		echo "</b>";
		}
	}
?>