|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
MYSQL paging set and variables
Help!!
I have been playing around with Joe O'Donnell's Mysql Paging Class and everything works pretty much as advertised. There is one problem though that I can't seem to solve. When the query is hardcoded everything works just fine: the links take to all the right pages. But when the query has a variable in it passed from a from (i.e. "Select * from table_name where CID=$cid") the script only ends up working for the first page. When you click on one of the page number links, the original variable from the form is lost and the script seems to run a new query and returns new results. If anyone has any idea how to solve this, I would greatly appreciate it. thanks, Dan ![]() |
|
#2
|
|||
|
|||
|
Dan,
What's the code you're using for the problem you're having? It'll be helpful to determine the problem...
__________________
____________________________________________ Developer Shed Weekly Writer | DevArticles Forum Moderator Build Your Own KlipFolio Klip With PHP FrankManno.com - Under Construction Design Interactive Group - Under Construction |
|
#3
|
|||
|
|||
|
What version of php are you running?? and do you have register_globals on or off?
I think you maybe running a new version of php, thats why the limit isnt working |
|
#4
|
|||
|
|||
|
I was going to reply last night but fell asleep putting my 2 year old to bed. Oh, well!!
Anyway, I am running PHP 4.0.6 with Globals on (for now). As for the code: I am using the Recordset Paging Class by Joe O'Donnell, a form php file and a results file. The Recordset Class (as I reconstructed it from the O'Donnell article) is: <?php class RecNav { var $__linkId; var $__dbType; var $__query; var $__template; var $__templateHeader; var $__templateFooter; var $__recsPerPage; function RecNav(&$LinkIdentifier, $Query, $Template=DEFAULT_TEMPLATE, $TemplateHeader=DEFAULT_TEMPLATE_HEADER, $TemplateFooter=DEFAULT_TEMPLATE_FOOTER, $RecsPerPage=DEFAULT_NUM_RECS) { // Validate constructor parameters if(!@mysql_query("SELECT 1", $LinkIdentifier)) { die("MYSQL link identifier is invalid"); } else { $this->__linkId = $LinkIdentifier; } if(!ereg("^SELECT", $Query)) { die("Invalid query: query must start with 'SELECT'"); } else { $this->__query = $Query; } if($Template == "") { $this->__template = "<p><| row0 |></p><p><| row3 |></p>"; } else { $this->__template = $Template; } if($TemplateHeader == "") { $this->__templateHeader = DEFAULT_TEMPLATE_HEADER; } else { $this->__templateHeader = $TemplateHeader; } if($TemplateFooter == "") { $this->__templateFooter = DEFAULT_TEMPLATE_FOOTER; } else { $this->__templateFooter = $TemplateFooter; } if(!is_numeric($RecsPerPage) || $RecsPerPage < 1) { $this->__recsPerPage = DEFAULT_NUM_RECS; } else { $this->__recsPerPage = $RecsPerPage; } } function ShowRecs($Page=1) { $finalOutput = $this->__templateHeader; if($Page <= 1) { $Page = 1; $query = $this->__query . " LIMIT 0, " . $this->__recsPerPage; } else { $query = $this->__query . " LIMIT " . (($Page-1) * $this->__recsPerPage) . ", " . $this->__recsPerPage; } $result = mysql_query($query); $hasRecords = mysql_num_rows($result) == 0 ? false : true; if($hasRecords) { // At least one records returned from the query while($row = mysql_fetch_row($result)) { $newRow = $this->__template; for($i = 0; $i < mysql_num_fields($result); $i++) { $newRow = str_replace("<| row" . $i . " |>", $row[$i], $newRow); } $finalOutput .= $newRow; } } else { // No records returned from the query $newRow = $this->__template; $newRow = str_replace("<| row0 |>", "No records found", $newRow); // Replace all template tags with $newRow = ereg_replace("<\| row[0-9] \|>", " ", $newRow); $finalOutput .= $newRow; } $finalOutput .= $this->__templateFooter; // Build the recordset paging links $numTotalRecs = mysql_num_rows(mysql_query($this->__query)); $numPages = ceil($numTotalRecs / $this->__recsPerPage); $nav = ""; // Can we have a link to the previous page? if($Page > 1) $nav .= "<a href='$PHP_SELF?page=" . ($Page-1) . "'><< Prev</a> |"; for($i = 1; $i < $numPages+1; $i++) { if($Page == $i) { // Bold the page and dont make it a link $nav .= " <b>$i</b> |"; } else { // Link the page $nav .= " <a href='$PHP_SELF?page=$i'>$i</a> |"; } } // Can we have a link to the next page? if($Page < $numPages) $nav .= " <a href='$PHP_SELF?page=" . ($Page+1) . "'>Next >></a>"; // Strip the trailing pipe if there is one $nav = ereg_replace("\|$", "", $nav); $finalOutput .= "<div align='right'><font face='verdana' size='1'><br>Pages: $nav</font></div>"; return $finalOutput; } } ?> The form file is simply a test file with one drop down field: <html> <head> <title>Paged Results Demo</title> </head> <body> <form method="POST" name="search" action="paging.php"> <Select name="origin"> <Option value="Please Select">Please Select</option> <Option value="German">German</option> <Option value="Czech">Czech</option> <Option value="Italian">Italian</option> </select> <input type="submit" value="Start Search" name="Search"> <input type="reset" name="Reset" value="Reset"> </form> </body> </html> and the results display file: <html> <head> <title> Recordset Paging Example </title> </head> <body bgcolor="#FFFFFF"> <form method="post" name="test" action=""> <input type="hidden" name="origin" value="<?php echo $origin?>"></form> <?php require("class.recnav.php"); $s = mysql_connect("localhost", "xxx", "xxxxxx"); $d = mysql_select_db("dbname", $s); $r = new RecNav($s, "SELECT * FROM table_name where Bass_Origin='$origin'", $bodyTemplate, $headerTemplate, "", 1); echo $r->ShowRecs($page); ?> </body> </html> Set up like this, this works fine for the first page, but as soon as you click on one of the page number or next links, the $origin variable gets lost and a different query ends up being run (with a different result). If I hard code the Bass_Origin (Bass_Origin='German', for example), than everything works fine for all the page number links. Not to confuse matters, but I started playing around with PHP sessions. In one way, this helps immediately. If I put in : <?php session_start(); session_register("origin"); ?> then the variable sticks and gets passed on to all the page links. Everything is fine and great except that then I can't seem to change the value of the variable once it's set this way. I'll stop here for now before I confuse things farther. Thanks so much for your input. Dan |
|
#5
|
|||
|
|||
|
put this at the top of your display page
$origin = $_POST['origin']; With php 4.2 register globals are turned off for security reasons so you must use $_GET $_POST $_SESSION $_COOKIE or even just $_REQUEST |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > MYSQL paging set and variables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|