sonal Posted July 10, 2011 Share Posted July 10, 2011 Thanks for the great book. Now, I'm a newbee to PHP and trying to learn pagination concept. I got another problem. I am trying to list out books from database, on page by page. The first page shows perfect result (list of 5 books), with navigation buttons. When I click 2 (or any next page), the script throws an error.. saying, undefined variable dbc. If it gets "dbc" variable for the first 5 records, how come it becomes undefined for next pages? Here is my code: <?php #Script 1.6 - view_books.php //This script retrieves all books from the books table. //This version paginates the results. $page_title = 'View all books'; require_once('../includes/configuration.inc.php'); include('../includes/headpart.html'); echo '<h1>Books List</h1>'; //Number of records to show per page. $display = 5; //Determine how many pages are there... if(isset($_GET['p']) && is_numeric($_GET['p'])) { //Already been determined. $pages = $_GET['p']; } else { //Need to be determine. require_once(MYSQL); //count the number of records: $q = "SELECT COUNT(bookid) 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 one 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; } //Make the query. $q = "SELECT bookid, title, author, price, category FROM books order by title ASC LIMIT $start, $display"; $r = @mysqli_query($dbc, $q); //Table header: echo '<table align="center" cellspacing="0" cellpadding="5" width="75%"> <tr> <td align="left"><b>Title</b></td> <td align="left"><b>Author</b></td> <td align="left"><b>Price</b></td> <td align="left"><b>Category</b></td> </tr>'; //Fetch and print all records: $bg = '#eeeeee'; //set the initial background color. if(mysqli_num_rows($r) > 0 ) { while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); //Switch the background color. echo '<tr bgcolor="' . $bg .'"> <td align="left">' . $row['title'] . '</td> <td align="left">' . $row['author'] . '</td> <td align="left">' . $row['price'] . '</td> <td align="left">' . $row['category'] . '</td> </tr>'; } } //End of while loop. echo '</table>'; 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 is not the first page, make a previous button: if($current_page != 1) { echo '<a href="view_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="view_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="view_books.php?s=' . ($start + $display) . '&p=' . $pages . '">Next</a>'; } echo '</p>'; } ?> Link to comment Share on other sites More sharing options...
HartleySan Posted July 11, 2011 Share Posted July 11, 2011 Do you get an error when you click "Previous" or "Next"? 1 Link to comment Share on other sites More sharing options...
Recommended Posts