Solved

Pagination limits

Posted on 2009-05-19
3
340 Views
Last Modified: 2012-05-07
I have a functional pagination script I've hacked out from a variety of examples online. I just need it to do a couple more things. I need to eliminate the possibility of negatives on the Previous link (http://localhost/_maps/paginate.php?startrow=-10).

I would like to just hide the Previous link if ?startrow=0 but I'm not sure how to make that happen. My code is below if someone can help.
<?php

include_once("includes/db.php");

?>

<?PHP

//check if the starting row variable was passed in the URL or not

if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {

  //we give the value of the starting row to 0 because nothing was found in URL

  $startrow = 0;

//otherwise we take the value from the URL

} else {

  $startrow = (int)$_GET['startrow'];

}

?>
 

<?PHP

//this part goes after the checking of the $_GET var

$fetch = mysql_query("SELECT * 

					FROM markers 

					JOIN category ON category.cat_id = markers.cat_id

					LIMIT $startrow, 10")or

die(mysql_error());

?>

<?php

	$num=mysql_numrows($fetch);

	

	mysql_close();

?>
 

<table border="1" cellspacing="0" cellpadding="1" bordercolor="#000000" width="60%" align="center">

	<tr>

		<th align="left"><font face="verdana" size="2">Marker</font></th>

		<th align="left"><font face="verdana" size="2">Category</font></th>

		<th valign="top" width="60"><font face="verdana" size="2">Edit</font></th>

		<th valign="top" width="60"><font face="verdana" size="2">Delete</font></th>

	</tr>
 

<?php

	$i=0;

		while ($i < $num) {

			$marker_id=mysql_result($fetch,$i,"marker_id");

			$name=mysql_result($fetch,$i,"name");

			$street=mysql_result($fetch,$i,"street");

			$city=mysql_result($fetch,$i,"city");

			$state=mysql_result($fetch,$i,"state");

			$zip=mysql_result($fetch,$i,"zip");

			$url=mysql_result($fetch,$i,"url");

			$lat=mysql_result($fetch,$i,"lat");

			$lng=mysql_result($fetch,$i,"lng");

			$cat_name=mysql_result($fetch,$i,"cat_name");

?>
 

<tr>

	<td valign="top">

		<font face="verdana" size="2"><b><?php echo $name; ?></b></font><br/>

		<font face="verdana" size="1"><?php echo $street; ?></font>,&nbsp;

		<font face="verdana" size="1"><?php echo $city; ?></font>,&nbsp;

		<font face="verdana" size="1"><?php echo $state; ?></font>,&nbsp;

		<font face="verdana" size="1"><?php echo $zip; ?></font><br/>

		<font face="verdana" size="1"><a href="<?php echo $url; ?>"><?php echo $url; ?></a></font><br/>

		<font face="verdana" size="1"><b>Latitude:</b>&nbsp;<?php echo $lat; ?></font>&nbsp;

		<font face="verdana" size="1"><b>Longitude:</b>&nbsp;<?php echo $lng; ?></font></td>

	<td valign="top"><font face="verdana" size="2"><?php echo $cat_name; ?></font></td>

	<td valign="top" width="60"><a href="edit.php?marker_id=<?php echo $marker_id; ?>"><img src="../images/edit.png" alt="Edit" border="0"></a></td>

	<td valign="top" width="60"><a href="confirm_delete.php?marker_id=<?php echo $marker_id; ?>"><img src="../images/delete.png" alt="Delete" border="0"></a></td>

</tr>

<input type="hidden" name="marker_id" value="<?php echo $marker_id; ?>">

<?php

	$i++;

	}

?>

</table>
 
 

<?PHP

//now this is the link..

echo '<center><table><td><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow-10).'">Previous</a></td><td align="right"><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a></td></table></center>';

?>

Open in new window

0
Comment
Question by:webweever
3 Comments
 
LVL 11

Accepted Solution

by:
BrianMM earned 100 total points
ID: 24420191
there is nicer but you could always just check for a negative value in your else

//otherwise we take the value from the URL
} else {
  if($_GET['startrow'] <0) {
    $startrow = 0;
  } else {
    $startrow = (int)$_GET['startrow'];
  }
}

to hide if $startrow = 0 quckly do an if and display wink with or without previous.
0
 
LVL 5

