<?php
/*
* @version V1.1 2002/July/12 (c) Erh-Wen,Kuo (erhwenkuo@yahoo.com). All rights reserved.
* Released under both BSD license and Lesser GPL library license.
* Whenever there is any discrepancy between the two licenses,
* the BSD license will take precedence.
*
* purpose: providing a easy method to display mysql sql content.
*/
/* New class method:SqlAdmStart() is implemented at V1.1,2002/July/12 */
class Sql2Table{
var $db_host;
var $db_user;
var $db_pwd;
var $db_name;
var $db_link;
var $db_query;
var $navigator;
var $pages;
var $grids;
var $limit;
/*Sql2Table is class constructor
Return: None
*/
function Sql2Table($db_host="localhost",$db_user="",$db_pwd="",$db_name="")
{
$this->db_host=$db_host;
$this->db_user=$db_user;
$this->db_pwd=$db_pwd;
$this->db_name=$db_name;
//connect mysql database
$this->db_link=mysql_pconnect($this->db_host,$this->db_user,$this->db_pwd) or die("Mysql database connecion failed!");
}//Sql2Table() END
/*SqlStart() is one of the key method to let this class works(the other one is SqlAdmStart()).
It needs to be called everytime using this class.(If you want to add "Edit","Insert"&"Delete"
link when display table, then you need to use SqlAdmStart() instead!!
Return: None
*/
function SqlStart($query="",$offset=0,$limit=10)
{
if(!isset($limit) or $limit==0){
$limit=10;
}
if(!isset($offset)){
$offset=0;
}
//localize text string
$first = '<code>|<</code>';
$prev = '<code><<</code>';
$next = '<code>>></code>';
$last = '<code>>|</code>';
//connect mysql database
mysql_select_db($this->db_name,$this->db_link);
//get origin result
$numresults=mysql_query($query);
//get the totoal number of rows
$numrows=mysql_num_rows($numresults);
//calculate total pages
$totalpages=intval($numrows/$limit);
if($numrows%$limit){
$totalpages++;
}
//calculate current pages
if($offset==0){
$currentpage=1;
}else{
$currentpage=($offset/$limit)+1;
}
//construct page string
$pages="Pages: $currentpage / $totalpages";
//construct navigator string
if ($currentpage>1){
$navigator.="<a href=\"$PHP_SELF?offset=0\">$first</a> \n";
}else{
$navigator.="$first \n";
}
if ($currentpage>1) { // bypass PREV link if offset is 0
$prevoffset=$offset-$limit;
$navigator.="<a href=\"$PHP_SELF?offset=$prevoffset\">$prev</a> \n";
}else{
$navigator.="$prev \n";
}
// check to see if last page
if ($currentpage<$totalpages) {
// not last page so give NEXT link
$newoffset=$offset+$limit;
$navigator.="<a href=\"$PHP_SELF?offset=$newoffset\">$next</a> \n";
}else{
$navigator.="$next \n";
}
if($currentpage<$totalpages){
$navigator.="<a href=\"$PHP_SELF?offset=".($limit*($totalpages-1))."\">$last</a><p>\n";
}else{
$navigator.="$last<p>\n";
}
// get display results
$query.=" limit $offset,$limit";
#echo "$query";
$result=mysql_query($query);
$numoffields=mysql_num_fields($result);
// now you can display the results returned
$grids.="<table border=1 bgcolor=white>\n";
$grids.=" <tr>\n";
for($i=0;$i<$numoffields;$i++){
$grids.=" <th>".mysql_field_name($result,$i)."</th>\n";
}
$grids.=" </tr>\n";
while ($data=mysql_fetch_array($result)) {
$grids.=" <tr>\n";
// include code to display results as you see fit
for($i=0;$i<$numoffields;$i++){
if(!isset($data[$i]) or $data[$i]==""){$data[$i]=' ';}
$grids.=" <td>".$data[$i]."</td>\n";
}//for END
$grids.=" </tr>\n";
}
$grids.="</table>\n";
//put these string to variables
$this->navigator=$navigator;
$this->pages=$pages;
$this->grids=$grids;
}//SqlStart() END
/*SqlAdmStart() is the other key method to let this class works.It needs to be called
everytime using this class if you want this class add "Edit","Delete" & "Insert" link
when displaing table.This method would be handy when you want to modify some record.
Return: None
*/
function SqlAdmStart($action,$key,$query="",$offset=0,$limit=10)
{
if(!isset($action)){
$action=$PHP_SELF;
}
if(!isset($limit) or $limit==0){
$limit=10;
}
if(!isset($offset)){
$offset=0;
}
//localize text string
$first = '<code>|<</code>';
$prev = '<code><<</code>';
$next = '<code>>></code>';
$last = '<code>>|</code>';
//connect mysql database
mysql_select_db($this->db_name,$this->db_link);
//get origin result
$numresults=mysql_query($query);
//get the totoal number of rows
$numrows=mysql_num_rows($numresults);
//calculate total pages
$totalpages=intval($numrows/$limit);
if($numrows%$limit){
$totalpages++;
}
//calculate current pages
if($offset==0){
$currentpage=1;
}else{
$currentpage=($offset/$limit)+1;
}
//construct page string
$pages="Pages: $currentpage / $totalpages";
//construct navigator string
if ($currentpage>1){
$navigator.="<a href=\"$PHP_SELF?offset=0\">$first</a> \n";
}else{
$navigator.="$first \n";
}
if ($currentpage>1) { // bypass PREV link if offset is 0
$prevoffset=$offset-$limit;
$navigator.="<a href=\"$PHP_SELF?offset=$prevoffset\">$prev</a> \n";
}else{
$navigator.="$prev \n";
}
// check to see if last page
if ($currentpage<$totalpages) {
// not last page so give NEXT link
$newoffset=$offset+$limit;
$navigator.="<a href=\"$PHP_SELF?offset=$newoffset\">$next</a> \n";
}else{
$navigator.="$next \n";
}
if($currentpage<$totalpages){
$navigator.="<a href=\"$PHP_SELF?offset=".($limit*($totalpages-1))."\">$last</a><p>\n";
}else{
$navigator.="$last<p>\n";
}
// get display results
$query.=" limit $offset,$limit";
#echo "$query";
$result=mysql_query($query);
$numoffields=mysql_num_fields($result);
// now you can display the results returned
$grids.="<table border=1 bgcolor=white>\n";
$grids.=" <tr>\n";
for($i=0;$i<$numoffields;$i++){
$grids.=" <th>".mysql_field_name($result,$i)."</th>\n";
}
$grids.=" <th colspan=2><a href=\"$action?action=insert\">Insert</a></th>\n";
$grids.=" </tr>\n";
while ($data=mysql_fetch_array($result)) {
$grids.=" <tr>\n";
// include code to display results as you see fit
for($i=0;$i<$numoffields;$i++){
if(!isset($data[$i]) or $data[$i]==""){$data[$i]=' ';}
$grids.=" <td>".$data[$i]."</td>\n";
}//for END
$grids.=" <td><a href=\"$action?action=edit&$key=".$data[$key]."\">Edit</a></td>\n";
$grids.=" <td><a href=\"$action?action=delete&$key=".$data[$key]."\">Delete</a></td>\n";
$grids.=" </tr>\n";
}
$grids.="</table>\n";
//put these string to variables
$this->navigator=$navigator;
$this->pages=$pages;
$this->grids=$grids;
}//SqlAdmStart() END
/*GetNav() would return table navigation string
Return: String
*/
function GetNav()
{
return $this->navigator;
}//GetNav() END
/*GetPages() would return table pages string
Return: String
*/
function GetPages()
{
return $this->pages;
}//GetPages() END
/*GetGrids() would return a format HTML table string displaying SQL content
Return: String
*/
function GetGrids()
{
return $this->grids;
}//GetGrids() END
/*ShowTable() method would return a string combine Pages,Navigation&Sql Content
in HTML table format. Easy for debugging!!
Return: String
*/
function ShowTable()
{
$show="";
$show.="<table border=1 bgcolor=beige>\n";
$show.=" <tr>\n";
$show.=" <td>\n".$this->navigator."</td>\n";
$show.=" </tr>\n";
$show.=" <tr>\n";
$show.=" <td>\n".$this->grids."</td>\n";
$show.=" </tr>\n";
$show.=" <tr>\n";
$show.=" <td>\n".$this->pages."</td>\n";
$show.=" </tr>\n";
$show.="</table>\n";
return $show;
}//ShowTable() END
}//class Sql2Table END
?> |