Jump to content

[SOLVED] Pagination Problem


pocobueno1388

Recommended Posts

I am having a bit of trouble getting my pagination to work correctly as there are some obstacles I have to work around in this specific script.

 

I have the script recognizing how many results there are, and it correctly displays the amount of pages there should be for the amount of data, but when I try to go to any other page than the first, it loses the rest of the rows and doesn't display anything.

 

Here is my code, I have narrowed it down as much as possible and deleted things you don't need to see so it is easier for you to look through.

 

<?php

if (isset($_POST['step3'])) {
    
    //put post data in variables
    $level = $_POST['level'];
    $show_levels = isset($_POST['show_levels']) ? $_POST['show_levels'] : "no_all";
    $sort = isset($_POST['sort_by']) ? $_POST['sort_by'] : "fee DESC";
    $show_type = $_POST['show_type'];
    
    //START PAGINATION
    if (!isset($_GET['page'])) {
        $page = 1;
    } else {
        $page = $_GET['page'];
    }
    
    $max_results = 2;
    $from = (($page * $max_results) - $max_results);
    
    //start building query
    $query = "SELECT showID, name, fee, prize, level, run_date FROM shows WHERE type='$show_type'";
    
    if ($show_levels == "all") {
        $query .= " AND level >= '$level'";
    } else if ($show_level == "no_all") {
        $query .= " AND level = '$level'";
    }
    
    $total_results = mysql_num_rows(mysql_query($query)); //<----Get number of results for pagination
    
    $query .= " ORDER BY $sort LIMIT $from, $max_results";
    $result = mysql_query($query)or die(mysql_error());
    
    
    //Start displaying info
    if (mysql_num_rows($result) > 0) {
                
        //show page numbers
        echo '<table width="70%" align="center"><td align="right">';
        
        // Figure out the total number of pages.
        $total_pages = ceil($total_results / $max_results);
        
        // Build Page Number Hyperlinks
        echo "Page ";
        
        // Build Previous Link
        if ($page > 1) {
            $prev = ($page - 1);
            echo '<a href="shows.php?page='.$prev.'"><< Prev</a> ';
        }
        
        for ($i = 1; $i <= $total_pages; $i++) {
            if (($page) == $i) {
                echo "<b>[$i]</b> ";
            } else {
                echo '<a href="shows.php?page='.$i.'">['.$i.']</a> ';
            }
        }
        
        // Build Next Link
        if ($page < $total_pages) {
            $next = ($page + 1);
            echo '<a href="shows.php?page='.$next.'">Next>></a>';
        }
        
        echo '</td></table>';
        
        echo '<table class="dark" width="70%" align="center" cellpadding=8>';
        
        while ($row = mysql_fetch_assoc($result)) {
            //This is just where I display the data from the DB
        }
        
        echo '</table>';
        
        
    } else {
        echo "<center><i>No shows found.</i></center>";
    }

include 'footer.php';
exit;    
}

?>

 

The only thing I can think of is it is losing it's POST information when you try to go to a different page, if thats possible.

 

Also, you can see that my query is being setup in a slightly different way than just writing the whole thing out. So I wasn't sure where exactly to put this line:

$total_results = mysql_num_rows(mysql_query($query));

 

Any help would be greatly appreciated with this =] Thank in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/58005-solved-pagination-problem/
Share on other sites

???The only thing I can think of is it is losing it's POST information when you try to go to a different page, if thats possible. ???

 

post entry will be lost because u use link in the navigation for pagination? so maybe thats why it terminate the execution of the script >>if (isset($_POST['step3'])) { try to figure it out

I don't really want to use sessions, I think there could be a better way.

 

Even if I did use the get method, I would still have post data...so that wouldn't work.

 

Maybe if I used define() on each varaible? Do you think it would hold the value?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.