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
https://forums.phpfreaks.com/topic/56887-solved-pagination/
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
https://forums.phpfreaks.com/topic/56887-solved-pagination/#findComment-281072
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
https://forums.phpfreaks.com/topic/56887-solved-pagination/#findComment-281220
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.