Jump to content

Recommended Posts

The title of this thread is basically the result I'm aiming for. I have pagination (paged results) in place. Say for example, I show 2 items per page and there are 10 items in the system, I want page 1 to show 1-2 of 10 and page 2 to show 3-4 of 10 and so forth. I have a script at the moment that I'm working on, but the result is never right, no matter what I do:

 

$start_num = $page->page_num * $advert->per_page;
$end_num = $start_num + $advert->per_page;
if($end_num > $all) $end_num = $all;
$showing = "$start_num - $end_num of $all";

 

$page->page_num is the current page number.

$advert->per_page is how many items to show on each page.

 

$page->page_num will never be 0 or bigger than the total amount of pages in the system. Any help would be appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/179173-solved-showing-1-2-of-15-results/
Share on other sites

Use below given functions

 

function select_row($sql)
{
//echo $sql . "<br />";
if ($sql!="")
{
	$result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error());
	if ($result)
	{
		while($row = mysql_fetch_assoc($result))
			$data[] = $row;
	}
	return $data;
}
}

function pagingSlot($sql, $recperpage, $pagesetlimit, $page, $class, $getvars)
{
$rescnt=mysql_query($sql);
$totcnt=mysql_num_rows($rescnt);
   
	if (!$page)   
		$page = 1;
	$first=(($page-1)* $recperpage);

	$sql = $sql . " limit ".$first.",".$recperpage;
	$res = select_row($sql);
   
	$serial_no = ($page - 1) * $recperpage;
   
	$t = ($totcnt/$recperpage);
	$arr=split('[.]',$t);
	if ($arr[1])
		$totalpages=$arr[0]+1;
	else
		$totalpages=$arr[0];
   
	if ($totalpages > $pagesetlimit)
	{
		if ($page > 1)
			$pagesetstart = $page - 1;
		else
			$pagesetstart = $page;               
		   
		$pagesetend= ($pagesetstart-1) + $pagesetlimit;
		if ($pagesetend > $totalpages)
		{
			$pagesetend = $totalpages;
			$pagesetstart = $pagesetend - $pagesetlimit + 1;
		}
	}
	else
	{
		$pagesetstart = 1;
		$pagesetend = $totalpages;
	}
   
	$str = "";

	if ($page > 1)
	{
		$prev = $page - 1;
		$str.= "<a href='".$_SERVER['PHP_SELF']."?page=$prev".$getvars."'  class='".$class."'> << </a> | ";                                           
	}
	else
	{
		$str.= "<<  | ";   
	}


	for ($i=$pagesetstart; $i<=$pagesetend; $i++)
	{       
   
		if ($i <= $totalpages)
		{
			if (!$page)
				$page=1;
			if ($page==$i)
				$str.= '<font color=red>'.$i.'</font> ';
			else
				$str.= "<a href='".$_SERVER['PHP_SELF']."?page=$i".$getvars."'  class='".$class."'>".$i."</a> ";
		}                               
	}

	if ($page < $totalpages)
	{
		$next = $page + 1;
		$str.= " | <a href='".$_SERVER['PHP_SELF']."?page=$next".$getvars."'  class='".$class."'> >> </a> ";                                           
	}
	else
	{
		$str.= " |  >>  ";
	}

if ($totcnt == 0)
	$str = "";
   
$arr["records"]=$res;
$arr["link"]=$str;
$arr["serial"]=$serial_no;
return $arr;
}        

Worked a charm. Cheers! Just made one change:

 

$start_num = ($page->page_num * $advert->per_page) - ($advert->per_page - 1);
$end_num = ($start_num + $advert->per_page) - 1; //take away one from end_num
if($end_num > $all) $end_num = $all;
$string_1 = "$start_num - $end_num of $all";

 

Seems to be working fine! :)

Use below given functions

 

function select_row($sql)
{
//echo $sql . "<br />";
if ($sql!="")
{
	$result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error());
	if ($result)
	{
		while($row = mysql_fetch_assoc($result))
			$data[] = $row;
	}
	return $data;
}
}

function pagingSlot($sql, $recperpage, $pagesetlimit, $page, $class, $getvars)
{
$rescnt=mysql_query($sql);
$totcnt=mysql_num_rows($rescnt);
   
	if (!$page)   
		$page = 1;
	$first=(($page-1)* $recperpage);

	$sql = $sql . " limit ".$first.",".$recperpage;
	$res = select_row($sql);
   
	$serial_no = ($page - 1) * $recperpage;
   
	$t = ($totcnt/$recperpage);
	$arr=split('[.]',$t);
	if ($arr[1])
		$totalpages=$arr[0]+1;
	else
		$totalpages=$arr[0];
   
	if ($totalpages > $pagesetlimit)
	{
		if ($page > 1)
			$pagesetstart = $page - 1;
		else
			$pagesetstart = $page;               
		   
		$pagesetend= ($pagesetstart-1) + $pagesetlimit;
		if ($pagesetend > $totalpages)
		{
			$pagesetend = $totalpages;
			$pagesetstart = $pagesetend - $pagesetlimit + 1;
		}
	}
	else
	{
		$pagesetstart = 1;
		$pagesetend = $totalpages;
	}
   
	$str = "";

	if ($page > 1)
	{
		$prev = $page - 1;
		$str.= "<a href='".$_SERVER['PHP_SELF']."?page=$prev".$getvars."'  class='".$class."'> << </a> | ";                                           
	}
	else
	{
		$str.= "<<  | ";   
	}


	for ($i=$pagesetstart; $i<=$pagesetend; $i++)
	{       
   
		if ($i <= $totalpages)
		{
			if (!$page)
				$page=1;
			if ($page==$i)
				$str.= '<font color=red>'.$i.'</font> ';
			else
				$str.= "<a href='".$_SERVER['PHP_SELF']."?page=$i".$getvars."'  class='".$class."'>".$i."</a> ";
		}                               
	}

	if ($page < $totalpages)
	{
		$next = $page + 1;
		$str.= " | <a href='".$_SERVER['PHP_SELF']."?page=$next".$getvars."'  class='".$class."'> >> </a> ";                                           
	}
	else
	{
		$str.= " |  >>  ";
	}

if ($totcnt == 0)
	$str = "";
   
$arr["records"]=$res;
$arr["link"]=$str;
$arr["serial"]=$serial_no;
return $arr;
}        

 

Thanks man, but I have it fixed. I have a pagination class that I made that works perfectly. Might add in support for showing the current range.

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.