paging script

Discussion in 'PHP' started by mahiraj, Nov 12, 2007.

  1. #1
    Hi all,

    I am new in php.
    Can anybody provide me the sample code of paging..in php?///////
     
    mahiraj, Nov 12, 2007 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    Find on google

    search for php paging script

    and you will get many

    Look on forums links on google

    Regards

    Alex
     
    kmap, Nov 12, 2007 IP
  3. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #3
    <?php 
    
    echo "Hello World";
    ?>
    PHP:
    hehehehe
     
    bartolay13, Nov 12, 2007 IP
  4. Mooseman

    Mooseman Peon

    Messages:
    453
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ^ Yeh, that's a good way to start :D

    Well, do what kmap said. Or search for tutorial instead of script, so that you might learn something aswell xD
     
    Mooseman, Nov 12, 2007 IP
  5. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Im feeling generous so here is a pagination function ive built for my framework

    // Generates pagination links for a result set
    	// Home is for the first page so we dont get done for duplicate content like page/0/
    	function getPagination($count, $page, $limit, $url, $home=NULL, $show=NULL)
    	{
    		$show = (empty($show)) ? $this->settings['paginationLinkCount'] : $show;
    
    		// There are no records so pagination is empty
    		if ($count == 0)
    		{
    			return "";
    		}
    
    		$half = ($show/2); // links to show before/after current page
    		$pages = ceil($count/$limit); // total pages altogether
    		$page = ($page > $pages) ? $pages : $page;
    
    		if ($page > $half && $pages > $show)
    		{
    			// Determine which links to show and where to show them (fucking complex!)
    			$start = floor(($page - $half) + 1);
    			$end = floor($page + $half);
    			$end = ($end > $pages) ? $pages : $end;
    			$start = (($end - $start) < ($show - 1) && $show < $pages) ? $end - ($show - 1) : $start;
    		}
    		else
    		{
    			$start = 1;
    			$end = ($pages <= $show) ? $pages : $show;
    		}
    
    		ob_start();
    		$link = ($page == 2) ? $home : $url.($page - 1)."/";
    		$previous = ($page == 1) ? "<span class=\"inactive\">« Previous</span> " : "<a href=\"".$link."\">« Previous</a> ";
    		$dots = ($start > 1) ? "<a href=\"".$home."\">1</a> ... " : "";
    
    		echo $previous;
    		echo $dots;
    
    		$current = $start;
    		while ($current <= $end)
    		{
    			$link = ($current == 1) ? $home : $url.$current."/";	
    
    			if ($current == $page)
    			{
    				echo "<span class=\"active\">".$current."</span> ";
    			}
    			else
    			{
    				echo "<a href=\"".$link."\">".$current."</a> ";
    			}
    
    			$current++;
    		}
    
    		$next = ($page == $pages) ? "<span class=\"inactive\">Next »</span> " : "<a href=\"".$url.($page + 1)."/\">Next »</a> ";
    		$dots = ($end < $pages) ? " ... <a href=\"".$url.$pages."/\">".$pages."</a> " : "";
    
    		echo $dots;
    		echo $next;
    
    		$links = ob_get_contents();
    		ob_end_clean();
    
    		return $links;
    	}
    
    PHP:
    The input parameters are

    $count = total number of items
    $page = the current page you are on
    $limit = how many items to show per page
    $url = the url to place before the pagination variables
    $home = the url to show on the first page so you dont get duplicates. This is needed if you want to have a default sort links option. like cat/ascending/0/ and your default page is just cat/ sorted as ascending by default.
    $show = how many pagination links to show (i have a settings parameter as default but change to whatever number you like

    It will output something like

    << Previous - 1 ... 10 - 11 - 12 - 13 - 14 - 15 ... 20 Next >>

    And will also deactivate previous and next links if you cant go any higher or lower (ie. are on the first or last page)

    Hope this helps :)
     
    tonybogs, Nov 12, 2007 IP
  6. ~*77*~

    ~*77*~ Peon

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Are you looking to pull from a database? Or flat files?
     
    ~*77*~, Nov 12, 2007 IP
  7. mahiraj

    mahiraj Guest

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi,

    I want to pull from database..........
     
    mahiraj, Nov 12, 2007 IP
  8. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    How would you adapt this with a database.

    say

    I have got a database

    --
    -- Table structure for table `filedetails`
    --


    `id` int(5) NOT NULL auto_increment,
    `type` char(1) NOT NULL default '0',
    `title` varchar(200) default NULL,
    `description` text NOT NULL,
    `links` text NOT NULL,
    PRIMARY KEY (`id`)

    thanks


     
    baris22, Nov 13, 2007 IP
  9. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    JavaScript solution:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Any Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    
    	var maxItems = 21;  // minimum is 5
    	var consecItems = 10; // minimum is 5, this is the number of integers appearing in between Previous and Next
    	var path = "./products/product_";  // the path and partial file name of your data documents, e.g., product_1.html;
    	var ext = ".html" // the file extension of your data files, e.g., .php, .pl, .asp, .aspx
    
    	function init(){
    
    		var activeURL = "";
    		var endItem = "";
    		var remaining = "";
    		var nextSet = "";
    		var index = 0;
    		var nList = document.getElementById('itemLinks');
    		for (i=0; i<consecItems+2; i++)
    			{
    			 var nLink = document.createElement('span');
    			 nLink.onclick = function()
    				{
    				 var pageList = nList.getElementsByTagName('span');
    				 if (this.firstChild.data == "Next")
    					{
    					 if (pageList[pageList.length-2].firstChild.data == maxItems && pageList[pageList.length-2].className == "activePage")
    						{
    						 return false;
    						}
    	 				 if (pageList[pageList.length-2].className != "activePage")
    					 	{
    					 	 for (i=1; i<consecItems+1; i++)
    							{
    					 	 	  if (pageList[i].className == "activePage")
    								{
    								 pageList[i].className = "availPage";
    								 index = i+1;
    								}
    							}
    						 if (pageList[index].firstChild.data == "-")
    							{
    							 pageList[index-1].className = "activePage"
    							 return false;
    							}
    						 pageList[index].className = "activePage";							
    					 	}
    				 	 else	{
    						 endItem = pageList[pageList.length-2].firstChild.data;
    					 	 remaining = maxItems - endItem;
    						 if (remaining >= consecItems)
    							{
    							 nextSet = consecItems+1;
    							} 
    						 else 	{
    							 nextSet = remaining + 1;
    							}
    						 for (i=1; i<nextSet; i++)
    							{
    							 pageList[i].firstChild.data = Number(endItem) + i;
    							}
    						 for (i=1; i<consecItems+1; i++)
    							{
    							 if (pageList[i].firstChild.data <= Number(endItem))
    								{
    								 pageList[i].firstChild.data = "-";
    								}
    							}
    						 pageList[1].className = "activePage";
    						 pageList[pageList.length-2].className = "availPage";
    						}
    					}
    				 else if (this.firstChild.data == "Previous")
    					{
    					 if (pageList[1].firstChild.data == 1 && pageList[1].className == "activePage")
    						{
    						 return false;
    						}
    	 				 if (pageList[1].className != "activePage")
    					 	{
    					 	 for (i=1; i<consecItems+1; i++)
    							{
    					 	 	 if (pageList[i].className == "activePage")
    								{
    								 pageList[i].className = "availPage";
    								 index = i-1;
    								}
    							}
    						 pageList[index].className = "activePage";							
    					 	}
    					 else	{
    						 endItem = pageList[1].firstChild.data;
    						 remaining = endItem - 1;
    						 var nSet = ((remaining / consecItems) - 1) * consecItems;
    						 for (i=1; i<consecItems+1; i++)
    							{
    							 pageList[i].firstChild.data = i + nSet; 
    							}
    						 pageList[1].className = "availPage";
    						 pageList[pageList.length-2].className = "activePage";
    						}
    					}
    				 else	{
    					 if (this.firstChild.data == "-")
    						{
    						 return false;
    						}
    					 for (i=1; i<consecItems+1; i++)
    						{
    						 if (pageList[i].className == "activePage")
    							{
    							 pageList[i].className = "availPage";
    							}
    						}
    					 this.className = "activePage";
    					}
    				 for (i=1; i<consecItems+1; i++)
    					{
    					 if (pageList[i].className == "activePage")
    						{
    						 activeURL = path + pageList[i].firstChild.data + ext;
    						}
    					}
    
    				 //alert(activeURL);
    				 // the following loads each selected document
    				 window.frames['docDisplay'].location.replace(activeURL);
    
    				}
    			 nLink.className = "availPage";
    			 var nText = document.createTextNode(i);
    			 nLink.appendChild(nText);
    			 nList.appendChild(nLink);
    			}
    		var nItems = nList.getElementsByTagName('span');
    		nItems[1].className = "activePage";
    		nItems[0].firstChild.data = "Previous";
    		nItems[nItems.length-1].firstChild.data = "Next";
    
    		// the following loads the initial document, product_1.html
    		window.frames['docDisplay'].location.href = path + "1" + ext;
    	}
    
    	onload=init;
    	
    </script>
    <style type="text/css">
    
    	 body {background-color:#eae3c6;margin-top:60px}
    	#itemLinks {background-color:#f0fff0;font-family:times;font-size:12pt;border:1px solid black;text-align:center;width:600px;margin:auto;position:relative;top:50px}
    	#docDisplay {background-color:#ffdab9;display:block;width:600px;height:400px;margin:auto}	
    	.availPage {color:#0000ff;cursor:pointer;padding:5px}
    	.activePage {color:#000000;padding:5px;text-decoration:underline;cursor:auto}
    	 
    </style>
    </head>
    	<body>
    		<iframe name="docDisplay" id="docDisplay" scrolling="auto"></iframe>
    		<div id="itemLinks"></div>
    	</body>
    </html>
    
    Code (markup):
     
    Mike H., Nov 13, 2007 IP