MySQL overflow and automatic pages

Discussion in 'PHP' started by deleted-account, Aug 23, 2008.

  1. #1
    I would like to pull data from a table and limit the amount of data shown to 10 per page.

    Now running this query:
    SELECT * FROM blog_main ORDER BY date LIMIT 10

    Will only allow 10 to show up, I would like the overflow meaning the others to also be displayed but on other pages such as:

    blog.php?page=2

    This page would display the rest and if there was still more a page 3 would be added.

    How can this be achieved?
     
    deleted-account, Aug 23, 2008 IP
  2. ForumJoiner

    ForumJoiner Active Member

    Messages:
    762
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    83
    #2
    ForumJoiner, Aug 23, 2008 IP
  3. deleted-account

    deleted-account Active Member

    Messages:
    655
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    85
    #3
    The files are useless to me as it is Wordpress, but the steps are helpful. Thanks!
     
    deleted-account, Aug 24, 2008 IP
  4. ForumJoiner

    ForumJoiner Active Member

    Messages:
    762
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    83
    #4
    ForumJoiner, Aug 24, 2008 IP
    Ali Razaqpur likes this.
  5. deleted-account

    deleted-account Active Member

    Messages:
    655
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    85
    #5
    Ahah that would have been helpful a little while ago. It's alright though I got it anyways.
     
    deleted-account, Aug 24, 2008 IP
  6. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    <?php
    		$var = @$_GET['q'] ;
    	$trimmed = (trim($var));
    	//   $var2 = @$_GET['Submit'] ;
    	// $trim   = (trim($var2));
      //\"$targetpage?page=$lpm1&s=$trimmed&Submit=$trim\"
      
    
    	$tbl_name="get";		//your table name
    	// How many adjacent pages should be shown on each side?
    	$adjacents = 1;
    	
    	/* 
    	   First get total number of rows in data table. 
    	   If you have a WHERE clause in your query, make sure you mirror it here.
    	*/
    	if(empty($var)){
    	$query = "SELECT COUNT(*) as num FROM $tbl_name";
    	}else{
    	$query = "SELECT COUNT(*) as num FROM $tbl_name where `name` like \"%$trimmed%\"";
    	}
    	$total_pages = mysql_fetch_array(mysql_query($query));
    	$total_pages = $total_pages[num];
    	
    	/* Setup vars for query. */
    	$targetpage = "index.php"; 	//your file name  (the name of this file)
    	$limit = 30; 								//how many items to show per page
    	$page = $_GET['page'];
    	if($page) 
    		$start = ($page - 1) * $limit; 			//first item to display on this page
    	else
    		$start = 0;								//if no page var is given, set start to 0
    	
    	/* Get data. */
    	if(strlen($var) <= 3 && isset($var) && strlen($var) != 0){
    	echo "SEARCH MUST BE 3 OR MORE";
    	$sql = "select * from $tbl_name LIMIT $start, $limit";	
    	}else{
    	$sql = "select * from $tbl_name where `name` like \"%$trimmed%\" LIMIT $start, $limit";	
    	}
    	
    	$result = mysql_query($sql);
    	$num_rows = mysql_num_rows($result); // showing amount on page
    
    	/* Setup page vars for display. */
    	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
    	$prev = $page - 1;							//previous page is page - 1
    	$next = $page + 1;	
    	$lastpage = ceil($total_pages/$limit);	
    	//lastpage is = total pages / items per page, rounded up.
    	$lpm1 = $lastpage - 1;						//last page minus 1
    	
    	/* 
    		Now we apply our rules and draw the pagination object. 
    		We're actually saving the code to a variable in case we want to draw it more than once.
    	*/
    
    
      
    	$pagination = "";
    	if($lastpage > 1)
    	{	
    		$pagination .= "<div id=\"pages\">";
    		//previous button
    		if ($page > 1) 
    			$pagination.= "<li><a href=\"$targetpage?page=$prev&q=$trimmed\">« previous - </a></li>";
    		else
    			$pagination.= "<li class=\"nolink\">« previous - </li>";	//DONE
    		
    		//pages	
    		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
    		{	
    			for ($counter = 1; $counter <= $lastpage; $counter++)
    			{
    				if ($counter == $page)
    					$pagination.= "<li class=\"nolink\">$counter - </li>";
    				else
    					$pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter - </a></a>";		//DONE			
    			}
    		}
    		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
    		{
    			//close to beginning; only hide later pages
    			if($page < 1 + ($adjacents * 2))		
    			{
    				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "<li class=\"current\">$counter - </li>";
    					else
    						$pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter</a></li>";					
    				}
    				$pagination.= "<li class=\"nolink\">...</li>";
    				$pagination.= "<li><a href=\"$targetpage?page=$lpm1&q=$trimmed&Submit=$trim\">$lpm1</a></li>";
    				$pagination.= "<li><a href=\"$targetpage?page=$lastpage&q=$trimmed&Submit=$trim\">$lastpage</a></li>";		
    			}
    			//in middle; hide some front and some back
    			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
    			{
    				$pagination.= "<li><a href=\"$targetpage?page=1&q=$trimmed&Submit=$trim\">1</a></li>";
    				$pagination.= "<li><a href=\"$targetpage?page=2&q=$trimmed&Submit=$trim\">2</a></li>";
    				$pagination.= "<li class=\"nolink\">...</li>";
    				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "<li class=\"current\">$counter - </li>";
    					else
    						$pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter</a></li>";					
    				}
    				$pagination.= "<li class=\"nolink\">...</li>";
    				$pagination.= "<li><a href=\"$targetpage?page=$lpm1&q=$trimmed&Submit=$trim\">$lpm1</a></li>";
    				$pagination.= "<li><a href=\"$targetpage?page=$lastpage&q=$trimmed&Submit=$trim\">$lastpage</a></li>";		
    			}
    			//close to end; only hide early pages
    			else
    			{
    				$pagination.= "<li><a href=\"$targetpage?page=1&q=$trimmed&Submit=$trim\">1</a></li>";
    				$pagination.= "<li><a href=\"$targetpage?page=2&q=$trimmed&Submit=$trim\">2</a></li>";
    				$pagination.= "<li class=\"nolink\">...</li>";
    				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "<li class=\"current\">$counter</li>";
    					else
    						$pagination.= "<li><a href=\"$targetpage?page=$counter&q=$trimmed&Submit=$trim\">$counter</a></li>";					
    				}
    			}
    		}
    		
    		//next button
    		if ($page < $counter - 1) 
    			$pagination.= "<li><a href=\"$targetpage?page=$next&q=$trimmed&Submit=$trim\">next »</a></li>";
    		else
    			$pagination.= "<li class=\"nolink\">next »</li>";
    		$pagination.= "</div>\n";		
    	}
    ?>
    
    PHP:
    <?= $pagination // where you want the pages to be printed 
    ?>
    PHP:
    This code works well but its been modified for some of my scripts and sites... it will give you an example thos of how to do this.. :)
     
    cornetofreak, Aug 24, 2008 IP
    Ali Razaqpur likes this.
  7. deleted-account

    deleted-account Active Member

    Messages:
    655
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    85
    #7
    Thanks corneto already taken care of though :)
     
    deleted-account, Aug 24, 2008 IP