PHP For Loop Question

Discussion in 'PHP' started by tdd1984, Mar 26, 2008.

  1. #1
    Hey guys I have a for loop I'm using with my pagination, but for some odd reason when I hit the next button the 1 dissappears and I don't want the 1 to dissapear until it leaves page 5 similar to what Google does.

    	for($ctr = 1; $ctr <= 5; $ctr++) // show ($show) links
    				{
    			
    					if($this->pPageID > $this->pRecord){ // if p is greater then totalpages then display nothing
    						echo "";
    					}
    
    					else{
    
    						if(isset($concat)){
    							if($this->pPageID == $pid)
    							$link .=  "&nbsp;<a href=\"$url?pid=".$this->pPageID."$concat\"><b>(".$this->pPageID.")</b></a>";
    							else
    							$link .= "&nbsp;<a href=\"$url?pid=".$this->pPageID."$concat\">(".$this->pPageID.")</a>";
    						}else{
    
    							if($this->pPageID == $pid)
    							@$link .=  "&nbsp;<a href=".$url."?pid=".$this->pPageID."><b>(".$this->pPageID.")</b></a>";
    							else
    							$link .= "&nbsp;<a href=".$url."?pid=".$this->pPageID.">(".$this->pPageID.")</a>";
    						}
    					}
    					
    				$this->pPageID++; 
    					
    				}
    PHP:
    Maybe you guys can help me out on this I just can't seem to get it working properly. Like right now when I hit the next button it goes to 2,3,4,5,6 which is cool, but I want it to scan threw the numbers more in order like 1,2,3,4,5 with out 1 disappearing until I leave page 5?

    If I confused you just ask :)
     
    tdd1984, Mar 26, 2008 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    you should put an index, anyways you use for loop its ok.

    put all variables in dynamic..

    compute for the max pages and divide it by the record limit per page

    
    // here are my variables included
    $searchtag = $_GET['keyword']; // User Defined Input	
    $category = $_GET['category']; // Category	
    $options = $_GET['options'];   // Options
    $pageLimit = 5; // Pagination Limit of Pages ie. 1-5, 6-10, 11-15
    
    function paging($recordCount,$recordLimit) {
    	$a = $recordCount/$recordLimit;
    	$b = explode('.',$a);
    	if($b[1]>0)
    	{
    		$b[0] = $b[0]+1;
    	}
    		return $b[0];
    }
    if(!isset($_GET['limitrecords']))
    {
          $limitrecords = 10; // records per page 
    }
    else
    { 
          $limitrecords = $_GET['limitrecords']; // records per page 
    } 
    
    $pagination = paging($rows,$limitrecords);
    $currrecord = $pagenumber+1;
    $maxrecord = ($currrecord+$limitrecords)-1;
    
    if($pageLimit>$pagination) 
    { 
    	$pageLimit=$pagination; 
    	}
    	$paginationPage = '1';	
    	$minpage = 1;
    	$maxpage = $pageLimit;
    	}
    	else
    	{
    		if($pageLimit>$pagination) 
    		{ 
    			$pageLimit=$pagination; 
    		}
    		$paginationPage = $_GET['pagenumber'];
    		$pagesx = paging($paginationPage,$pageLimit);
    		$minpage = ($pageLimit * $pagesx)-$pageLimit+1;
    		$maxpage = ($minpage + $pageLimit) - 1;
    		if($maxpage>$pagination) 
    		{ 
    			$maxpage = $pagination; 
    		}
    	}
    }
    
    PHP:
    hope this helps.. but this is the main logic in putting page numbers
     
    bartolay13, Mar 26, 2008 IP
  3. tdd1984

    tdd1984 Well-Known Member

    Messages:
    2,357
    Likes Received:
    42
    Best Answers:
    0
    Trophy Points:
    150
    #3


    What do you mean the main logic is putting in the page numbers?

    I think I get what your saying?

    Well everything is working fine, but it don't scroll threw the numbers correctly for example if I hit the next button it shows 2,3,4,5,6 when it should show 1,2,3,4,5 and just make 2 bold, but still show 1, do you see what I'm saying?
     
    tdd1984, Mar 26, 2008 IP
  4. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #4
    oh ok.. my mistake..

    before executing the for loop(the one that displays page numbers)
    you must compute for the current page (in your example is 2) divided by the maximum limit of page number (as you said only 5 page numbers)
    in my example i execute the method in my function
    
    function paging($recordCount,$recordLimit) 
    {    
         $a = $recordCount/$recordLimit;    
         $b = explode('.',$a);    
    if($b[1]>0)    
    {        
         $b[0] = $b[0]+1;    
    }        
    return $b[0];
    }
    $pagination = paging($rows,$limitrecords);
    
    PHP:
    try to put your variables in the parameter of the paging function ()
    echo the result of the function,

    in your example
    2 as the current page
    5 as the limit of page


    your answer multiply by your limit of page equals to your maximum page

    $maxpage = $pagination * $limitrecords;

    on the other hand, handling your minimum page number, the same logic as getting the max page number
     
    bartolay13, Mar 26, 2008 IP