Welcome Guest, Not a member yet? Register   Sign In
How do i make helper?
#1

[eluser]richzilla[/eluser]
Hi all, ive written a very small simple helper for paginating non database arrays(because i couldnt find one myself.), and i was wondering how i turn it into a code igniter helper (or of anything exists already that does that better than ive written it).

cheers
#2

[eluser]imn.codeartist[/eluser]
please post your code first so that we can tell what you need to do.

cheers
#3

[eluser]richzilla[/eluser]
voila

Code:
$this->load->helper('file');
            
            //sets the total number of items per page
            $num_per_page = 25;
            
            //set datasource
            $data_source = 'assets/images/gallery';
            
            //set display filepath (after index.php)
            $filepath = 'gallery/Index/';
            
            //set link seperator
            $link_seperator = ' | ';
            
            //Number of links either side of your current page to eb shown in the
            //links string e.g. 2 would show 5 links in total. This will be overidden
            //if there arent enough pages for all the links
            $link_count = 4;
            
            //set previous symbol
            $prev_symbol = '<img src="'.base_url().'assets/images/prev_ico.png" class="ico"/>';
            
            //set next symbol
            $next_symbol = '<img src="'.base_url().'assets/images/next_ico.png" class="ico"/>';
            

            //The current page number
            $current_page = $this->uri->segment(3);

            //gets an array of all the items file names
            $data = get_filenames($data_source);
            
            //counts the number of files in the array
            $total = count($data);
                
            //if no page is specified return the first page of the array results
            if($current_page == '')
            {    
                //offset zero will return the first available page of results            
                $offset = 0;        
            }
            
            //if a page is specified, return the next set of results offset by the page number
            //multiplied by the number of results per page.
            else
            {    
                $offset = $current_page*$num_per_page;
            }
            
            //get the offset slice of the overall array.
            $data_window = array_slice($data,$offset,$num_per_page);    
            
            
            //if the remaining results are less than the capacity of the page,
            //only count the remaining results    
            if (count($data_window) < $num_per_page)
            {
                $stop = count($data_window) + $offset;
            }
            
            //else count the next page of results
            else
            {
                $stop = $offset + $num_per_page;
            }
            
            //calculate the numbe rof links required. total divided by results per page
            $div_result = $total / $num_per_page;
            
            //if php rounds the number DOWN add 1 to the count to ensure you have enough room for all your results
            if (round($div_result) < $div_result)
            {
                $pages = round($div_result) + 1;
                
            }
            else
            {
                $pages = round($div_result);
            }
            
            //if the requested page cannot display any results redirect to gallery home
            if ($current_page > $pages)
            {
                redirect($filepath);
            }
                        
            //find the start point of the link count
            $start = $current_page - $link_count;
            
            //if the link count woudl start below 0, shift it till all links are positve
            if ($start < 0)
            {
                $start = 0;
            }
            
            //calculate the total number of links to show
            $total_links_shown = ($link_count*2)+1;
            
            //if the total number of links shown, is greater than the number of
            //pages, reduce to suit
            if($total_links_shown > $pages)
            {
                $total_links_shown = $pages;
            }
            
            //if the link count would finish above the max pages, shift so only
            //valid pages will be shown
            if (($start + $total_links_shown) > $pages)
            {
                $start = $pages - $total_links_shown;
            }
            $i = 0;
            $links = '';
            
            //create link string containing the requested number of links
            while (($i < $total_links_shown) AND  ($start < $pages))
            {    
                    $links = $links.anchor($filepath.$start,($start+1)).$link_seperator;
                    $i ++;
                    $start ++;
            }
            
            
            //if not the last page, show the next link
            if($current_page != ($pages - 1))
            {
                $next = $current_page + 1;
                $links = $links.anchor($filepath.$next,$next_symbol);
            }
            
            //if not the first page, show the previous link
            if($current_page != '')
            {
                if ($current_page != 0)
                {
                    $prev = $current_page - 1;
                    $prepend = anchor($filepath.$prev,$prev_symbol).$link_seperator;
                    $links = $prepend.$links;
                }    
            }
            
            //the file count start point
            $output['start'] = $offset + 1;    
            
            //the file count stop point
            $output['stop'] = $stop;
            
            //The link string
            $output['links'] = $links;
            
            //The current page
            $output['currentpage'] = $current_page + 1;
            
            //output the total number of pages
            $output['pagecount'] = $pages;
            
            //output the total number of results
            $output['total'] = $total;
            
            //The retrieved data
            $output['filelist'] = $data_window;
                    
            return $output;
#4

[eluser]n0xie[/eluser]
Why not use the Pagination Library which is supplied by CI? It doesn't make any assumptions about having to use a Database. It just takes care of the whole 'paging' part. The logic what should be shown or where it gets it's input from is up to you...
#5

[eluser]davidbehler[/eluser]
- Create a file in application/helpers, e.g. pagination_helper.php
- In that file you put your function:
Code:
function do_pagination()
{
            $ci = & get_instance();
            $ci->load->helper('file');
....
            return $output;
}
(I had to shorten the function as I was running out of characters Big Grin)
- In your controller load the helper and use the function
Code:
$this->load->helper('pagination');

echo do_pagination();

Obviously I don't know what arguments the function has but I think you can fill in the blanks and work with this.




Theme © iAndrew 2016 - Forum software by © MyBB