Roman Posted January 22, 2013 Share Posted January 22, 2013 Hello all. I am a relative newbie to php and mysql but have been progressing through book with great results up until i have hit a wall with a pagination script. The scripts used are a combination of 9.4 - paginate view users and chapter 17 ecommerce from the PHP 6 and MySQL 5 book. The issue involves the script not creating further links for additional pages should the results returned exceed the $display variable. I don't get any next or previous links at all and for the life of me cannot see why. The page displays with whatever the $display number is set out but no links to additional results. My code is below but the site is local so cannot show you direct links. <?php # browse_books.php // This page displays the available books (products). // Set the page title and include the HTML header: $page_title = 'Browse Books'; include ('includes/header.html'); require_once ('../mysqli_connect.php'); // Number of records to show per page: $display = 15; // Determine how many pages there are... if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined. $pages = $_GET['p']; } else { // Need to determine. // Count the number of records: $q = "SELECT COUNT (book_id) FROM books"; $r = @mysqli_query ($dbc, $q); $row = @mysqli_fetch_array ($r, MYSQLI_NUM); $records = $row[0]; // Calculate the number of pages... if ($records > $display) { // More than 1 page. $pages = ceil ($records/$display); } else { $pages = 1; } } // End of p IF. // Determine where in the database to start returning results... if (isset($_GET['s']) && is_numeric($_GET['s'])) { $start = $_GET['s']; } else { $start = 0; } // Default query for this page: $q = "SELECT authors.author_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS author, book_name, price, description, book_id FROM authors, books WHERE authors.author_id = books.author_id ORDER BY authors.last_name ASC, books.book_name ASC LIMIT $start, $display"; // Are we looking at a particular author? if (isset($_GET['aid']) && is_numeric($_GET['aid']) ) { $aid = (int) $_GET['aid']; if ($aid > 0) { // Overwrite the query: $q = "SELECT authors.author_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS author, book_name, price, description, book_id FROM authors, books WHERE authors.author_id = books.author_id AND books.author_id = $aid ORDER BY books.book_name"; } } // Display all the books, linked to URLs: $r = mysqli_query ($dbc, $q); while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) { // Display each record: echo "<div class=\"book_holder\"> <h1><a href=\"view_book.php?pid={$row['book_id']}\">{$row['book_name']}</a></h1> <h2><a href=\"browse_books.php?aid={$row['author_id']}\">{$row['author']}</a></h2> <p>"; $newstring = substr("{$row['description']}",0,100); echo $newstring; echo "....<a href=\"view_book.php?pid={$row['book_id']}\"> more info</a>"; echo "</p> <h2>£{$row['price']}</h2> </div>"; } // End of while loop. mysqli_free_result ($r); mysqli_close($dbc); // Make the links to other pages, if necessary. if ($pages > 1) { // Add some spacing and start a paragraph: echo '<br /><p>'; // Determine what page the script is on: $current_page = ($start/$display) + 1; // If it's not the first page, make a Previous button: if ($current_page != 1) { echo '<a href="browse_books.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> '; } // Make all the numbered pages: for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="browse_books.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> '; } else { echo $i . ' '; } } // End of FOR loop. // If it's not the last page, make a Next button: if ($current_page != $pages) { echo '<a href="browse_books.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; // Close the paragraph. } // End of links section. include ('includes/footer.html'); ?> Thanks in advance Link to comment Share on other sites More sharing options...
HartleySan Posted January 22, 2013 Share Posted January 22, 2013 Have you tried echoing you $pages variable to see what value is actually stored in it? Unless the $pages variable is greater than 1, then none of that information will be output to the screen. I'm guessing that your $pages variable is not being properly created for one reason or another. 2 Link to comment Share on other sites More sharing options...
Roman Posted January 22, 2013 Author Share Posted January 22, 2013 Thanks for quick reply HartleySan. Have just echoed $pages variable and it returns 1 so obviously that is the problem. Will have a play and try and resolve before posting back. Link to comment Share on other sites More sharing options...
Roman Posted January 23, 2013 Author Share Posted January 23, 2013 Rebuilt the script from scratch and it worked perfectly. Thanks for pointing me inright direction. 1 Link to comment Share on other sites More sharing options...
HartleySan Posted January 23, 2013 Share Posted January 23, 2013 Excellent! Glad you were able to solve it on your on. Link to comment Share on other sites More sharing options...
Recommended Posts