// some databases will return -1 on MoveLast() - change to MoveNext() while(!$rstest->EOF) { $rstest->MoveNext(); } $qryRecs = $rstest->_currentRow; } $rstest->Close(); if ($qryRecs == -1) return 0; } return $qryRecs; } /* Code originally from "Cornel G" This code will not work with SQL that has UNION in it Also if you are using CachePageExecute(), there is a strong possibility that data will get out of synch. use CachePageExecute() only with tables that rarely change. */ function &_adodb_pageexecute_all_rows(&$zthis, $sql, $nrows, $page, $inputarr=false, $arg3=false, $secs2cache=0) { $atfirstpage = false; $atlastpage = false; $lastpageno=1; // If an invalid nrows is supplied, // we assume a default value of 10 rows per page if (!isset($nrows) || $nrows <= 0) $nrows = 10; $qryRecs = false; //count records for no offset $qryRecs = _adodb_getcount($zthis,$sql,$inputarr,$secs2cache); $lastpageno = (int) ceil($qryRecs / $nrows); $zthis->_maxRecordCount = $qryRecs; // If page number <= 1, then we are at the first page if (!isset($page) || $page <= 1) { $page = 1; $atfirstpage = true; } // ***** Here we check whether $page is the last page or // whether we are trying to retrieve // a page number greater than the last page number. if ($page >= $lastpageno) { $page = $lastpageno; $atlastpage = true; } // We get the data we want $offset = $nrows * ($page-1); if ($secs2cache > 0) $rsreturn = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $offset, $inputarr, $arg3); else $rsreturn = &$zthis->SelectLimit($sql, $nrows, $offset, $inputarr, $arg3, $secs2cache); // Before returning the RecordSet, we set the pagination properties we need if ($rsreturn) { $rsreturn->_maxRecordCount = $qryRecs; $rsreturn->rowsPerPage = $nrows; $rsreturn->AbsolutePage($page); $rsreturn->AtFirstPage($atfirstpage); $rsreturn->AtLastPage($atlastpage); $rsreturn->LastPageNo($lastpageno); } return $rsreturn; } // Iv醤 Oliva version function &_adodb_pageexecute_no_last_page(&$zthis, $sql, $nrows, $page, $inputarr=false, $arg3=false, $secs2cache=0) { $atfirstpage = false; $atlastpage = false; if (!isset($page) || $page <= 1) { // If page number <= 1, then we are at the first page $page = 1; $atfirstpage = true; } if ($nrows <= 0) $nrows = 10; // If an invalid nrows is supplied, we assume a default value of 10 rows per page // ***** Here we check whether $page is the last page or whether we are trying to retrieve a page number greater than // the last page number. $pagecounter = $page + 1; $pagecounteroffset = ($pagecounter * $nrows) - $nrows; if ($secs2cache>0) $rstest = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr, $arg3); else $rstest = &$zthis->SelectLimit($sql, $nrows, $pagecounteroffset, $inputarr, $arg3, $secs2cache); if ($rstest) { while ($rstest && $rstest->EOF && $pagecounter>0) { $atlastpage = true; $pagecounter--; $pagecounteroffset = $nrows * ($pagecounter - 1); $rstest->Close(); if ($secs2cache>0) $rstest = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr, $arg3); else $rstest = &$zthis->SelectLimit($sql, $nrows, $pagecounteroffset, $inputarr, $arg3, $secs2cache); } if ($rstest) $rstest->Close(); } if ($atlastpage) { // If we are at the last page or beyond it, we are going to retrieve it $page = $pagecounter; if ($page == 1) $atfirstpage = true; // We have to do this again in case the last page is the same as the first //... page, that is, the recordset has only 1 page. } // We get the data we want $offset = $nrows * ($page-1); if ($secs2cache > 0) $rsreturn = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $offset, $inputarr, $arg3); else $rsreturn = &$zthis->SelectLimit($sql, $nrows, $offset, $inputarr, $arg3, $secs2cache); // Before returning the RecordSet, we set the pagination properties we need if ($rsreturn) { $rsreturn->rowsPerPage = $nrows; $rsreturn->AbsolutePage($page); $rsreturn->AtFirstPage($atfirstpage); $rsreturn->AtLastPage($atlastpage); } return $rsreturn; } function _adodb_getupdatesql(&$zthis,&$rs, $arrFields,$forceUpdate=false,$magicq=false) { if (!$rs) { printf(ADODB_BAD_RS,'GetUpdateSQL'); return false; } $fieldUpdatedCount = 0; // Get the table name from the existing query. preg_match("/FROM\s".ADODB_TABLE_REGEX."/i", $rs->sql, $tableName); // Get the full where clause excluding the word "WHERE" from // the existing query. preg_match('/\sWHERE\s(.*)/i', $rs->sql, $whereClause); $discard = false; // not a good hack, improvements? if ($whereClause) preg_match('/\s(LIMIT\s.*)/i', $whereClause[1], $discard); if ($discard) $whereClause[1] = substr($whereClause[1], 0, strlen($whereClause[1]) - strlen($discard[1])); // updateSQL will contain the full update query when all // processing has completed. $updateSQL = "UPDATE " . $tableName[1] . " SET "; $hasnumeric = (isset($rs->fields[0])); // Loop through all of the fields in the recordset for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) { // Get the field from the recordset $field = $rs->FetchField($i); // If the recordset field is one // of the fields passed in then process. if (isset($arrFields[$field->name])) { // If the existing field value in the recordset // is different from the value passed in then // go ahead and append the field name and new value to // the update query. $val = ($hasnumeric) ? $rs->fields[$i] : $rs->fields[$field->name]; if ($forceUpdate || strcmp($val, $arrFields[$field->name])) { // Set the counter for the number of fields that will be updated. $fieldUpdatedCount++; // Based on the datatype of the field // Format the value properly for the database $mt = $rs->MetaType($field->type); // "mike" patch and "Ryan Bailey" //PostgreSQL uses a 't' or 'f' and therefore needs to be processed as a string ('C') type field. if ((substr($zthis->databaseType,0,8) == "postgres") && ($mt == "L")) $mt = "C"; // is_null requires php 4.0.4 if (/*is_null($arrFields[$fieldname]) ||*/ $arrFields[$field->name] === 'null') $updateSQL .= $field->name . " = null, "; else switch($mt) { case 'null': case "C": case "X": case 'B': $updateSQL .= $field->name . " = " . $zthis->qstr($arrFields[$field->name],$magicq) . ", "; break; case "D": $updateSQL .= $field->name . " = " . $zthis->DBDate($arrFields[$field->name]) . ", "; break; case "T": $updateSQL .= $field->name . " = " . $zthis->DBTimeStamp($arrFields[$field->name]) . ", "; break; default: $updateSQL .= $field->name . " = " . (float) $arrFields[$field->name] . ", "; break; }; }; }; }; // If there were any modified fields then build the rest of the update query. if ($fieldUpdatedCount > 0 || $forceUpdate) { // Strip off the comma and space on the end of the update query. $updateSQL = substr($updateSQL, 0, -2); // If the recordset has a where clause then use that same where clause // for the update. if ($whereClause[1]) $updateSQL .= " WHERE " . $whereClause[1]; return $updateSQL; } else { return false; }; } function _adodb_getinsertsql(&$zthis,&$rs,$arrFields,$magicq=false) { $values = ''; $fields = ''; if (!$rs) { printf(ADODB_BAD_RS,'GetInsertSQL'); return false; } $fieldInsertedCount = 0; // Get the table name from the existing query. preg_match("/FROM\s".ADODB_TABLE_REGEX."/i", $rs->sql, $tableName); // Loop through all of the fields in the recordset for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) { // Get the field from the recordset $field = $rs->FetchField($i); // If the recordset field is one // of the fields passed in then process. if (isset($arrFields[$field->name])) { // Set the counter for the number of fields that will be inserted. $fieldInsertedCount++; // Get the name of the fields to insert $fields .= $field->name . ", "; $mt = $rs->MetaType($field->type); // "mike" patch and "Ryan Bailey" //PostgreSQL uses a 't' or 'f' and therefore needs to be processed as a string ('C') type field. if ((substr($zthis->databaseType,0,8) == "postgres") && ($mt == "L")) $mt = "C"; // Based on the datatype of the field // Format the value properly for the database if (/*is_null($arrFields[$fieldname]) ||*/ $arrFields[$field->name] === 'null') $values .= "null, "; else switch($mt) { case "C": case "X": case 'B': $values .= $zthis->qstr($arrFields[$field->name],$magicq) . ", "; break; case "D": $values .= $zthis->DBDate($arrFields[$field->name]) . ", "; break; case "T": $values .= $zthis->DBTimeStamp($arrFields[$field->name]) . ", "; break; default: $values .= (float) $arrFields[$field->name] . ", "; break; }; }; }; // If there were any inserted fields then build the rest of the insert query. if ($fieldInsertedCount > 0) { // Strip off the comma and space on the end of both the fields // and their values. $fields = substr($fields, 0, -2); $values = substr($values, 0, -2); // Append the fields and their values to the insert query. $insertSQL = "INSERT INTO " . $tableName[1] . " ( $fields ) VALUES ( $values )"; return $insertSQL; } else { return false; }; } ?> implemented Render_PageLinks(). Please note, this class is entirely unsupported, and no free support requests except for bug reports will be entertained by the author. My company also sells a commercial pagination object at http://phplens.com/ with much more functionality, including search, create, edit, delete records. */ class ADODB_Pager { var $id; // unique id for pager (defaults to 'adodb') var $db; // ADODB connection object var $sql; // sql used var $rs; // recordset generated var $curr_page; // current page number before Render() called, calculated in constructor var $rows; // number of rows per page var $linksPerPage=10; // number of links per page in navigation bar var $showPageLinks; var $gridAttributes = 'width=100% border=1 bgcolor=white'; // Localize text strings here var $first = '首页'; var $prev = '上一页'; var $next = '下一页'; var $last = '尾页'; var $moreLinks = '...'; var $startLinks = '...'; var $gridHeader = false; var $htmlSpecialChars = true; var $page = '页次:'; var $linkSelectedColor = 'red'; var $cache = 0; #secs to cache with CachePageExecute() var $header; //上页、下页的表示 var $param = ''; //---------------------------------------------- // constructor // // $db adodb connection object // $sql sql statement // $id optional id to identify which pager, // if you have multiple on 1 page. // $id should be only be [a-z0-9]* // function ADODB_Pager(&$db,$sql,$param = '',$id = 'adodb', $showPageLinks = false) { global $HTTP_SERVER_VARS,$PHP_SELF,$HTTP_SESSION_VARS,$HTTP_GET_VARS; $this->param = $param; $curr_page = $id.'_curr_page'; if (empty($PHP_SELF)) $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF']; $this->sql = $sql; $this->id = $id; $this->db = $db; $this->showPageLinks = $showPageLinks; $next_page = $id.'_next_page'; /* if (isset($HTTP_GET_VARS[$next_page])) { $HTTP_SESSION_VARS[$curr_page] = $HTTP_GET_VARS[$next_page]; } if (empty($HTTP_SESSION_VARS[$curr_page])) $HTTP_SESSION_VARS[$curr_page] = 1; ## at first page */ if (isset($HTTP_GET_VARS[$next_page])) { $this->curr_page = $HTTP_GET_VARS[$next_page]; } if (empty($this->curr_page)) $this->curr_page = 1; ## at first page //$this->curr_page = $HTTP_SESSION_VARS[$curr_page]; } //--------------------------- // Display link to first page function Render_First($anchor=true) { global $PHP_SELF; if ($anchor) { ?> first;?>   first   "; } } //-------------------------- // Display link to next page function render_next($anchor=true) { global $PHP_SELF; if ($anchor) { ?> next;?>   next   "; } } //------------------ // Link to last page // // for better performance with large recordsets, you can set // $this->db->pageExecuteCountRows = false, which disables // last page counting. function render_last($anchor=true) { global $PHP_SELF; if (!$this->db->pageExecuteCountRows) return; if ($anchor) { ?> last;?>   last   "; } } //--------------------------------------------------- // original code by "Pablo Costa" function render_pagelinks() { global $PHP_SELF; $pages = $this->rs->LastPageNo(); $linksperpage = $this->linksPerPage ? $this->linksPerPage : $pages; for($i=1; $i <= $pages; $i+=$linksperpage) { if($this->rs->AbsolutePage() >= $i) { $start = $i; } } $numbers = ''; $end = $start+$linksperpage-1; $link = $this->id . "_next_page"; if($end > $pages) $end = $pages; if ($this->startLinks && $start > 1) { $pos = $start - 1; $numbers .= "$this->startLinks "; } for($i=$start; $i <= $end; $i++) { if ($this->rs->AbsolutePage() == $i) $numbers .= "linkSelectedColor>$i "; else $numbers .= "$i "; } if ($this->moreLinks && $end < $pages) $numbers .= "$this->moreLinks "; return $numbers . '   '; } // Link to previous page function render_prev($anchor=true) { global $PHP_SELF; if ($anchor) { ?> prev;?>   prev   "; } } //-------------------------------------------------------- // Simply rendering of grid. You should override this for // better control over the format of the grid // // We use output buffering to keep code clean and readable. function RenderGrid() { global $gSQLBlockRows; // used by rs2html to indicate how many rows to display include_once(ADODB_DIR.'/tohtml.inc.php'); ob_start(); $gSQLBlockRows = $this->rows; rs2html($this->rs,$this->gridAttributes,$this->gridHeader,$this->htmlSpecialChars); $s = ob_get_contents(); ob_end_clean(); return $s; } //------------------------------------------------------- // Navigation bar // // we use output buffering to keep the code easy to read. function RenderNav() { ob_start(); if (!$this->rs->AtFirstPage()) { $this->Render_First(); $this->Render_Prev(); } else { $this->Render_First(false); $this->Render_Prev(false); } if ($this->showPageLinks){ $this->Render_PageLinks(); } if (!$this->rs->AtLastPage()) { $this->Render_Next(); $this->Render_Last(); } else { $this->Render_Next(false); $this->Render_Last(false); } $s = ob_get_contents(); ob_end_clean(); return $s; } //------------------- // This is the footer function RenderPageCount() { if (!$this->db->pageExecuteCountRows) return ''; $lastPage = $this->rs->LastPageNo(); if ($lastPage == -1) $lastPage = 1; // check for empty rs. return "$this->page ".$this->curr_page."/".$lastPage; } //----------------------------------- // Call this class to draw everything. function Render($rows=10) { global $ADODB_COUNTRECS; $this->rows = $rows; $savec = $ADODB_COUNTRECS; if ($this->db->pageExecuteCountRows) $ADODB_COUNTRECS = true; if ($this->cache) $rs = &$this->db->CachePageExecute($this->cache,$this->sql,$rows,$this->curr_page); else $rs = &$this->db->PageExecute($this->sql,$rows,$this->curr_page); $ADODB_COUNTRECS = $savec; $this->rs = &$rs; if (!$rs) { print "

Query failed: $this->sql

"; return; } if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage())) $this->header = $this->RenderNav(); else $this->header = " "; return $this->rs; //$grid = $this->RenderGrid(); //$footer = $this->RenderPageCount(); //$rs->Close(); //$this->rs = false; //$this->RenderLayout($header,$grid,$footer); } //------------------------------------------------------ // override this to control overall layout and formating function RenderLayout($header,$grid,$footer,$attributes='border=1 bgcolor=beige') { echo "
", $header, "
", $grid, "
", $footer, "
"; } function GetHeader() { return $this->header; } } ?> | * and Tomas V.V.Cox