home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / postgresrec.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  2.8 KB  |  139 lines

  1. PostgreSQL Recordset 
  2.  
  3. PostgreSQL recordset class 
  4.  
  5. <?php 
  6.  
  7. // 
  8. // Author: Razvan STANESCU <pappy@gecad.ro> 
  9. // 
  10.  
  11. class CPGRs 
  12.     var        $recs; 
  13.     var        $rows; 
  14.     var        $cols; 
  15.  
  16.     var        $row; 
  17.  
  18.     function CPGRs($pdb, $sql) 
  19.     { 
  20.         $this->recs = 0; 
  21.         $this->rows = 0; 
  22.         $this->cols = 0; 
  23.         $this->row  = 0; 
  24.  
  25.         $this->recs = pg_exec($pdb, $sql); 
  26.  
  27.         if( $this->recs ) { 
  28.             $this->rows = pg_numrows($this->recs); 
  29.             $this->cols = pg_numfields($this->recs); 
  30.             $this->first(); 
  31.             } 
  32.         return $this->recs && $this->rows > 0; 
  33.     } 
  34.  
  35.     function valid() 
  36.     { 
  37.         return $this->recs; 
  38.     } 
  39.  
  40.     function eof() 
  41.     { 
  42.         return $this->row == $this->rows; 
  43.     } 
  44.  
  45.     function bof() 
  46.     { 
  47.         return $this->row < 0; 
  48.     } 
  49.  
  50.     function first() 
  51.     { 
  52.         $this->row = 0; 
  53.     } 
  54.      
  55.     function last() 
  56.     { 
  57.         $this->row = $this->rows-1; 
  58.     } 
  59.  
  60.     function prev() 
  61.     { 
  62.         if( $this->row >= 0 ) { 
  63.             $this->row--; 
  64.             } 
  65.     } 
  66.  
  67.     function next() 
  68.     { 
  69.         if( $this->row < $this->rows ) { 
  70.             $this->row++; 
  71.             } 
  72.     } 
  73.  
  74.     function valueof($col) 
  75.     { 
  76.         if( $col < $this->cols ) { 
  77.             return pg_result($this->recs, $this->row, $col); 
  78.             } 
  79.         else { 
  80.             return  ""; 
  81.             } 
  82.     } 
  83.  
  84.     function showheader($col, $fmt =  "") 
  85.     { 
  86.         printf( "\t<th %s>%s</th>\n", $fmt, is_string($col) ? $col : pg_fieldname($this->recs, $col)); 
  87.     } 
  88.  
  89.     function showvalue($col, $fmt =  "", $def =  " ") 
  90.     { 
  91.         $v = $this->valueof($col); 
  92.         printf( "\t<td %s>%s</td>\n", $fmt, $v ==  "" ? $def : $v); 
  93.     } 
  94.  
  95.     function showurl($col, $fmt =  "") 
  96.     { 
  97.         $v = $this->valueof($col); 
  98.         if( $v !=  "" ) { 
  99.             printf( "\t<td %s> </td>\n", $fmt); 
  100.             } 
  101.         else { 
  102.             printf( "\t<td %s><a href=%s>%s</a></td>\n", $fmt, $v, $v); 
  103.             } 
  104.     } 
  105.  
  106.     function display() 
  107.     { 
  108.         if( !$this->valid() ) { 
  109.             return; 
  110.             } 
  111.  
  112.         printf( "<table cellspacing=1 cellpadding=1 border=1>\n"); 
  113.  
  114.         printf( "<tr>\n"); 
  115.         for( $c = 0; $c < $this->cols; $c++ ) { 
  116.             $this->showheader($c); 
  117.             } 
  118.         printf( "</tr>\n"); 
  119.  
  120.         $this->first(); 
  121.         while( !$this->eof() ) { 
  122.             printf( "<tr>\n"); 
  123.  
  124.             for( $c = 0; $c < $this->cols; $c++ ) { 
  125.                 $this->showvalue($c); 
  126.                 } 
  127.  
  128.             printf( "</tr>\n"); 
  129.             $this->next(); 
  130.             } 
  131.         printf( "</table\n"); 
  132.     } 
  133.  
  134. }; 
  135.  
  136. ?> 
  137.  
  138.