Page 1 of 1

Pagination Problems

Posted: Sat Apr 10, 2010 12:31 pm
by sirk
Hi im having a prblem with my pagination most of it seems to work yet when i wanted to add a forward and back button and put a check in to stop it going past the amount of results that qwas returned by the it wouldnt work heres my code im pretty sure its probably a stupid little thing but i cant seem to find it

Code: Select all

<?PHP
//start the session
session_start();
//check to make sure the session variable isn't registered
if(!session_is_registered('EmpID')){
//the session variable isn't registered, send them back to the login page
header( "Location: JMSystemLogin.php" );
} 
$EmpID = $_SESSION['EmpID'];
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password1"; // Mysql password 
$db_name="JMSystemDB"; // Database nam
mysql_connect($host,$username,$password) or die("ERROR:".mysql_error());
mysql_select_db($db_name) or die("ERROR DB:".mysql_error()); 

$next = 1;
$prev = 1;
if(!isset($_GET['offset']))
{
	$offset = 0;
	$prev = 0;
}
elseif(isset($_GET['offset']))
{	
	$offset = $_GET['offset'];
	$record = $_GET['record'];
	if(record == 'next')
	{
		$offset++;
	}
	if(record == 'prev')
	{
		$offset--;	
	}
	if($offset == 0)
	{
		$prev = 0;
	}
}
else
{
echo 'error1';
}
$query = "SELECT * FROM Job WHERE Employee_EmployeeID = '$EmpID' AND Fixed = 0 ORDER BY TargetDate LIMIT $offset,1";
$result = mysql_query($query);

	while($r = mysql_fetch_array($result))
	{
		$JobID = $r['JobID'];
		$Unit = $r['Units_UnitID'];
		$Desc = $r['JobDescription'];
		$OpenedBy = $r['JobOpendBy'];
		$Target = $r['TargetDate'];
		$Comments = $r['Comments'];
		}
		
$chknext = $offset + 1;	

$query_chk = "SELECT * FROM Job WHERE Employee_EmployeeID = '$EmpID' AND Fixed = 0 ORDER BY TargetDate LIMIT $chknext,1";	
$result_chk = mysql_query($query_chk);
if(!$result_chk)
{
	$next = 0;
}

?>
this bit is for the actuall checking to get the results and restricting the page numbers and these next parts are for the actual links to cycle back and forth the pages

Code: Select all

<?PHP if(prev == 1) { ?><a href="Jobs.php?offset=<?PHP echo $offset ?>&record=prev">previous record</a> <?PHP } ?><br />
    <?PHP if(next == 1) { ?><a href="Jobs.php?offset=<?PHP echo $offset ?>&record=next">next record</a><?PHP } ?>
if you could maybe point me to some resources that could help me see where i have gone wrong in my logic or w/e that would be much appreciated thanks

Re: Pagination Problems

Posted: Sat Apr 10, 2010 4:47 pm
by lunarnet76
have you simply forgot
in

Code: Select all

<?PHP if(prev == 1) { ?><a href="Jobs.php?offset=<?PHP echo $offset ?>&record=prev">previous record</a> <?PHP } ?><br />
    <?PHP if(next == 1) { ?><a href="Jobs.php?offset=<?PHP echo $offset ?>&record=next">next record</a><?PHP } ?>
to use $prev and not prev, and $next instead of next^^?

Re: Pagination Problems

Posted: Sat Apr 10, 2010 6:15 pm
by sirk
that seemed to get the link showing for the next page but it still doesnt actually take me to the next record i have checked to make sure there is more than one record in the db just in case and there is. here is the changed version of that link code though.

Code: Select all

<?PHP if($prev == 1) { ?><a href="Jobs.php?offset=<?PHP echo $offset ?>&record=prev">previous record</a> <?PHP } ?><br />
    <?PHP if($next == 1) { ?><a href="Jobs.php?offset=<?PHP echo $offset ?>&record=next">next record</a><?PHP } ?>

Re: Pagination Problems

Posted: Sat Apr 10, 2010 7:14 pm
by lunarnet76
you have the same problems with record it should be $record,
and use

Code: Select all

mysql_query($query)or die($query.'<br>'.mysql_error());
to show the problems!
you should also add

Code: Select all

error_reporting(E_ALL);
just after <?php so it would give you the errors in the page!

Re: Pagination Problems

Posted: Sun Apr 11, 2010 10:59 am
by sirk
hey thanks for the help my page is now working thanks guys