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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do I Add Constant Values To Pagination Script?

Status
Not open for further replies.

HowdeeDoodee

Technical User
Mar 14, 2005
61
US
Here is a pagination script that works great with only one input
variable sent to the script via a form. However, I now have
more input variables being sent to the form. The script grabs
all the new input variables one time and the script runs fine
on page one. The problem is the new input variables are not
stored in the script like the [SeeAlso] variable and thus the
new input variables lose their value after the script runs one
time on page one. When the script processes to page 2 and higher,
the only value available to the script is the [SeeAlso] variable
value because the [SeeAlso] value has been preserved in the
script. What I need to know is how to preserve the other input
values sent to the script from the form.


Code:
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<form>
<font size="2" face="Arial"><b>
</form>

<?p>

//sample input if all checkboxes are checked and EEE is the search 
//term in the input box
//Array ( [TR] => ON [ES] => ON [NV] => ON [TH] => ON [SM] => ON [EN] => ON [ALL] => ON [SeeAlso] => EEE )

//here are the variables containing values 
//sent to the script from the form
$ES = $_GET['ES']; 
$NV = $_GET['NV']; 
$TR = $_GET['TR']; 
$TH = $_GET['TH']; 
$SM = $_GET['SM']; 
$EN = $_GET['EN']; 
$ALL = $_GET['ALL']; 
$SeeAlso = $_GET['SeeAlso']; 


    $username = "theusual";
    $password = "herewego";
    $dbname = "ouchouchouch";
    $global_db = mysql_connect('localhost', $username, $password);
    mysql_select_db($dbname, $global_db);


$query = "SELECT * FROM `View2` WHERE `Topic` REGEXP '[[:<:]]($SeeAlso)[[:>:]]'";

//These code lines append to the $query


//On page two and higher these values are
//wiped out and the SELECT statement no longer
//has the Source appended to the SELECT statement

//I want to preserve these values in the script like
//the script preserves the [SeeAlso] value

  if(!empty($ES)){ 
   $ES = "'ES'"; 
   $query = $query . " AND `Source`={$ES}"; 
   } 

  if(!empty($TH)){ 
  $TH = "'TH'"; 
  $query = $query . " AND `Source`={$TH}"; 
  } 
  
  if(!empty($SM)){ 
  $SM = "'SM'"; 
  $query = $query . " AND `Source`={$SM}"; 
  } 

  if(!empty($NV)){ 
  $NV = "'NV'"; 
  $query = $query . " AND `Source`={$NV}"; 
  } 

  if(!empty($TR)){ 
  $TR = "'TR'"; 
  $query = $query . " AND `Source`={$TR}"; 
  } 

  if(!empty($EN)){ 
  $EN = "'EN'"; 
  $query = $query . " AND `Source`={$EN}"; 
  } 

    
    $result = mysql_query($query) or die("ERROR63");
    $num_record = mysql_num_rows($result);
    if($num_record > $display) { // Only show 1,2,3,etc. when //there are more records found that fit on 1 page
    // when the page is loaded first...
    if(empty($pagenr)) { 
    $pagenr = 1;
    }
    // some variables
    $display = 10; // number of records to display per page
    $max_pages_to_print = 7; // number of pages to show, if you change this you also have to change the variable 'middlenumber', for example:increase this one with two, increase middlenumber with one
    $startrecord = $pagenr * $display; // f$displayirst record to show from the queryresult
    $num_pages = (ceil($num_record/$display)); //set number of pages
    $loopcounter = 0; // counter for whileloop
    $currentpage = $pagenr; // Page where we are at the moment
    $middlenumber = 3; // Number will be decreased from variable currentpage in order to get the currentpage always in the middle
    $colourcounter = 0; // Variable to change the background-color of the <td>
    $i = 1; // variable that will print 1,2,3,etc..
    $x = 0; // variable i use to put always the current, marked page in the middle

echo "<div align=\"center\">\n";
echo "  <center>\n";
echo "  <table border=\"1\" width=\"700\" height=\"25\" cellspacing=\"1\" bgcolor=\"#D8E0F0\">\n";
echo "    <tr>\n";
echo "      <td colspan=\"2\" valign=\"middle\" align=\"center\" bgcolor=\"#0000c8\" width=\"690\">\n";
echo "        <p align=\"center\"><font face=\"Arial\" color=\"#FFFFFF\">Retrieval Data</font></td>\n";
echo "    </tr>\n";
echo "    <tr>\n";
echo "      <td width=\"248\"><font face=\"Arial\" size=\"2\">Search Term:</font></td>\n";
echo "      <td width=\"436\"><font face=\"Arial\" size=\"2\">$SeeAlso</font></td>\n";
echo "    </tr>\n";
echo "    <tr>\n";
echo "      <td width=\"248\"><font face=\"Arial\" size=\"2\">Current Page</font></td>\n";
echo "      <td width=\"436\"><font face=\"Arial\" size=\"2\">$currentpage</font></td>\n";
echo "    </tr>\n";
echo "    <tr>\n";
echo "      <td width=\"248\"><font face=\"Arial\" size=\"2\">Total Pages Retrieved:</font></td>\n";
echo "      <td width=\"436\"><font face=\"Arial\" size=\"2\">$num_pages</font></td>\n";
echo "    </tr>\n";
echo "    <tr>\n";
echo "      <td width=\"248\"><font face=\"Arial\" size=\"2\">Total Citations Retrieved:</font></td>\n";
echo "      <td width=\"436\"><font face=\"Arial\" size=\"2\">$num_record</font></td>\n";
echo "    </tr>\n";
echo "  </table>\n";
echo "  </center>\n";
echo "</div>\n";

    // actual stuff starts here
    print("<table border=0 align=center><tr>");
    if($currentpage >= $max_pages_to_print) {
print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a> &nbsp</td>' ; // print pagenumbers 
        }
    //BEGIN LOOP
    while($loopcounter < $max_pages_to_print) { 
    if($currentpage >= $max_pages_to_print) { // If user clicks om page higher than $max_pages_to_print
    // Mark current page
    if($currentpage == $i) {
    print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a> &nbsp</td>' ; // print pagenumbers 
    $i += 1; //increase pagenr
    $loopcounter += 1; // increase loopcounter
    }
    // End marking
    if($i > $num_pages) { // if last page has been printed, exit loop
    break;
    }
    if($x == 0) {
    $i = $currentpage - $middlenumber; // current page will always be printed in the middle
    }
    print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a> &nbsp</td>' ; // print pagenumbers 
    $x = $x + 1;
    $i += 1; //increase pagenr
    $loopcounter += 1; // increase loopcounter
    }
    else { // Else user clicks on a pagenumber lower $max_pages_to_print
    // Mark current page
    if($currentpage == $i) {
    print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a> &nbsp</td>' ; // print pagenumbers 
    $i += 1; //increase pagenr
    $loopcounter += 1; // increase loopcounter
    }
    // End marking
    if($i > $num_pages) { // if less than $max_mages_to_print, exit loop
    break;
    }
    print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a> &nbsp</td>' ; // print pagenumbers 
    $i += 1; //increase pagenr
    $loopcounter += 1; // increase loopcounter
    } // End if
    } 
    // END LOOP
    if(($num_pages > $max_pages_to_print AND // notice the user that there are more pages
        $i <= $num_pages)) {
    print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a> &nbsp</td>' ; // print pagenumbers 
    }
    print("</tr></table>");
    $startrecord = $startrecord - $display; // Set startrecord to the right position
    // Some calculation for the lastrecord
    if($currentpage == $num_pages) { // Last page...
    $lastrecord = $num_record; // so $lastrecord = $num_record
    }
    else {
    $lastrecord = ($currentpage * $display);
    }
    } // End of the first if-statement


    // actual query, watch the end($startrecord, $display)
  

