Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pages

Status
Not open for further replies.

manicleek

Technical User
Jun 16, 2004
143
GB
I have a page with the following code in which I am trying to paginate, at the moment it is only a test page.

It works fine except I can't get the value for the page to increase by 1 when you click the next button.

the code I have is as follows

Code:
<?php
$connection = mysql_connect("host","user","pass")
    or die("Couldn't make connection.");
$db = mysql_select_db("marinas", $connection)
    or die("Couldn't select database.");
	
$limit = 10;
$query_count = "SELECT * FROM marinas";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);

if (empty($page)){
	$page = 1;
}

$limitvalue = ($page * $limit) - $limit;
	
$detailsquery = "SELECT * FROM marinas LIMIT $limitvalue, $limit";

$result = mysql_query($detailsquery, $connection) or die ("Error; " .mysql_error());

?>	
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php echo $page ?><br><br>

<?php 
while ($row = mysql_fetch_array($result)) {
echo ($row["marinaname"]);
echo ("<br>");
}
?>
<br><br>
<?php
if ($page != 1) {
$pageprev = $page --;
echo ("<a href=\"test2.php?page=$pageprev\")Prev</a>&nbsp;");
} else {
echo ("Prev &nbsp;");
}
?>
<?php
if (($totalrows - ($limit * $page)) > 0) {
$pagenext = $page++;
echo "<a href=\"test2.php?page=$pagenext\">Next</a>&nbsp;";
} else {
echo ("Next &nbsp;");
}
?>
</body>
</html>
 
before the line:
Code:
if (empty($page)){
    $page = 1;
}
shouldn't you be 'getting' the page variable?
Code:
$page = $HTTP_GET_VARS["page"];


[cheers]
Cheers!
Laura
 
Sorry yeah, assume that is there, it still doesn't work,

I have used $_GET["page"];
 
I found the same code... mine works but with a completely different bug. The page increases by 1 however it also adds a last ghost page. So if i display 10 entries per page and have 17 entries, it says i have 2 pages yet the next button lets me go to page 3. Anyway, my code works and is fully commented, feel free to use it.

Code:
<?php
include 'include/include_common.php';
?>
<html>
<head>
<title><?=$company?> - <?=$appName?></title>
</head>
<body <?=$bodyTagAttribs?> >
<center>

<?php 
// put in the page header
include 'include/include_header.php';
include 'include/include_menu.php';

// Print top caption for table
echo "<br><blockquote><CAPTION ALIGN=top><FONT size=4 color=\"white\">
Insert information about this site later.
</FONT></CAPTION></blockquote>";


include 'include/include_dbconnect.php';
ConnectToDatabase();
	
	// This part of the code simply finds the total number of results that are going to be displayed. //
	$countquery = "SELECT * FROM hours;";
	$countresults = mysql_query($countquery);
	$totalresults = mysql_num_rows($countresults);
	
	// Sets it up so that the variable for page is always present //
	if (empty($page))
	{
	$page = 1;
	}

	// This sets the entry to start with when using the LIMIT command in MySQL //
	$limitstart = $page * $per_page - ($per_page);

	// Its the Query time again! //
	$query  = "SELECT * FROM $dbtable WHERE name='$user' ORDER BY date, name LIMIT $limitstart, $per_page";
	
	// Should get the result for the MySQL Query stated above //
	$result = mysql_query($query) or die("Somthing Wong!");
	
	// Set up the table formatting //
		echo "<table WIDTH=100% BORDER=\"1\" CELLPADDING=\"1\" bgcolor=\"" . $gridBGColor1 . "\">";
		
		echo "<tr bgcolor=\"#CCCCCC\"><th colspan=\"3\"> &nbsp </th><th>Name</th><th>Month</th><th>Day</th><th>Year</th><th>Hours</th><th>Project ID</th><th>Description / Comments</th></tr>\n";
	
	// Begin the while loop //
	while ($myrow = mysql_fetch_array($result))
	{
		printf("<tr>
		<td bgcolor=\"#787878\" valign=\"top\"><a href='edit.php?id=".$myrow["id"]."&action=edit'>Edit</a></td>
		<td bgcolor=\"#787878\" valign=\"top\"><a href='edit2.php?id=".$myrow["id"]."&action=edit'>Copy</a></td>
		<td bgcolor=\"#787878\" valign=\"top\"><a href='edit.php?id=".$myrow["id"]."&action=delete' onclick=\"return confirm('Are you sure you want to delete this entry?')\">Delete</a></td>
		<td valign=\"top\">%s</td><td valign=\"top\">%s</td>
		<td valign=\"top\">%s</td><td valign=\"top\">%s</td>
		<td valign=\"top\">%s</td><td width=75  valign=\"top\">%s-%s</td>
		<td>%s</tr>\n", 
		$myrow["name"], $myrow["month"], $myrow["day"], $myrow["year"], $myrow["hours"], $myrow["pid1"], $myrow["pid2"], $myrow["comment"]);
	}
	
		echo "</table>";
	
		// Sets value $prevpage to find previous pages. //
		if ($page !=1)
		{
		$prevpage = $page - 1;
		}
		else
		{
		$prevpage = 0;
		}                  
		
		echo ("<center>");
		
		// This displays a link to view previous results only if paginating is on a page other than 1 //
		if ($prevpage > 0)
		{
		print "<a href=\"$PHP_SELF?page=$prevpage\">Previous</a>&nbsp";
		}
		
		// Does the math to figure out the number of page links needed //
		$numberlinks = $totalresults / $per_page;
		
		// This displays the pages links except the last one //
		for ($n = 1; $n <= $numberlinks; $n++)
		{
			if ($n == $page)
			{
			echo ($n . "&nbsp");
			}
			else
			{
			print "<a href=\"$PHP_SELF?page=$n\">$n</a>&nbsp;";
			}
		}
		
		// This puts in the last number regardless if the last page ends on entry 13 //
		if ($totalresults == $per_page)
		{
			if ($n == $page)
			{
			echo ($n);
			}
			else
			{
			print "<a href=\"$PHP_SELF?page=$n\">$n</a>&nbsp;";
			}       
		}
		
		[COLOR=red]// This puts in the next link //
		if (($totalresults - ($per_page * $page)) > 0)
		{
		$nextpage = $page + 1;
		print "<a href=\"$PHP_SELF?page=$nextpage\">Next</a>";
		}[/color]

