Jump to content

Pagination help


heric9

Recommended Posts

hi ive simple code. but i would need help how to do pagination. ive followed one tutorial and ive code.but i really dont know how to connect these.or if its even possible to connect.

 

heres the index.php file

 

<?php

include '_class/cms_class.php';

$obj = new modernCMS();

$obj->host =  'localhost';
$obj->username =  'root';
$obj->password =  'root';
$obj->db =  'modernCMS';

$obj->connect();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" title="no title" charset="utf-8">

</head>

<body>

<div id="page-wrap">
<?php 

if(isset($_GET['id'])):
$obj->get_content($_GET['id']);
else:
$obj->get_content();
endif;

?>
</div>
</body>
</html>

 

cms_class.php

<?php 

class modernCMS {

var $host;
var $username;
var $password;
var $db;

function connect() {
	$con = mysql_connect($this->host, $this->username, $this->password) or die(mysql_error());
	mysql_select_db($this->db, $con) or die(mysql_error());
}



function get_content($id = '') {

	if($id != ""):
		$id = mysql_real_escape_string($id);
		$sql = "SELECT * FROM cms_content WHERE id = '$id'";	

	else:
		$sql = "SELECT * FROM cms_content ORDER BY id DESC";
	endif;

	$res = mysql_query($sql) or die(mysql_error());

		while($row = mysql_fetch_assoc($res)) {
			echo '<h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1>';
			echo '<p>' . $row['body'] . '</p>';
		}


}



} 

?>

 

Heres the pagination code ihave

<?php

include 'db.inc.php';
$per_page = 2;



$pages_query = mysql_query("SELECT COUNT(`id`) FROM `cms_content`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);

$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;

$query = mysql_query("SELECT * FROM `cms_content` LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query)) {

echo '<p>', $query_row['title']   ,'</p>';
echo '<p>', $query_row['body']   ,'</p>';


}

if ($pages >=1 && $page <= $pages) {

for ($x=1; $x<=$pages; $x++) {
	echo ($x == $page) ? '<strong><a href="?page=' .$x. '">' .$x. '</a></strong>  ' : '<a href="?page=' .$x. '">' .$x. '</a> ';


	}


}

?>

 

thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.