php pagination post variables to next page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

sectionLeader123
Forum Commoner
Posts: 31
Joined: Fri Oct 11, 2013 8:46 am

php pagination post variables to next page

Post by sectionLeader123 »

I have the following pagination code for my html form and it works great, the only problem is that the $criteria and $nature variable doesn't get posted to the next page. I know that I need to pass the variables through the links but anything I have tried so far doesn't work, any suggestions would be greatly appreciated thanks.

Code: Select all

<?php
include 'connect.php';
$criteria = $_POST['sCriteria1'];
$nature = $_POST['nature'];
$sql = "SELECT COUNT(*) FROM Customers, Jobs, Manufacturers, 
OperatingSystems, JobStatus, dataRecSpec, MediaSpec, Company";
$result=mysql_query($sql) or die(mysql_error());
$result2=mysql_fetch_row($result);
$numrows = $result2[0];
$rowsperpage = 1;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

$offset = ($currentpage - 1) * $rowsperpage;

$sql="	SELECT DISTINCT Customers.CUST_ID, Jobs.J_RefNum, Customers.CUST_Forename,
		Manufacturers.MANU_ID, Customers.CUST_Surname, Manufacturers.MANU_Name,  
		Customers.CUST_Email, Jobs.J_Model, Customers.CUST_Mobile, OperatingSystems.OS_ID,
		Customers.CUST_HomeNum, OperatingSystems.OS_Name, Customers.CUST_AddressL1,
		Jobs.J_ReceivedBy, Customers.CUST_AddressL2, 
		Jobs.J_DateRec, 
		Customers.CUST_AddressL3,
		Jobs.J_FaultDesc, Customers.CUST_Postcode, Jobs.J_PassWinAdmin, 
		Jobs.J_DataRecYN, Jobs.J_PowerSuppYN, Jobs.J_MediaYN, Jobs.J_BagYN, Jobs.J_Conditions,
		Jobs.J_ServiceTag, JobStatus.JS_ID, JobStatus.JS_Status, Jobs.J_Engineer, Jobs.J_EngComments,
		Jobs.J_AntivirusYN, Jobs.J_ServicePackYN, Jobs.J_PDFYN, Jobs.J_FlashYN, Jobs.J_VLCYN,
		Jobs.J_ValidatedYN, Jobs.J_DataRestoredYN, Jobs.J_Quote, Jobs.J_Comms,
		Jobs.J_DateWorkComm, 
		Jobs.J_DateCollec, Jobs.J_ProductKey,
		MediaSpec.MediaSpec_Spec, dataRecSpec.DRS_Name, Company.COMP_ID, Company.COMP_Name,
		Company.COMP_Email, Company.COMP_PrimaryNum, Company.COMP_SecondaryNum, Company.COMP_AddressL1,
		Company.COMP_AddressL2, Company.COMP_AddressL3, Company.COMP_Postcode
		FROM Customers, Jobs, Manufacturers, OperatingSystems, JobStatus, MediaSpec, dataRecSpec, Company
		WHERE (Customers.CUST_Postcode LIKE '%$criteria%' AND Jobs.J_RefNum = Customers.CUST_ID)
		AND (JobStatus.JS_ID = Jobs.J_RefNum) AND (Manufacturers.MANU_ID = Jobs.J_RefNum)
		AND (OperatingSystems.OS_ID = Jobs.J_RefNum) AND (MediaSpec.MediaSpec_ID = Jobs.J_RefNum)
		AND (dataRecSpec.DRS_ID = Jobs.J_RefNum) AND (Company.COMP_ID = Jobs.J_RefNum)
		LIMIT $offset, $rowsperpage
	 ";	
