Login   Register  
PHP Classes
elePHPant
Icontem

File: sample.using.qtag.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Tom Schaefer  >  QDataObject  >  sample.using.qtag.php  >  Download  
File: sample.using.qtag.php
Role: Example script
Content type: text/plain
Description: Sample Table using QTag
Class: QDataObject
Access MySQL query results as objects
Author: By
Last change: upd
Date: 2008-07-20 02:25
Size: 2,566 bytes
 

Contents

Class file image Download
<?php

    
include_once 'config.inc.php';
    
    
$link mysqli_connect(
        
db_default_host
        
db_default_user
        
db_default_password,
        
db_default_database
    
) OR die(mysqli_error());



    
$sql "SELECT 
    CustomerID,
    CompanyName,
    ContactName,
    City,
    Country
    FROM customers"

    
// if table column names are written underscored, then they will be automatically
    // converted to camel-case notation for php access
    // the northwind field names are usually written camel-cased.

    
$pageSize = ($_GET["s"]?(int)$_GET["s"]:2);
    
    
$result mysqli_query($link$sql);

    
$dataObject = new QDataObject::getInstance('mysqli',$result$link);
    
$dataObject->byPage((int)$_GET["p"],$pageSize);

    
// TABLE
    
$table QTag::factory("table")
                ->
setAttribute("border",1)
                ->
setAttribute("cellspacing",2)
                ->
setAttribute("cellpadding",2);
    
// THEAD
    
$tr QTag::factory("tr");
    foreach(
$dataObject->getFieldNames() as $columnName) {
        
$th QTag::factory("th")->add($columnName);
        
$tr->add($th);
    }
    
$thead QTag::factory("thead")->add($tr);
    
$table->add($thead);
    
    
// TBODY
    
$tbody QTag::factory("tbody");
    for(
$i=0;$i<$dataObject->getAmountOfRows();$i++) {
        
$tr QTag::factory("tr");
        foreach(
$dataObject->getFieldNames() as $columnName) {
            
$get "get".$columnName;
            
$data $dataObject->$get($i);
            
$td QTag::factory("td")->add(empty($data) ? '&nbsp;':$data);
            
$tr->add($td);
        }
        
$tbody->add($tr);
    }
    
$table->add($tbody);
    
    
// TFOOT
    
$td QTag::factory("td")->setAttribute("colspan"$dataObject->getNumFields());
    
$tr QTag::factory("tr")->add($td);    
    
$tfoot QTag::factory("tfoot")->add($tr);
    
    
$table->add($tfoot);
    
    
// Pagination
    
$records QTag::factory("span")->add($dataObject->getNumRows().' records &nbsp;');
    
$table->getChild(2)->getFirstChild()->getFirstChild()->add($records);
    
    if(
$dataObject->getActivePage()>1) {
        
$previous QTag::factory("a")->setAttribute("href",'?p='.($dataObject->getActivePage()-1).'&s='.$pageSize)->add('&lt;');
        
$table->getChild(2)->getFirstChild()->getFirstChild()->add($previous);
    }
    
    
$active QTag::factory("b")->add(($dataObject->getActivePage()));
    
$table->getChild(2)->getFirstChild()->getFirstChild()->add($active);
    
    if(
$dataObject->getActivePage() < $dataObject->getNumOfPages()) {
        
$next QTag::factory("a")->setAttribute("href",'?p='.($dataObject->getActivePage()+1).'&s='.$pageSize)->add('&gt;');
        
$table->getChild(2)->getFirstChild()->getFirstChild()->add($next);
    }
    
    
    echo(
$table->doRender());