Please help!!!! I am a complete newbie here i am trying to put together a php mysql apache database application. I am learning the basics as i go along and have had a small measure of success, however when it comes to pagination i am a little frustrated - can you help?
can anyone suggest a simple useful script to work pretty much as is with little code change. - failing that i have come across what appears to be a nice little script, but when i run this script none of the links to PREV, NEXT or any of the page numbers actually produce anything other than page not found error - i have followed all the advice even changing the actual page id in
echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");
to the page title itself - in this case paginate.php
yet no success - any and all advice would be gratefully received -
here is the full code with the actual tips and changes that i have made--
<?php
$localhost = "localhost";
$user ="root";
$password ="";
// The first function connects to your database, the second selects a database
mysql_connect($localhost, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER");
mysql_select_db("book") or die("ERROR--CAN'T CONNECT TO DB");
$limit = 20;
// Sets how many results shown per page
$query_count = ("SELECT lastname FROM book limit 200 ");
// Sets what we want to pull from the database
// count(*) is better for large databases (thanks Greg!)
$result_count = mysql_query($query_count);
// Pulls what we want from the database
$totalrows = mysql_num_rows($result_count);
echo "$totalrows";
// This counts the number of users
if(empty($page)){ // Checks if the $page variable is empty (not set)
$page = 1; // If it is empty, we're on page 1
}
$limitvalue = $page * $limit - ($limit);
// Ex: (2 * 25) - 25 = 25 <- data starts at 25
$query = "SELECT * FROM book LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Error: " . mysql_error());
// Selects all the data from table.
// mysql_error() will print an error if one occurs.
/* Tip: The MySQL LIMIT value syntax is as follows:
LIMIT $row_to_start_at, $how_many_rows_to_return
*/
if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}
// This reads the number of rows returned
// from the result above.
$bgcolor = "#E0E0E0"; // light gray
/ Sets up one of the background colors. The color stated here will be
displayed second /
echo "<table border=1 cellSpacing=11 cellpadding=0>";
// Just a simple table
while($row = mysql_fetch_array($result)){
/* This starts the loop (a while loop). What this does is returns a row of data
to the $row array. Each time the loop restarts, the next row of data is used.
So, each pass of the loop returns a different row of data. */
// Start The Background Check
if($bgcolor == "#E0E0E0"){
$bgcolor = "#FFFFFF";
}else{
$bgcolor = "#E0E0E0";
}
/* Tip: The color you want to appear first on the list should be where the
#FFFFFF is listed. What this script does is checks to see if #E0E0E0 was used
last; if it was, then it'll use #FFFFFF. All you have to do is replace the
colors of your choice. */
echo("<tr bgcolor=".$bgcolor.">\n<td>");
// Here we start table row & table data
// Make sure your bgcolor is the $bgcolor variable
echo($row["lastname"]);
/* Tip: This is how we add the users field from our row. You can use any field
from your row: all you do is include the field's name inbetween the array
brackets. Ex: $row["field_name_here"] */
// Here we end the one section of table data, and start another.
echo "<td>" . $row [0] . "</td>";
echo "<td>" . $row [1] . "</td>";
echo "<td>" . $row [2] . "</td>";
echo "<td>" . $row [3] . "</td>";
echo "<td>" . $row [4] . "</td>";
echo "<td>" . $row [5] . "</td>";
echo "<td>" . $row [6] . "</td>";
echo "<td>" . $row [7] . "</td>";
echo "<td>" . $row [8] . "</td>";
echo "<td>" . $row [9] . "</td>";
// Prints the usersID field
echo("</td></tr>");
// Here we end the table data & table row
} /* This closes the loop. Anything after this bracket will display after the
data you've pulled from the database. */
echo("</table>");
/* While we're at it, let's close the table tag--we don't want other data
inside this table */
if($page != 1){
$pageprev = $page--;
// Fancy way of subtracting 1 from $page
echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");
/* Tip: It is a good idea NOT to use $PHP_SELF in this link. It may work,
but to be 99.9% sure that it will, be sure to use the actual name of the file
this script will be running on. Also, the adds a space to the end of
PREV, and gives some room between the numbers. */
}else
echo("PREV".$limit." ");
// If we're on page 1, PREV is not a link
$numofpages = $totalrows / $limit;
/* We divide our total amount of rows (for example 102) by the limit (25). This
will yield 4.08, which we can round down to 4. In the next few lines, we'll
create 4 pages, and then check to see if we have extra rows remaining for a 5th
page. */
for($i = 1; $i <= $numofpages; $i++){
/* This for loop will add 1 to $i at the end of each pass until $i is greater
than $numofpages (4.08). */
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF&page=$i\">$i</a> ");
}
/* This if statement will not make the current page number available in
link form. It will, however, make all other pages available in link form. */
} // This ends the for loop
if(($totalrows % $limit) != 0){
/ The above statement is the key to knowing if there are remainders, and it's
all because of the %. In PHP, C++, and other languages, the % is known as a
Modulus. It returns the remainder after dividing two numbers. If there is no
remainder, it returns zero. In our example, it will return 0.8 /
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF&page=$i\">$i</a> ");
}
/* This is the exact statement that turns pages into link form that is used
above */
} // Ends the if statement
if(($totalrows - ($limit $page)) > 0){
/ This statement checks to see if there are more rows remaining, meaning there
are pages in front of the current one. */
$pagenext = $page++;
// Fancy way of adding 1 to page
echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");
/* Since there are pages remaining, this outputs NEXT in link form. */
}else{
echo("NEXT".$limit);
/* If we're on the last page possible, NEXT will NOT be displayed in link
form. */
}
mysql_free_result($result);
/* This line is not required, since MySQL will free the result after all
scripts have finished executing; however, it's a nice little backup. */
// The next line tells the server to stop parsing PHP
?>
I would be grateful for your help
marq 🙂 :o :o 😕