$result=mysql_query($sql) or die(mysql_error());
while($result2=mysql_fetch_assoc($result))
{
	$CID = $result2['CUST_ID'];
	$cuEmail = $result2['CUST_Email'];
	$forename = $result2['CUST_Forename'];
	$surname = $result2['CUST_Surname'];
	$mobile = $result2['CUST_Mobile'];
	$homenum = $result2['CUST_HomeNum'];
	$add1 = $result2['CUST_AddressL1'];
	$add2 = $result2['CUST_AddressL2'];
	$add3 = $result2['CUST_AddressL3'];
	$pCode = $result2['CUST_Postcode'];
	$compID = $result2['COMP_ID'];
	$compName = $result2['COMP_Name'];
	$compEmail = $result2['COMP_Email'];
	$pNumber = $result2['COMP_PrimaryNum'];
	$sNumber = $result2['COMP_SecondaryNum'];
	$compAdd1 = $result2['COMP_AddressL1'];
	$compAdd2 = $result2['COMP_AddressL2'];
	$compAdd3 = $result2['COMP_AddressL3'];
	$compPostcode = $result2['COMP_Postcode'];
	$jrefnum = $result2['J_RefNum'];
	$manuID = $result2['MANU_ID'];
	$manufacturer = $result2['MANU_Name'];
	$model = $result2['J_Model'];
	$osID = $result2['OS_ID'];
	$os = $result2['OS_Name'];
	$prokey = $result2['J_ProductKey'];
	$recBy = $result2['J_ReceivedBy'];
	$dateRec = $result2['J_DateRec'];
	$faultDesc = $result2['J_FaultDesc'];
	$passwin = $result2['J_PassWinAdmin'];
	$dataRecYN = $result2['J_DataRecYN'];
	$dataRecSpec = $result2['DRS_Name'];
	$powerSupp = $result2['J_PowerSuppYN'];
	$media = $result2['J_MediaYN'];
	$mediaSpec = $result2['MediaSpec_Spec'];
	$bag = $result2['J_BagYN'];
	$conditions = $result2['J_Conditions'];
	$servTag = $result2['J_ServiceTag'];
	$jstatID = $result2['JS_ID'];
	$jstatus = $result2['JS_Status'];
	$engineer = $result2['J_Engineer'];
	$engComments = $result2['J_EngComments'];
	$antivirus = $result2['J_AntivirusYN'];
	$servicepack = $result2['J_ServicePackYN'];
	$pdf = $result2['J_PDFYN'];
	$flash = $result2['J_FlashYN'];
	$vlc = $result2['J_VLCYN'];
	$validated = $result2['J_ValidatedYN'];
	$datarest = $result2['J_DataRestoredYN'];
	$quote = $result2['J_Quote'];
	$comms = $result2['J_Comms'];
	$dateworkcomm = $result2['J_DateWorkComm'];
	$datecollec = $result2['J_DateCollec'];
}//end while

$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for
                 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
?>
antorizz
Forum Newbie
Posts: 8
Joined: Sat Nov 03, 2012 12:03 pm

Re: php pagination post variables to next page

Post by antorizz »

Have you tried appending the two variables onto any of your links?

Code: Select all

$link = '<a href="file.php?currentpage=' . $x . '&criteria=' . $criteria . '&nature=' . $nature;
sectionLeader123
Forum Commoner
Posts: 31
Joined: Fri Oct 11, 2013 8:46 am

Re: php pagination post variables to next page

Post by sectionLeader123 »

Is this right, I think I have quotes in the wrong places :p

Code: Select all

 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=' . $nextpage . '&criteria=' . $criteria . '&nature=' . $nature>></a> ";
Thanks Again,, appreciate it
antorizz
Forum Newbie
Posts: 8
Joined: Sat Nov 03, 2012 12:03 pm

Re: php pagination post variables to next page

Post by antorizz »

I may not be explaining this correctly but; the . in php combines strings. Ie.

Code: Select all

$myName = 'Anthony';
$var1 = 'Hi, my name is ';
$var2 = ', pleased to meet you.';

echo $var1 . $myName . $var2;
// will print: Hi, my name is Anthony, pleased to meet you.
Same outcome, different format:

Code: Select all

$myName = 'Anthony';
echo 'Hi, my name is ' . $myName . ', pleased to meet you.';
The above will work because I'm using all single quotes, below won't work because I'm mixing double and signel quotes.

Code: Select all

$myName = 'Anthony';
echo "Hi, my name is ' . $myName . ', pleased to meet you";
You have to follow through with your quoting style. I prefer single for everything in PHP and double for all HTML (not sure on what is best practice or if it even matters). In the following code block, notice how I use the single quote to encapsulate what you are echoing to the page and the double quote for the <a> code.

