0

My PHP pagination code is quite messy. I use something like this:

$display = 8;
       
       if(isset($_GET['p'])&&is_numeric($_GET['p'])) { 
                $pages = $_GET['p'];
        } else { 
            $q = "SELECT COUNT(image_id) FROM images";
            $r = mysqli_query($dbc, $q);
            $ps = mysqli_fetch_array($r, MYSQL_NUM);
            $broj_redova = $ps[0];
            
            if($broj_redova <= 8) { 
                    $pages = 1;
            } else {
                    $pages = ceil($broj_redova / $display);
            }
            
    }
    
       if(isset($_GET['s'])&&is_numeric($_GET['s'])) { 
            $start = $_GET['s'];
       } else { 
            $start = 0;
       }



    // here is the query 
    $q = "SELECT * FROM images LIMIT $start, $display";


       if($pages > 1) {  // make pagination only if there is more than a one page
            
                    $current_page = ($start/$display) + 1;
                    
                    // If it's not the first page, make previous button: *****************************
                    if($current_page != 1) { 
                            echo '<a href="gallery.php?s='.($start - $display).'&p='.$pages.'">Previous</a>';
                    }
                    // ****************************************************************************    
                    
                for($i = 1; $i <= $pages; $i++) { 
                    if ($i != $current_page) { 
                            echo '<a href=gallery.php?s='.(($display*($i - 1))).'&p='.$pages.'">'.$i.'</a>';
                    } else {
                            echo $i . ' ';
                    }
                    
                } // end of foor loop
                    
                    
                    // if it's not the last page, make next button =***********************************
                
                    if($current_page != $pages) { 
                            echo '<a href="gallery.php?s='.($start + $display).'&p='.$pages.'">Next</a>';
                    }
                    // *****************************************************************************
            
            }

I wonder if you guys program each pagination code when it's needed or you have something ready made, so when you need you just add it?

5
Contributors
4
Replies
9
Views
5 Years
Discussion Span
Last Post by subrata_ushasi
0

I use this for sorting the pages and putting the info out

$per_page = 5;
$pages_query = mysql_query("SELECT COUNT('ID') FROM serch".$queryOp);
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = "SELECT * FROM serch".$queryOp." LIMIT $start, $per_page";
$query = mysql_query($query);
while($row = mysql_fetch_assoc($query)){
	$id = $row['ID'];
	$title = $row['title'];
	$description = $row['description'];
	$keywords = $row['keywords'];
	$link = $row['link'];

then for changing pages i use

if($pages >= 1 && $page <= $pages) {
	 for ($x=1; $x<=$pages; $x++) {
		echo ($x == $page) ? '<a href="?page='.$x.'&search='.$_GET['search'].'">'.$x.'</a> </strong> ' : '<a href="?page='.$x.'&search='.$_GET['search'].'">'.$x.'</a> ';
	}
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.