Login   Register  
PHP Classes
elePHPant
Icontem

File: sql2table.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Erh-Wen,Kuo  >  sql2table  >  sql2table.class.php  >  Download  
File: sql2table.class.php
Role: ???
Content type: text/plain
Description: sql2table class file
Class: sql2table
Easy way to display Mysql sql content.
Author: By
Last change: SqlAdmStart() is the new class method for this update. It would add "Edit","Delete" & "Insert" link when displaing table.This method would be handy when you want to do some management in specific table.
Date: 2002-07-13 07:24
Size: 8,504 bytes
 

Contents

Class file image Download
<?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>|&lt;</code>';
			$prev = '<code>&lt;&lt;</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> &nbsp; \n";
			}else{
				$navigator.="$first &nbsp; \n";
			}

			if ($currentpage>1) { // bypass PREV link if offset is 0
				$prevoffset=$offset-$limit;
				$navigator.="<a href=\"$PHP_SELF?offset=$prevoffset\">$prev</a> &nbsp; \n";
			}else{
				$navigator.="$prev &nbsp; \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> &nbsp; \n";
			}else{
				$navigator.="$next &nbsp; \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]='&nbsp;';}
					$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>|&lt;</code>';
			$prev = '<code>&lt;&lt;</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> &nbsp; \n";
			}else{
				$navigator.="$first &nbsp; \n";
			}

			if ($currentpage>1) { // bypass PREV link if offset is 0
				$prevoffset=$offset-$limit;
				$navigator.="<a href=\"$PHP_SELF?offset=$prevoffset\">$prev</a> &nbsp; \n";
			}else{
				$navigator.="$prev &nbsp; \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> &nbsp; \n";
			}else{
				$navigator.="$next &nbsp; \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]='&nbsp;';}
					$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
?>