Expert Comment

by:bored321
ID: 24420322
Another solution that hides the tag if start row <=0 would be
<?

//check if $startrow is less than or equal to 0 

$previoustag = ($startrow <=0) ?  "" : '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow-10).'">Previous</a>';

//now this is the link..

echo '<center><table><td>'.$previoustag.'</td><td align="right"><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a></td></table></center>';

?>

Open in new window

0
 
LVL 1

Author Comment

by:webweever
ID: 24420345
Below is how I did it and it appears to work. Thanks for the help
<?php

include_once("includes/db.php");

?>

<?PHP

//check if the starting row variable was passed in the URL or not

if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow']) or ($_GET['startrow'] < 0)) {

  //we give the value of the starting row to 0 because nothing was found in URL

	$startrow = 0;

	// otherwise we take the value from the URL

	} else {

	$startrow = (int)$_GET['startrow'];

}
 

?>
 

<?PHP

//this part goes after the checking of the $_GET var

$fetch = mysql_query("SELECT * 

					FROM markers 

					JOIN category ON category.cat_id = markers.cat_id

					LIMIT $startrow, 10")or

die(mysql_error());

?>

<?php

	$num=mysql_numrows($fetch);

	

	mysql_close();

?>
 

<table border="1" cellspacing="0" cellpadding="1" bordercolor="#000000" width="60%" align="center">

	<tr>

		<th align="left"><font face="verdana" size="2">Marker</font></th>

		<th align="left"><font face="verdana" size="2">Category</font></th>

		<th valign="top" width="60"><font face="verdana" size="2">Edit</font></th>

		<th valign="top" width="60"><font face="verdana" size="2">Delete</font></th>

	</tr>
 

<?php

	$i=0;

		while ($i < $num) {

			$marker_id=mysql_result($fetch,$i,"marker_id");

			$name=mysql_result($fetch,$i,"name");

			$street=mysql_result($fetch,$i,"street");

			$city=mysql_result($fetch,$i,"city");

			$state=mysql_result($fetch,$i,"state");

			$zip=mysql_result($fetch,$i,"zip");

			$url=mysql_result($fetch,$i,"url");

			$lat=mysql_result($fetch,$i,"lat");

			$lng=mysql_result($fetch,$i,"lng");

			$cat_name=mysql_result($fetch,$i,"cat_name");

?>
 

<tr>

	<td valign="top">

		<font face="verdana" size="2"><b><?php echo $name; ?></b></font><br/>

		<font face="verdana" size="1"><?php echo $street; ?></font>,&nbsp;

		<font face="verdana" size="1"><?php echo $city; ?></font>,&nbsp;

		<font face="verdana" size="1"><?php echo $state; ?></font>,&nbsp;

		<font face="verdana" size="1"><?php echo $zip; ?></font><br/>

		<font face="verdana" size="1"><a href="<?php echo $url; ?>"><?php echo $url; ?></a></font><br/>

		<font face="verdana" size="1"><b>Latitude:</b>&nbsp;<?php echo $lat; ?></font>&nbsp;

		<font face="verdana" size="1"><b>Longitude:</b>&nbsp;<?php echo $lng; ?></font></td>

	<td valign="top"><font face="verdana" size="2"><?php echo $cat_name; ?></font></td>

	<td valign="top" width="60"><a href="edit.php?marker_id=<?php echo $marker_id; ?>"><img src="../images/edit.png" alt="Edit" border="0"></a></td>

	<td valign="top" width="60"><a href="confirm_delete.php?marker_id=<?php echo $marker_id; ?>"><img src="../images/delete.png" alt="Delete" border="0"></a></td>

</tr>

<input type="hidden" name="marker_id" value="<?php echo $marker_id; ?>">

<?php

	$i++;

	}

?>

</table>
 
 

<?PHP

//now this is the link..

if (!isset($_GET['startrow']) or ($_GET['startrow'] <= 0)) {

	echo '<center><table><td></td><td align="right"><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a></td></table></center>';

} else {

	echo '<center><table><td><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow-10).'">Previous</a></td><td align="right"><a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a></td></table></center>';

}

?>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
phpmyadmin 3 33
Whether to use true/false, yes/no or 0/1 11 51
How to count in a table in php 22 19
PHP Sessions in WordPress 8 11
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now