?
Solved

Passing variable whilst using php paging

Posted on 2005-05-05
3
Medium Priority
?
221 Views
Last Modified: 2013-12-12
My page now has paging {finally} i have come across a snag now though. I have a page which the fromDate and toDate are selected and passed to this page which's code is below. The first page displayed shows fine but when i go to click onto the any other pages it is as iff all the variables in my page reset.....

i have included som variable passing in the paging code but i think there may be a problem.

see what you can make of it.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<?php
//$fromDate = $_POST['fromDate'];
$fromDate = isset($_POST['fromDate']) ? $_POST['fromDate'] :-99;
//$toDate = $_POST['toDate'];
$toDate = isset($_POST['toDate']) ? $_POST['toDate'] :-99;
$link = mysql_connect('localhost','','');  
$col = '#EDEDED';

mysql_select_db('expense') or die('Error selecting database.');  


//$query = "SELECT jobs.jNo, jobs.jLNo, locations.lName, locations.lTown, jobs.jDate, jobs.jWorkHours,
//jobs.jWorkPay, jobs.jTravelHours, jobs.jTravelPay, jobs.jTotalPay, jobs.jExtras FROM jobs INNER JOIN locations
//ON jobs.jLNo=locations.lNo WHERE jobs.jDate BETWEEN '$fromDate' and '$toDate' LIMIT $from, $max_results";
//$result = mysql_query($query);
?>

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>Summary Report</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>
<body background="images/background.jpg">
<table width=100% border="0" cellspacing="0" cellpadding="1">
      <tr bgcolor="#000000">      
            <td width=10% border="0" cellpadding="8" cellspacing="0"><b><font face=verdana size=-2 color=#ffffff>#</font></b></td>
            <td width=5%><b><font face=verdana size=-2 color=#ffffff>Date</font></b></td>
            <td width=*><b><font face=verdana size=-2 color=#ffffff>Location</font></b></td>
            <td width=15%><b><font face=verdana size=-2 color=#ffffff>Labour</font></b></td>
            <td width=15%><b><font face=verdana size=-2 color=#ffffff>Travel</font></b></td>
            <td width=6%><b><font face=verdana size=-2 color=#ffffff>Extras</font></b></td>
            <td width=6%><b><font face=verdana size=-2 color=#ffffff>Total</font></b></td>
      </tr>
      
<?
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}
$max_results = 10;

$from = (($page * $max_results) - $max_results);

$limitqry = "SELECT jobs.jNo, jobs.jLNo, locations.lName, locations.lTown, jobs.jDate, jobs.jWorkHours,
jobs.jWorkPay, jobs.jTravelHours, jobs.jTravelPay, jobs.jTotalPay, jobs.jExtras FROM jobs INNER JOIN locations
ON jobs.jLNo=locations.lNo WHERE jobs.jDate BETWEEN '$fromDate' and '$toDate' LIMIT $from, $max_results";
$col = '#EDEDED';
$sql = mysql_query($limitqry);
while($lRow = mysql_fetch_assoc($sql)){

      echo '<tr bgcolor="',$col,'"><td rowspan="2"><font face=verdana size=-2>';
      echo $lRow['jNo'];      
      echo '</td><td rowspan="2"><font face=verdana size=-2>';
      echo $lRow['jDate'];      
      echo '</td><td rowspan="2"><font face=verdana size=-2>';
      echo $lRow['lName'];
      echo ', ';
      echo $lRow['lTown'];      
      echo '</td><td><font face=verdana size=-2>';
      echo $lRow['jTravelHours'];      
      echo 'hrs</td><td><font face=verdana size=-2>';
      echo $lRow['jWorkHours'];
      echo 'hrs</td><td rowspan="2"><font face=verdana size=-2 color=#ff0000>£';
      echo $lRow['jExtras'];
      echo '</td><td rowspan="2"><font face=verdana size=-2 color=#ff0000>£';
      echo $lRow['jTotalPay'];
      echo '</td></tr><tr bgcolor="', $col,'"><td><font face=verdana size=-2 color=#ff0000>£';
      echo $lRow['jTravelPay'];
      echo '</td><td><font face=verdana size=-2 color=#ff0000>£';
      echo $lRow['jWorkPay'];
      echo '</td></tr>';
}
echo '</table>';
mysql_free_result($sql);

//creation of page numbering

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM jobs WHERE jobs.jDate BETWEEN '$fromDate' and '$toDate'"),0);
$total_pages = ceil($total_results / $max_results);
echo "<center>";