include 'include/include_footer.php';
?>
 
The problem with your original script was this line:

$pagenext = $page++;

You're using the postdecrement operator. The line of code above assigns the value in $page to $pagenext, then performs the increment. You need to preincrement:

$pagenext = ++$page;




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
The code
Code:
$pagenext = ++$page;

works the same as

Code:
$pagenext = $page + 1;
Provdided that you are incrementing the pages by 1. However both options leave you with a last page that displays nothing. there has to be a problem with the logic of this part of the code. I get a previous button after i leave page 1 and i get numbered links for pages 1 and 2 but on page 2, instead of disappearing, the next button remains, lets me go to a page 3, then disappears. What is wrong with this?
 
Hi Manicleek,
In that case, I believe the problem hinges near the end:
Code:
if (($totalrows - ($limit * $page)) > 0) {
  $pagenext = $page++;
  echo "<a href=\"test2.php?page=$pagenext\">Next</a>&nbsp;";
} else {
  echo ("Next &nbsp;");
}
Perhaps in that if statement?
Try doing:
Code:
if (($limit * $page) - $totalrows > 0) {
  $pagenext = $page++;
  echo "<a href=\"test2.php?page=$pagenext\">Next</a>&nbsp;";
} else {
  echo ("Next &nbsp;");
}
Here's a tip:
When executing if statements that don't seem to work, echo out the values you are comparing:
so: right before that if statement:
Code:
echo "if ((" . $limit . "*" . $page . ") - " . $totalrows > 0)";
That way you can see exactly how your if statement will execute with the values.

