PHP Classes

File: sample.mysqli.version.0.2..table.php

Recommend this page to a friend!
  Packages of Tom Schaefer   QDataObject   sample.mysqli.version.0.2..table.php   Download  
File: sample.mysqli.version.0.2..table.php
Role: Example script
Content type: text/plain
Description: Sample output as table
Class: QDataObject
Access MySQL query results as objects
Author: By
Last change: chg
Date: 18 years ago
Size: 1,903 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($result, $link);
    $dataObject->byPage((int)$_GET["p"],$pageSize);

    $html = '<table border="1" cellspacing="2" cellpadding="2">';
    $html.='<thead>';
    $html.='<tr>';
    foreach($dataObject->getFieldNames() as $columnName) {
        $html.='<th>';
        $html.=$columnName;
        $html.='</th>';
    }
    $html.='</tr>';
    $html.='</thead>';

    $html.='<tbody>';
    for($i=0;$i<$dataObject->getAmountOfRows();$i++) {
        $html.='<tr>';
        foreach($dataObject->getFieldNames() as $columnName) {
            $html.='<td>';
            $get = "get".$columnName;
            $data = $dataObject->$get($i);
            $html.= empty($data) ? '&nbsp;':$data;
            $html.='</td>';
        }
        $html.='</tr>';
    }
    $html.='</tbody>';
    $html.='<tfoot>';
    $html.='<tr>';
    $html.='<td colspan="'.$dataObject->getNumFields().'">';
    $html.=$dataObject->getNumRows().' records &nbsp;';
    if($dataObject->getActivePage()>1) {
        $html.='<a href="?p='.($dataObject->getActivePage()-1).'&s='.$pageSize.'">&lt;</a>';
    }
    $html.='<b>'.($dataObject->getActivePage()).'</b>';
    if($dataObject->getActivePage() < $dataObject->getNumOfPages()) {
        $html.='<a href="?p='.($dataObject->getActivePage()+1).'&s='.$pageSize.'">&gt;</a>';
    }
    $html.='</td>';
    $html.='</tr>';
    $html.='</tfoot>';

    $html .= '</table>';
    echo $html;