if($page > 1){
    $prev = ($page - 1);
    echo "<font face=verdana size=-2><a href=\"".$_SERVER['PHP_SELF']."?fromDate=$fromDate?toDate=$toDate?page=$prev\"> Previous </a>: </font>";
} else {
echo "<font face=verdana size=-2> Previous :</font>";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<font face=verdana size=-2><a href=\"".$_SERVER['PHP_SELF']."?fromDate=$fromDate?toDate=$toDate?page=$i\"> $i </a> :</font>";
    }
}

if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?fromDate=$fromDate?toDate=$toDate?page=$next\"><font face=verdana size=-2> Next </font></a>";
} else {
echo "<font face=verdana size=-2>: Next </font>";
}
echo "</center>";

//creation of totals at footer of page
$tQuery = "select sum(jWorkHours)as sum_jWorkHours, sum(jWorkPay)as sum_jWorkPay, sum(jTravelHours)as sum_jTravelHours, sum(jTravelPay)as sum_jTravelPay, sum(jExtras)as sum_jExtras from jobs WHERE jDate BETWEEN '$fromDate' and '$toDate'";
$result = mysql_query($tQuery);
if ($row = mysql_fetch_array($result))
{
      echo '<br><font size="-2" face="Verdana">Work Total = ';
      echo $row['sum_jWorkHours'];
      echo 'hrs (£';
      echo $row['sum_jWorkPay'];
      echo ')</font><br><font size="-2" face="Verdana">Travel Totals = ';
      echo $row['sum_jTravelHours'];
      echo 'hrs (£';
      echo $row['sum_jTravelPay'];
      echo ')</font><br><font size="-2" face="Verdana">Total Extras = ';
      echo '£';
      echo $row['sum_jExtras'];
      echo '</font><br><b><font size="-2" face="Verdana">Grand Total Between the Dates = £';
      echo $row['sum_jWorkPay'] + $row['sum_jTravelPay'] + $row['sum_jExtras'];
}
?>

</body>
</html>
0
Comment
Question by:ShaneJones
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
AlanJDM earned 100 total points
ID: 13944128
Your date variables are passed via POST data the first time, however you are passing them through the paging vig GET data so on all of the subsequent pages $_POST['fromDate'] and $_POST['toDate'] are empty. You can fix this by using the $_REQUEST array instead of the $_POST array. The $_REQUEST array contains a combination of the $_POST and the $_GET array.

change...

$fromDate = isset($_POST['fromDate']) ? $_POST['fromDate'] :-99;
$toDate = isset($_POST['toDate']) ? $_POST['toDate'] :-99;

to...

$fromDate = isset($_REQUEST['fromDate']) ? $_REQUEST['fromDate'] :-99;
$toDate = isset($_REQUEST['toDate']) ? $_REQUEST['toDate'] :-99;



Alan
0
 
LVL 12

Author Comment

by:ShaneJones
ID: 13946182
No it is still not working?

Have i got the hyperlinks set up cottectly in these lines.

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM jobs WHERE jobs.jDate BETWEEN '$fromDate' and '$toDate'"),0);
$total_pages = ceil($total_results / $max_results);
echo "<center>";

if($page > 1){
    $prev = ($page - 1);
    echo "<font face=verdana size=-2><a href=\"".$_SERVER['PHP_SELF']."?fromDate=$fromDate?toDate=$toDate?page=$prev\"> Previous </a>: </font>";
} else {
echo "<font face=verdana size=-2> Previous :</font>";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<font face=verdana size=-2><a href=\"".$_SERVER['PHP_SELF']."?fromDate=$fromDate?toDate=$toDate?page=$i\"> $i </a> :</font>";
    }
}

if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?fromDate=$fromDate?toDate=$toDate?page=$next\"><font face=verdana size=-2> Next </font></a>";
} else {
echo "<font face=verdana size=-2>: Next </font>";
}
echo "</center>";
0
 
LVL 12

Author Comment

by:ShaneJones
ID: 13949955
solved it now

used &'s instead of ? marks

ie from this
a href=\"".$_SERVER['PHP_SELF']."?fromDate=$fromDate?toDate=$toDate?page=$prev\">

to this
a href=\"".$_SERVER['PHP_SELF']."?fromDate=$fromDate&toDate=$toDate&page=$prev\">

for all of the links
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 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