Jump to content
Larry Ullman's Book Forums

Recommended Posts

View_users script shows the concept of pagination. It is not working as it should be on my browser. It shows first 10 records on the first load, and also shows navigation button. But when I click to see the second page, it shows next rows plus previous rows too. I have total 17 records in the table, so on the second page it shows all 17 records. And this second page does not show Previous button either.

I thought may be I made some mistake, so I copied the entire script from this site's downloads, and it is still shows the same result.

Link to comment
Share on other sites

  • 2 weeks later...

Sounds like an issue with the LIMIT BY clause in the query.

 

The LIMIT BY clause requires two values to properly paginate results.

 

I recommend checking Larry's book again for the details.

Thanks for your reply.

Now, I tried it again and I have another problem. I am trying to list out books from database, on page by page. The first page shows perfect result, 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

I think this is because you put

require_once(MYSQL);

in the first "else" (need to determine the number of pages), whereas you also need the bdd connection if the number of pages is in the GET array.

 

Try moving the bdd connection to the top of your script, just before the first "if".

 

I hope this helps,

  • Upvote 1
Link to comment
Share on other sites

Yes, HartleySan, I posted both of them at the same time, as I was in doubt that whether anyone would look at this in reply. But, from now onwards I'll take care and post a question in one forum only, as a new thread. thanks again.. Right now I'm trying to configure SMTP server and searching for that thread on this site.. The search result says, no topics found..

Link to comment
Share on other sites

 Share

×
×
  • Create New...