Login   Register  
PHP Classes
elePHPant
Icontem

File: class.QueryPrint.inc

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Daniel Kushner  >  QueryPrint  >  class.QueryPrint.inc  >  Download  
File: class.QueryPrint.inc
Role: ???
Content type: text/plain
Description: The main class that queries the database and calls the printing methods
Class: QueryPrint
Author: By
Last change:
Date: 2001-11-13 12:19
Size: 3,990 bytes
 

Contents

Class file image Download
<?php
/*
    QueryPrint ver 1.1.2
    Author: Daniel Kushner
    Email: daniel@websapp.com
    Release: 13 Nov 2001
    Copyright 2001
*/
class QueryPrint {
    
    /* public: connection parameters */
    var $host     = 'localhost';
    var $database = 'websapp';
    var $user     = '';
    var $password = '';
    
    var $printModule = 'QP_Default.mod';
    var $printer;
    
    var $linkId;
    var $queryId;
    
    /*
        Contructs a new QueryPrint object.
        $printModule - the name of the file containing a 'QP_Printer' class.
                       if no printModule is given then the default is used: QP_Printer.mod
     */
    function QueryPrint($printModule = '') {
        if($printModule != '') {
            $this->printModule = $printModule;
        }
        include_once($this->printModule);
        $this->printer = new QP_Printer();
    }
    
    
    /*
        Sends the given query to the database
     */
    function query($query) {
        global $HTTP_GET_VARS;
        
        if($this->linkId == 0) {
            $this->connect();
        }
        
        // If the printer module defines the number of records per page a 
        // limit is added to the query.
        if(isset($this->printer->RecordsPerPage) && ($this->printer->RecordsPerPage > 0)) {
            (!isset($HTTP_GET_VARS['QP_From']) || (int)$HTTP_GET_VARS['QP_From'] < 0) ? 
                $QP_From = 0 : 
                $QP_From = (int)$HTTP_GET_VARS['QP_From'];
            $numberOfRecords = $this->printer->RecordsPerPage + 1;
            $query .= " LIMIT $QP_From, $numberOfRecords";
            
        }

        $this->queryId = mysql_query($query, $this->linkId)
            or die ('Invalid query');
        return min(mysql_num_rows($this->queryId), $this->printer->RecordsPerPage) ;
    }
    
    /*
        Prints the query in a table using the print module
    */
    function printQuery() {
        global $HTTP_GET_VARS;
        
        if($this->queryId == 0) {
            return;
        }
        if(is_array($arr = mysql_fetch_assoc($this->queryId))) {
            foreach($arr as $title => $val) {
                $header[] = $title;
                $row[$title] = $val;
            }
            $this->printer->OpenTable();
            $this->printer->PrintHead($header);
            $this->printer->PrintRow($row);
            $printedRows = 1;
            while(is_array($arr = mysql_fetch_assoc($this->queryId)) && 
                  ($printedRows < $this->printer->RecordsPerPage || $this->printer->RecordsPerPage <= 0)) {
                $this->printer->PrintRow($arr);
                $printedRows++;
            }
            $this->printer->CloseTable();
            
            if(isset($this->printer->RecordsPerPage) && ($this->printer->RecordsPerPage > 0)) {
                
                (!isset($HTTP_GET_VARS['QP_From']) || (int)$HTTP_GET_VARS['QP_From'] < 0) ? 
                    $QP_From = 0 : 
                    $QP_From = (int)$HTTP_GET_VARS['QP_From'];
                if($QP_From > 0) {
                    $this->printer->PreviousLink($QP_From - $this->printer->RecordsPerPage);
                }
                if(mysql_num_rows($this->queryId) > $this->printer->RecordsPerPage) {
                    $this->printer->NextLink($QP_From + $this->printer->RecordsPerPage);
                }
            }
                
        } else {
            $this->printer->EmptyQuery();
        }
    }
    
    
    /*  PRIVATE FUNCTIONS   */
     
    /*
        Creates a connection to the database
     */
    function connect() {
        $this->linkId = mysql_pconnect ($this->host, $this->user, $this->password)
            or die ('Could not connect');
        mysql_select_db($this->database, $this->linkId)
            or die ('Can not use database $this->database');
    }
            
}
?>