I hope this helped!
if (($limit * $page) - $totalrows > 0) {

[cheers]
Cheers!
Laura
 
I feel dumb

I had my code right except for this part. here is the fixed version, compare and youll understand that filters were needed in the first sql statement.

Code:
<?php
include 'include/include_common.php';
?>
<html>
<head>
<title><?=$company?> - <?=$appName?></title>
</head>
<body <?=$bodyTagAttribs?> >
<center>

<?php 
// put in the page header
include 'include/include_header.php';
include 'include/include_menu.php';

// Print top caption for table
echo "<br><blockquote><CAPTION ALIGN=top><FONT size=4 color=\"white\">
Insert information about this site later.
</FONT></CAPTION></blockquote>";


include 'include/include_dbconnect.php';
ConnectToDatabase();
	
	// This part of the code simply finds the total number of results that are going to be displayed. //
	$countquery = "SELECT * FROM $dbtable [COLOR=red]WHERE name='$user'[/color]";
	$countresults = mysql_query($countquery);
	$totalresults = mysql_num_rows($countresults);
	
	// Sets it up so that the variable for page is always present //
	if (empty($page))
	{
	$page = 1;
	}

	// This sets the entry to start with when using the LIMIT command in MySQL //
	$limitstart = $page * $per_page - ($per_page);

	// Its the Query time again! //
	$query  = "SELECT * FROM $dbtable WHERE name='$user' ORDER BY date, name LIMIT $limitstart, $per_page";
	
	// Should get the result for the MySQL Query stated above //
	$result = mysql_query($query) or die("Somthing Wong!");
	
	// Set up the table formatting //
		echo "<table WIDTH=100% BORDER=\"1\" CELLPADDING=\"1\" bgcolor=\"" . $gridBGColor1 . "\">";
		
		echo "<tr bgcolor=\"#CCCCCC\"><th colspan=\"3\"> &nbsp </th><th>Name</th><th>Month</th><th>Day</th><th>Year</th><th>Hours</th><th>Project ID</th><th>Description / Comments</th></tr>\n";
	
	// Begin the while loop //
	while ($myrow = mysql_fetch_array($result))
	{
		printf("<tr>
		<td bgcolor=\"#787878\" valign=\"top\"><a href='edit.php?id=".$myrow["id"]."&action=edit'>Edit</a></td>
		<td bgcolor=\"#787878\" valign=\"top\"><a href='edit2.php?id=".$myrow["id"]."&action=edit'>Copy</a></td>
		<td bgcolor=\"#787878\" valign=\"top\"><a href='edit.php?id=".$myrow["id"]."&action=delete' onclick=\"return confirm('Are you sure you want to delete this entry?')\">Delete</a></td>
		<td valign=\"top\">%s</td><td valign=\"top\">%s</td>
		<td valign=\"top\">%s</td><td valign=\"top\">%s</td>
		<td valign=\"top\">%s</td><td width=75  valign=\"top\">%s-%s</td>
		<td>%s</tr>\n", 
		$myrow["name"], $myrow["month"], $myrow["day"], $myrow["year"], $myrow["hours"], $myrow["pid1"], $myrow["pid2"], $myrow["comment"]);
	}
	
		echo "</table>";
	
		// Sets value $prevpage to find previous pages. //
		if ($page !=1)
		{
		$prevpage = $page - 1;
		}
		else
		{
		$prevpage = 0;
		}                  
		
		echo ("<center>");
		
		// This displays a link to view previous results only if paginating is on a page other than 1 //
		if ($prevpage > 0)
		{
		print "<a href=\"$PHP_SELF?page=$prevpage\">Previous</a>&nbsp";
		}
		
		// Does the math to figure out the number of page links needed //
		$numberlinks = $totalresults / $per_page;
		
		// This displays the pages links except the last one //
		for ($n = 1; $n <= $numberlinks; $n++)
		{
			if ($n == $page)
			{
			echo ($n . "&nbsp");
			}
			else
			{
			print "<a href=\"$PHP_SELF?page=$n\">$n</a>&nbsp;";
			}
		}
		
		// This puts in the last number regardless if the last page ends on entry 13 //
		if ($totalresults == $per_page)
		{
			if ($n == $page)
			{
			echo ($n);
			}
			else
			{
			print "<a href=\"$PHP_SELF?page=$n\">$n</a>&nbsp;";
			}       
		}
		// This puts in the next link //
		if (($totalresults - ($per_page * $page)) > 0)
		{
		$nextpage = ++$page;
		print "<a href=\"$PHP_SELF?page=$nextpage\">Next</a>";
		}

include 'include/include_footer.php';
?>
 
Mine links etc I have working properly, and if I manually enter the page number I get the correct result, the problem is that for some reason the links aren't adding 1 or subtracting 1 to the value for $page

as you can see in my origional posting of the code I have echoed out the value for page so I could see what its doing and it always says 1
 
manicleek,
did you echo out your if statement, like I suggested above?
I do not think the if statement that adds or decrements from $page is even executing.

[cheers]
Cheers!
Laura
 
FOUND THE PROBLEM :D
Took me about an 1/2 hour of reading each line!

The Script Above Reads
Code:
// This puts in the last number regardless if the last page ends on entry 13 //
        if ($totalresults == $per_page)
        {
            if ($n == $page)
            {
            echo ($n);
            }
            else
            {
            print "<a href=\"$PHP_SELF?page=$n\">$n</a>&nbsp;";
            }       
        }

It SHOULD Read

Code:
// This puts in the last number regardless if the last page ends on entry 13 //
        if ($totalresults != $per_page)
        {
            if ($n == $page)
            {
            echo ($n);
            }
            else
            {
            print "<a href=\"$PHP_SELF?page=$n\">$n</a>&nbsp;";
            }       
        }

You can view the fixed version at:

if it doesn't load or is "screwed up" wait a few minutes. As the homepage reads I am activly coding the site, so all that mean is I'm chnaging somthing on the page.

PS:I made a few changes to the script to meet my fancy, but it really help and saved me from hrs of coding and trials :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top