$query2 = "$query1 LIMIT $startrecord, $display";



 $result2 = mysql_query($query2) or die("ERROR148");
    
    
    // print results on screen

      print("<table border=1 align=center width=700 bgcolor=#0000c8 style=\"font-family: Arial; font-weight: bold\" cellpadding=\"1\" cellspacing=\"1\" font face=Arial><tr><td align=center><font color=#FFFFFF><b>Source</b></font></td><td align=center><font color=#FFFFFF><b>Topic</b></font></td><td align=center><font color=#FFFFFF><b>Subtopic</b></font></td><td align=center><font color=#FFFFFF><b>References</b></font></td></tr>");

    $Source="Source";
    $Topic="Topic";
    $Subtopic="Subtopic";
    $References="References";
    while(list($Source, $Topic,$Subtopic,$References)= mysql_fetch_row($result2)) {
    if ($colorcounter == 0) {
    $colorbg = "#d8e0f0";    
    }
    else {
    $colorbg = "#e6eaf4";
    $colorcounter = $colorcounter - 2;
    }
    //<td><font size="1">cat</font></td>

      print("<tr><td bgcolor=$colorbg><font size=\"1\">$Source</font></td><td align=left bgcolor=$colorbg><font size=\"1\">$Topic</font></td><td align=left bgcolor=$colorbg><font size=\"1\">$Subtopic</font></td><td align=left bgcolor=$colorbg><font size=\"1\">$References</font></td></tr>");

    //print("<tr><td bgcolor=$colorbg><font size=\"1\">$Topic</font></td><td align=left     bgcolor=$colorbg><font size=\"1\">$Subtopic</td><td align=left bgcolor=$colorbg><font size=\"1\">$References</td></tr>");


    $colorcounter = $colorcounter + 1;
    }
    print("</table>");

  ?>

</body>

</HTML>
 
Hint:

All GET parameters need to be in the href attribute of the clicked link.
Code:
# add ES etc. to the constructed link in the code below
# Example
print '<td><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '&ES='.$_GET['ES'].'">' . $i . '</a> &nbsp</td>' ;
That's it.
 
Thank you. I will give this a try and hopefully it will solve my issues.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top