Code: Select all

echo '<a href="'{$_SERVER['PHP_SELF']}?currentpage=' . $nextpage . '&criteria=' . $criteria . '&nature=' . $nature . '">Next Page</a>';
sectionLeader123
Forum Commoner
Posts: 31
Joined: Fri Oct 11, 2013 8:46 am

Re: php pagination post variables to next page

Post by sectionLeader123 »

Now I get the following error:

Parse error: syntax error, unexpected '{', expecting ',' or ';'

Thanks Again
antorizz
Forum Newbie
Posts: 8
Joined: Sat Nov 03, 2012 12:03 pm

Re: php pagination post variables to next page

Post by antorizz »

sorry, I edited my post, i forgot the last ' after you close the </a> tag. Look at my last php code again.
sectionLeader123
Forum Commoner
Posts: 31
Joined: Fri Oct 11, 2013 8:46 am

Re: php pagination post variables to next page

Post by sectionLeader123 »

Sorry still no change, here is what I have now:

echo '<a href="'{$_SERVER['PHP_SELF']}?currentpage=' . $nextpage . '&criteria=' . $criteria . '&nature=' . $nature . '">Next Page</a>';

Thanks Again
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php pagination post variables to next page

Post by Celauran »

Code: Select all

echo "<a href=\"{$_SERVER['PHP_SELF']}?currentpage={$nextpage}&criteria={$criteria}&nature={$nature}\">Next Page</a>";
sectionLeader123
Forum Commoner
Posts: 31
Joined: Fri Oct 11, 2013 8:46 am

Re: php pagination post variables to next page

Post by sectionLeader123 »

Appreciate the reply Celauran, I don't get any errors now but the values still aren't being posted to the next page.
antorizz
Forum Newbie
Posts: 8
Joined: Sat Nov 03, 2012 12:03 pm

Re: php pagination post variables to next page

Post by antorizz »

ooh.. I see.
at the top of your page initialize a new variable.
ooh.. I see.
at the top of your page initialize a new variable.

Code: Select all

$site_url = $_SERVER['PHP_SELF'];
Then replace

Code: Select all

{$_SERVER['PHP_SELF']}
with

Code: Select all

$site_url
So your new link will look like

Code: Select all

echo '<a href="' . $site_url . '?currentpage=' . $nextpage . '&criteria=' . $criteria . '&nature=' . $nature . '">Next Page</a>';
That is my bad, we all make typos. Makes me afraid I missed something else. I'm at work so not all here. But, this is basic trouble shooting and should be learned.

Before you fix the issue take a look at that error message again,
Parse error: syntax error, unexpected '{', expecting ',' or ';'
It should have also given you a line number on where the error occured. Once you read and break down the error message, you'll notice the important part of it:
"...unexpected '{', expecting ',' or ';'..."

Now go to that line of code and you notice your {$_SERVER['PHP_SELF']}... "{"!. You will also notice that I made a type in the code I gave you, I placed a single quote after the double quote and before the variable where one shouldn't be. So I broke the echo statement causing a parse error because I concluded the echo statement prematurely. Why? Because I used a single quote to start the statement, so a single quote will end the statement.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php pagination post variables to next page

Post by Celauran »

sectionLeader123 wrote:Appreciate the reply Celauran, I don't get any errors now but the values still aren't being posted to the next page.
I was just addressing the syntax error. Just looked at the original code now and the problem looks to be that you're passing $_GET variables and looking for $_POST variables.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php pagination post variables to next page

Post by Celauran »

antorizz wrote: at the top of your page initialize a new variable.

Code: Select all

$site_url = $_SERVER['PHP_SELF'];
Then replace

Code: Select all

{$_SERVER['PHP_SELF']}
with

Code: Select all

$site_url
There is absolutely no reason to do this.
sectionLeader123
Forum Commoner
Posts: 31
Joined: Fri Oct 11, 2013 8:46 am

date format not working with pagination

Post by sectionLeader123 »

I have the following query which works fine, but when I applied pagination to allow the user to see multiple result sets it gave me an undefined index error on my date fields but when I removed the date_format the query works fine. Am confused as to why it gave me an error just because I applied pagination.
Anyways, here is the query and any suggestions would be greatly appreciated thanks:

