pagenation help

Discussion in 'PHP' started by izlik, Mar 14, 2014.

  1. #1
    Hello

    I have this code bellow i started to make into a "pagenation". What im having trouble with right now is creating the "next" and "previous" links, anyone that can help me cause i cant get it to work :/

    
    <?php
    
    echo '<div id="block" border="1" width="200" style="float:center">';
    $i = 0;
    $perpage = 10;
    $currentpage = 0;
    $sql = "SELECT * FROM table";
    $numRows = mysql_num_rows(mysql_query($sql));
    $lastpage = ceil($numRows / $perpage);
    $getquery = mysql_query("$sql ORDER by ID LIMIT $currentpage, $perpage");
    while($rows=mysql_fetch_assoc($getquery)){
    
        $id=$rows['id'];
        echo '<a href="/index.php?image='. $id .'">
            <img src="/thumbs/'. $id .'.jpg" width="100" height="100"  alt="" />
        </a>';
    
        $i++;
        if($i == 10) {
            echo '<br />';
            echo '<br />';
            $i = 0;
        }
    }
    echo '</div>';
    
    $page = (isset($_GET['page'])) ? intval($_GET['page']) : 1;
    
    
    ?>
    PHP:
     
    izlik, Mar 14, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Took me a moment to figure out what a pagenation was... but if you want pagination, here, try mine:
    http://www.cutcodedown.com/methods/pagination/

    function theme_pagination($urlRoot, $current, $perPage, $totalArticles) {
    
    	if ($totalArticles > $perPage) {
    	
    		$urlRoot .= ((strpos($urlRoot,'?') === false) ? '?' : '&') . 'start=';
    		
    		echo '
    		<div class="pagination">
    			Pages:
    			<ul>';
    		
    		if (($currentPage = floor($current / $perPage)) > 0) echo '
    				<li><a href="', $urlRoot, '0">First</a></li>
    				<li><a href="', $urlRoot, $currentPage - 1, '" title="previous">&laquo;</a></li>';
    		
    		if (($lastPage = floor(($totalArticles - 1) / $perPage)) > 9) {
    			$counter = ($currentPage < 6) ? 0 : $currentPage - 5;	
    			if (($endPage = $counter + 10) > $lastPage) {
    				$endPage = $lastPage;
    				$counter = $endPage - 10;
    			}
    		} else {
    			$counter = 0;
    			$endPage = $lastPage;
    		}
    	
    		while ($counter <= $endPage) {
    			echo '
    				<li>', (
    					($noAnchor = ($counter == $currentPage)) ?
    					'<span>' :
    					'<a href="' . $urlRoot . $counter . '">'
    				), ++$counter, (
    					$noAnchor ?
    					'</span>' :
    					'</a>'
    				), '</li>';
    		}
    		
    		if ($currentPage < $lastPage) echo '
    				<li><a href="', $urlRoot, ++$currentPage, '" title="next">&raquo;</a></li>
    				<li><a href="', $urlRoot, $lastPage, '">Last</a></li>';
    		
    		echo '
    			</ul>
    		<!-- .pagination --></div>';
    			
    	}
    	
    } // theme_pagination
    Code (markup):
    Shows 10 'quick jumps' at a time, keeps the current selection centered (if possible), gives you first, last, previous and next links.

    Oh, and on your code -- if this is NEW code, you shouldn't be using the long deprecated and soon to disappear mysql_ functions, especially since prepared queries would work wonders on getting rid of the string additions 'for nothing' -- likewise doing select * to get the count is GROSSLY inefficient; that's what COUNT is for. Might also help if you used valid/modern markup, since DIV don't even (and never did) have WIDTH or BORDER attributes :/

    Also not sure why you have logic and double breaks inside the loop doing the job of margin on the DIV...

    Integrating to what you are doing and modernizing it, that would look something like this:
    <?php
    
    /* assumes $db is a connected PDO object */
    
    /*
    	Turning off emulation prepares increases performance and makes it so we
    	can actually use prepare with multiple LIMIT values. MAJOR bug in the 
    	default PREPARE behavior in PHP, all because some (crappy) SQL engines
    	don't support it. If you're using MYSQL use the bloody driverto handle
    	prepare/exec!
    */
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
    $paginationData = [
    	':start' : isset($_GET['page']) ? (int) $_GET['page'] : 0,
    	':limit' : 10
    ];
    
    $statement = $db->exec('
    	COUNT * FROM TABLE
    ');
    $numRows = $statement->fetchColumn();
    
    $statement = $db->prepare('
    	SELECT * FROM TABLE
    	ORDER BY id
    	LIMIT :start, :limit
    ');
    
    $statement->execute($paginationData);
    
    /* I like to put pagination above and below the data */
    
    theme_pagination(
    	'index.php?page=',
    	$paginationData[':start'],
    	$paginationData[':limit'],
    	$numRows
    );
    
    echo '
    	<div id="block">';
    
    while ($row = $statement->fetch(PDO::FETCH_ASSOC)) echo '
    		<a href="/index.php?image=', $row['id'] ,'">
    			<img src="/thumbs/', $row['id'], '.jpg" alt="" />
    		</a>';
    }
    echo '
    	</div>';
     
    theme_pagination(
    	'index.php?page=',
    	$paginationData[':start'],
    	$paginationData[':limit'],
    	$numRows
    );
     
    ?>
    Code (markup):
    Hope this helps.
     
    Last edited: Mar 14, 2014
    deathshadow, Mar 14, 2014 IP
  3. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #3
    salmanshafiq, Mar 15, 2014 IP
  4. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #4
    @deathshadow, looks like a nice functional script but loading the .php.txt files breaks the server :p Just thought you might want to know.
     
    blueparukia, Mar 22, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Yeah, I've switched to running my own server again, but the bastard is fighting me every step of the way... I'm getting 500 and 403 errors across the board on **** that used to just work out of the box... I know my configuration choices are a bit out of the ordinary, but I've never had issues like this before. I can't serve anything with two . in the name without a 500 error, I can't serve .phps without a 403 error... (either normally or with my own custom rewriterule version) and I can't find anything in the configuration that would cause this.

    Half tempted to wipe it and start over.

    I've changed those to just .txt for now.
     
    deathshadow, Mar 22, 2014 IP
    blueparukia likes this.