Jump to content

[SOLVED] Pagination


chocopi

Recommended Posts

Can someone help me with my pagination ?

I have used it fine before but i do not know how to impliment it on my current page.

 

I have a page that loops throught my database results and I want a new page to be created every 10 results. The page number is pulled from the url using $_GET so I was wondering if anyone could possible help.

 

Here is the code

 

<?php

// start session
session_start();
require_once('page_header.php');
// get board id
$board = $_GET['board'];
if(empty($board))
{
// set board id
$board = '1';
} else
if(!empty($board))
{
	// set board to session
	$_SESSION['board'] = $board;
}
// get page number
$page_num = $_GET['page'];
// get max post_num
$query = mysql_query("SELECT MAX(post_num) AS `post_num_max` FROM `zBoard_messages` WHERE board_id='$board'") or die(mysql_error());
$fetch= mysql_fetch_assoc($query) or die(mysql_error());
$post_num_max = $fetch['post_num_max'];

for ($pn = 1; $pn <= $post_num_max; $pn++) 
{

// some loopy stuf

}

?>

 

So I need something like $pn divide by 10 then round up to nearest interger which would then decide where the info goes.

 

Many thanks ;D

 

~ Chocopi

Link to comment
Share on other sites

seriously m8 look at the tutorials on this site someone has gone through great lengths to write one on pagination and give a working demo that does exactly what u want if your stuck after tutorial then come back with bit ur stuck on  ps do a search on here for it and theres like 30 posts :)

Link to comment
Share on other sites

I have had a look at them before, but i didnt really like them ;D

 

So i took a stab at it and it works (well i havent come across any problems yet) here is the code i wrote, thanks for your help aswell !

 

<?php

// start session
session_start();
require_once('page_header.php');

// get board id
$board = $_GET['board'];
if(empty($board))
{
// set board id
$board = '1';
} else
if(!empty($board))
{
	// set board to session
	$_SESSION['board'] = $board;
}
// get page number
$page_num = $_GET['page'];
if(empty($page_num))
{
//set page num
$page_num = '1';
}
// get max post_num
$query = mysql_query("SELECT MAX(post_num) AS `post_num_max` FROM `zBoard_messages` WHERE board_id='$board'") or die(mysql_error());
$fetch= mysql_fetch_assoc($query) or die(mysql_error());
$post_num_max = $fetch['post_num_max'];

for ($pn = 1; $pn <= $post_num_max; $pn++) 
{

// pagination
$ppp = 10; //posts per page
$page = ceil($pn/$ppp);
// get max page
$max_page = ceil($post_num_max/$ppp);
// if the page equals the page number then print results
if($page == $page_num)
{

// display stuff here

}
}

//get prev button
if($page_num == 1)
{
echo "Prev ";
} else
if($page_num >=2)
{
	$prev_page = $page_num - 1;
	echo "<a href=\"view.php?page=".$prev_page."\">Prev</a> ";
}
// get page numbers
for ($page_number = 1; $page_number <= $max_page; $page_number++) 
{
if($page_number == $page_num)
{
	echo $page_number;
} else
	if($page_number != $page_num)
	{
		echo "<a href=\"view.php?page=".$page_number."\">".$page_number."</a>";
	}

	if($page_number != $max_page)
	{
		echo ", ";
	} else
		if($page_number == $max_page)
		{
			echo" ";
		}
}
// get next button
if($page_num == $max_page)
{
echo " Next";
} else
if($page_num < $max_page)
{
	$next_page = $page_num + 1;
	echo " <a href=\"view.php?page=".$next_page."\">Next</a>";
}

require('page_footer.php');

?>

 

Tell me what you think :D

 

~ Chocopi

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.