Code: Select all

$sql="	SELECT DISTINCT Customers.CUST_ID, Jobs.J_RefNum, Customers.CUST_Forename,
		Manufacturers.MANU_ID, Customers.CUST_Surname, Manufacturers.MANU_Name,  
		Customers.CUST_Email, Jobs.J_Model, Customers.CUST_Mobile, OperatingSystems.OS_ID,
		Customers.CUST_HomeNum, OperatingSystems.OS_Name, Customers.CUST_AddressL1,
		Jobs.J_ReceivedBy, Customers.CUST_AddressL2, 
		DATE_FORMAT(Jobs.J_DateRec, '%d/%m/%Y'), 
		Customers.CUST_AddressL3,
		Jobs.J_FaultDesc, Customers.CUST_Postcode, Jobs.J_PassWinAdmin, 
		Jobs.J_DataRecYN, Jobs.J_PowerSuppYN, Jobs.J_MediaYN, Jobs.J_BagYN, Jobs.J_Conditions,
		Jobs.J_ServiceTag, JobStatus.JS_ID, JobStatus.JS_Status, Jobs.J_Engineer, Jobs.J_EngComments,
		Jobs.J_AntivirusYN, Jobs.J_ServicePackYN, Jobs.J_PDFYN, Jobs.J_FlashYN, Jobs.J_VLCYN,
		Jobs.J_ValidatedYN, Jobs.J_DataRestoredYN, Jobs.J_Quote, Jobs.J_Comms,
		DATE_FORMAT(Jobs.J_DateWorkComm, '%d/%m/%Y'), 
		DATE_FORMAT(Jobs.J_DateCollec, '%d/%m/%Y'), Jobs.J_ProductKey,
		MediaSpec.MediaSpec_Spec, dataRecSpec.DRS_Name, Company.COMP_ID, Company.COMP_Name,
		Company.COMP_Email, Company.COMP_PrimaryNum, Company.COMP_SecondaryNum, Company.COMP_AddressL1,
		Company.COMP_AddressL2, Company.COMP_AddressL3, Company.COMP_Postcode
		FROM Customers, Jobs, Manufacturers, OperatingSystems, JobStatus, MediaSpec, dataRecSpec, Company
		WHERE (Jobs.J_RefNum = '$criteria' AND Jobs.J_RefNum = Customers.CUST_ID)
		AND (JobStatus.JS_ID = Jobs.J_RefNum) AND (Manufacturers.MANU_ID = Jobs.J_RefNum)
		AND (OperatingSystems.OS_ID = Jobs.J_RefNum) AND (MediaSpec.MediaSpec_ID = Jobs.J_RefNum)
		AND (dataRecSpec.DRS_ID = Jobs.J_RefNum) AND (Company.COMP_ID = Jobs.J_RefNum)
	 ";
Last edited by requinix on Sun Oct 13, 2013 7:30 pm, edited 1 time in total.
Reason: merged this in from another thread
sectionLeader123
Forum Commoner
Posts: 31
Joined: Fri Oct 11, 2013 8:46 am

Re: php pagination post variables to next page

Post by sectionLeader123 »

Got it thanks guys for your input, much appreciated.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php pagination post variables to next page

Post by requinix »

sectionLeader123, I merged your new thread into here since it's all the same code.

Code: Select all

DATE_FORMAT(Jobs.J_DateWorkComm, '%d/%m/%Y'), 
DATE_FORMAT(Jobs.J_DateCollec, '%d/%m/%Y'),
If you want to access those columns in your PHP you'd have to use

Code: Select all

$result2['DATE_FORMAT(Jobs.J_DateWorkComm, \'%d/%m/%Y\')']
$result2['DATE_FORMAT(Jobs.J_DateCollec, \'%d/%m/%Y\')']
which is, of course, ridiculous.

"Rename" those values with aliasing:

Code: Select all

DATE_FORMAT(Jobs.J_DateWorkComm, '%d/%m/%Y') AS J_DateWorkComm, 
DATE_FORMAT(Jobs.J_DateCollec, '%d/%m/%Y') AS J_DateCollec,
Post Reply