Pagination For Script [PHP]

Discussion in 'Programming' started by Pathan, Jan 4, 2011.

  1. #1
    Hi,

    I am running a script on my website, currently we have simple pagination like

    [​IMG]

    it doesn't show how many total pages we have and when clicked the last page i.e. 12 in this case here is how it look

    [​IMG]

    I want it like

    [​IMG]

    Here is what we have in pagination class for now.

    <?
    class pagination
    {
    	public function __construct()
    	{
    	}
    	public function calculate_pages($total_rows, $rows_per_page, $page_num)
    	{
    		$arr = array();
    		// calculate last page
    		$last_page = ceil($total_rows / $rows_per_page);
    		// make sure we are within limits
    		$page_num = (int) $page_num;
    		if ($page_num < 1)
    		{
    		   $page_num = 1;
    		} 
    		elseif ($page_num > $last_page)
    		{
    		   $page_num = $last_page;
    		}
    		$upto = ($page_num - 1) * $rows_per_page;
    		$arr['limit'] = 'LIMIT '.$upto.',' .$rows_per_page;
    		$arr['current'] = $page_num;
    		if ($page_num == 1)
    			$arr['previous'] = $page_num;
    		else
    			$arr['previous'] = $page_num - 1;
    		if ($page_num == $last_page)
    			$arr['next'] = $last_page;
    		else
    			$arr['next'] = $page_num + 1;
    		$arr['last'] = $last_page;
    		$arr['info'] = 'Page ('.$page_num.' of '.$last_page.')';
    		$arr['pages'] = $this->get_surrounding_pages($page_num, $last_page, $arr['next']);
    		return $arr;
    	}
    	function get_surrounding_pages($page_num, $last_page, $next)
    	{
    		$arr = array();
    		$show = 12; // how many boxes
    		// at first
    		if ($page_num == 1)
    		{
    			// case of 1 page only
    			if ($next == $page_num) return array(1);
    			for ($i = 0; $i < $show; $i++)
    			{
    				if ($i == $last_page) break;
    				array_push($arr, $i + 1);
    			}
    			return $arr;
    		}
    		// at last
    		if ($page_num == $last_page)
    		{
    			$start = $last_page - $show;
    			if ($start < 1) $start = 0;
    			for ($i = $start; $i < $last_page; $i++)
    			{
    				array_push($arr, $i + 1);
    			}
    			return $arr;
    		}
    		// at middle
    		$start = $page_num - $show;
    		if ($start < 1) $start = 0;
    		for ($i = $start; $i < $page_num; $i++)
    		{
    			array_push($arr, $i + 1);
    		}
    		for ($i = ($page_num + 1); $i < ($page_num + $show); $i++)
    		{
    			if ($i == ($last_page + 1)) break;
    			array_push($arr, $i);
    		}
    		return $arr;
    	}
    }
    ?>
    PHP:
    And on page where we are displaying the pagination have the following code

    {foreach from=$pagination.pages item=page}
    PHP:
    Can anybody please sort this thing out for me. I am willing to pay a reasonable fees for this.

    -Regards.
     
    Pathan, Jan 4, 2011 IP
  2. viruthagiri

    viruthagiri Active Member

    Messages:
    232
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    95
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    viruthagiri, Jan 5, 2011 IP
  3. dhblewis

    dhblewis Guest

    Messages:
    466
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    I could fix this for you for $50 as it will require a lot of rejigging of the code. PM if interested.
     
    dhblewis, Jan 6, 2011 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    As Seller:
    100% - 0
    As Buyer:
    100% - 1
    #4
    Here you go.
    Besides fixing your code I've took the permission to show only 11 pages at once.
    PM what the reward is please.

    
    <?
    class pagination
    {
        public function __construct()
        {
        }
        public function calculate_pages($total_rows, $rows_per_page, $page_num)
        {
            $arr = array();
            // calculate last page
            $last_page = ceil($total_rows / $rows_per_page);
            // make sure we are within limits
            $page_num = (int) $page_num;
            if ($page_num < 1)
            {
               $page_num = 1;
            }
            elseif ($page_num > $last_page)
            {
               $page_num = $last_page;
            }
            $upto = ($page_num - 1) * $rows_per_page;
            $arr['limit'] = 'LIMIT '.$upto.',' .$rows_per_page;
            $arr['current'] = $page_num;
            if ($page_num == 1)
                $arr['previous'] = $page_num;
            else
                $arr['previous'] = $page_num - 1;
            if ($page_num == $last_page)
                $arr['next'] = $last_page;
            else
                $arr['next'] = $page_num + 1;
            $arr['last'] = $last_page;
            $arr['info'] = 'Page ('.$page_num.' of '.$last_page.')';
            $arr['pages'] = $this->get_surrounding_pages($page_num, $last_page, $arr['next']);
            return $arr;
        }
        function get_surrounding_pages($page_num, $last_page, $next)
        {
            $arr = array();
            $show = 11; // how many boxes
            $left = $right = 5;
            // at first
            if ($page_num == 1)
            {
                // case of 1 page only
                if ($next == $page_num) return array(1);
                for ($i = 0; $i < $show; $i++)
                {
                    if ($i == $last_page) break;
                    array_push($arr, $i + 1);
                }
                return $arr;
            }
            // at last
            if ($page_num == $last_page)
            {
                $start = $last_page - $show;
                if ($start < 1) $start = 0;
                for ($i = $start; $i < $last_page; $i++)
                {
                    array_push($arr, $i + 1);
                }
                return $arr;
            }
            // at middle
            $lrest = $rrest = 0;
            $start = 0;
            $end = $last_page;
            if($page_num - $left >= 0)
            {
                $start = $page_num - $left;    
            } else {
                $lrest = $left - $page_num;
            }
            
            if($page_num + $right <= $last_page)
            {
                $end = $page_num + $right;    
            } else {
                $rrest = ($page_num + $right) - $last_page;
            }
            
            if($rrest > 0)
                $start -= $rrest;
            
            if($lrest > 0)
                $end += $lrest;
            
            for ($i = $start-1; $i < $end; $i++)
            {
                array_push($arr, $i + 1);
            }
            return $arr;
        }
    }
    ?>
    
    PHP:
     
    tvoodoo, Jan 6, 2011 IP
  5. Pathan

    Pathan Well-Known Member

    Messages:
    2,196
    Likes Received:
    218
    Best Answers:
    0
    Trophy Points:
    165
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    Here is what I got after adding the above coding.

    [​IMG]

    This is not what i want.
     
    Pathan, Jan 6, 2011 IP
  6. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    As Seller:
    100% - 0
    As Buyer:
    100% - 1
    #6
    Show me the full part of the template related to pagination please...
    the one that includes :
    {foreach from=$pagination.pages item=page}
     
    tvoodoo, Jan 6, 2011 IP
  7. Pathan

    Pathan Well-Known Member

    Messages:
    2,196
    Likes Received:
    218
    Best Answers:
    0
    Trophy Points:
    165
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #7
    Here is what we have

     
    Pathan, Jan 6, 2011 IP
  8. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    As Seller:
    100% - 0
    As Buyer:
    100% - 1
    #8
    How about this :

    
    <?
    class pagination
    {
        public function __construct()
        {
        }
        public function calculate_pages($total_rows, $rows_per_page, $page_num)
        {
            $arr = array();
            // calculate last page
            $last_page = ceil($total_rows / $rows_per_page);
            // make sure we are within limits
            $page_num = (int) $page_num;
            if ($page_num < 1)
            {
               $page_num = 1;
            }
            elseif ($page_num > $last_page)
            {
               $page_num = $last_page;
            }
            $upto = ($page_num - 1) * $rows_per_page;
            $arr['limit'] = 'LIMIT '.$upto.',' .$rows_per_page;
            $arr['current'] = $page_num;
            if ($page_num == 1)
                $arr['previous'] = $page_num;
            else
                $arr['previous'] = $page_num - 1;
            if ($page_num == $last_page)
                $arr['next'] = $last_page;
            else
                $arr['next'] = $page_num + 1;
            $arr['last'] = $last_page;
            $arr['info'] = 'Page ('.$page_num.' of '.$last_page.')';
            $arr['pages'] = $this->get_surrounding_pages($page_num, $last_page, $arr['next']);
            return $arr;
        }
        function get_surrounding_pages($page_num, $last_page, $next)
        {
            $arr = array();
            $show = 5; // how many boxes
            $left = $right = 2;
            // at first
            if ($page_num == 1)
            {
                // case of 1 page only
                if ($next == $page_num) return array(1);
                for ($i = 0; $i < $show; $i++)
                {
                    if ($i == $last_page) break;
                    array_push($arr, $i + 1);
                }
                return $arr;
            }
            // at last
            if ($page_num == $last_page)
            {
                $start = $last_page - $show;
                if ($start < 1) $start = 0;
                for ($i = $start; $i < $last_page; $i++)
                {
                    array_push($arr, $i + 1);
                }
                return $arr;
            }
            // at middle
            $lrest = $rrest = 0;
            $start = 0;
            $end = $last_page;
            if($page_num - $left >= 0)
            {
                $start = $page_num - $left;    
            } else {
                $lrest = $left - $page_num;
            }
            
            if($page_num + $right <= $last_page)
            {
                $end = $page_num + $right;    
            } else {
                $rrest = ($page_num + $right) - $last_page;
            }
            
            if($rrest > 0)
                $start -= $rrest;
            
            if($lrest > 0)
                $end += $lrest;
            
            for ($i = $start-1; $i < $end; $i++)
            {
                array_push($arr, $i + 1);
            }
            return $arr;
        }
    }
    ?>
    
    PHP:
    Drop me a screenshot pls.
     
    tvoodoo, Jan 6, 2011 IP
  9. Pathan

    Pathan Well-Known Member

    Messages:
    2,196
    Likes Received:
    218
    Best Answers:
    0
    Trophy Points:
    165
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #9
    No changes same as it was before.

    But one more thing i have noticed there is some coding for pagination in one another file here is what we have

     
    Pathan, Jan 6, 2011 IP
  10. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    As Seller:
    100% - 0
    As Buyer:
    100% - 1
    #10
    Dude did you change the template to what I gave you :
    <div style="border : 1px solid black;">{$pagination.info}</div>
    {if $page > 1}
    <a href="/sms-messages/{$category.category_id}/{$category.name_url}/1" style="border: 1px solid #000000; padding: 0px 5px; color: #000000;">First &laquo;</a>
    {/if}
    {if $page > 1}
    <a href="/sms-messages/{$category.category_id}/{$category.name_url}/{$pagination.previous}" {if $pagination.current != $page}style="border: 1px solid #000000; padding: 0px 5px; color: #000000;"{/if}>&laquo;</a>
    {/if}
    {foreach from=$pagination.pages item=page}
    <a href="/sms-messages/{$category.category_id}/{$category.name_url}{if $page != 1}/{$page}{/if}" {if $pagination.current != $page}style="border: 1px solid #000000; padding: 0px 5px; color: #000000;"{/if}>{$page}</a>
    {/foreach}
    {if $page < $pagination.last}
    <a href="/sms-messages/{$category.category_id}/{$category.name_url}/{$pagination.next}" {if $pagination.current != $page}style="border: 1px solid #000000; padding: 0px 5px; color: #000000;"{/if}>&raquo;</a>
    {/if}
    {if $page < $pagination.last}
    <a href="/sms-messages/{$category.category_id}/{$category.name_url}/{$pagination.last}" style="border: 1px solid #000000; padding: 0px 5px; color: #000000;">Last &raquo;</a>
    {/if}
     
    tvoodoo, Jan 6, 2011 IP
  11. Pathan

    Pathan Well-Known Member

    Messages:
    2,196
    Likes Received:
    218
    Best Answers:
    0
    Trophy Points:
    165
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #11
    Awesome it worked :)
     
    Pathan, Jan 6, 2011 IP