Location: PHPKode > scripts > nbrecs class > class.nbrecs.php
<?
class nbrecs {

	//   NBRECS CLASS
	//   ------------
	//   Author  : Copyright Pierre FAUQUE, hide@address.com, jun 2009
	//   Usage   : Display informations about all tables of a database.
	//   License : GPL (http://www.gnu.org/licenses/gpl-2.0.html)
	//   Version : 1.0
	//
	//   <DELETE>
	//
	//   Example of use:
	//         ...
	//         require("class.nbrecs.php");
	//         $stats = new nbrecs();
	//         $stats->showtables();
	//         ...
	//
	//   Note:
	//
	//      You can provide informations about your MySQL connection into this class
	//      (attributes: server, database, user, pass) or when you instanciate the class
	//      ex: $stats = new nbrecs("mysql_server","database_name","mysql_user","pass")
	//      MySQL server can be a full qualified name or an IP number
	//
	//   </DELETE>
	//
	// ==========================================================================
	// ATTRIBUTES OF THIS CLASS
	// All texts are written here. You can use the template for your own usage
	// to translate and define the original texts into your own language.
	// --------------------------------------------------------------------------
	// English texts. Must be commented if texts in another language are defined.
	var $t_nosrv  = "server unreachable";                // server unreachable
	var $t_nobase = "database unreachable";              // database unreachable
	var $t_table  = array("table","tables");             // table (singular, plural)
	var $t_recs   = "records";                           // records
	var $t_nbrec  = "nb recs.";                          // number of records
	var $t_incr   = "inc.";                              // next increment
	var $t_size   = array("bytes","Kb","Mb");            // sizes (different units)
	var $t_update = "updated on";                        // update
	var $t_comm   = "comment";                           // table comment
	var $t_today  = "modified today";                    // modified today
	var $t_yest   = "modified yesterday";                // modified yesterday
	var $t_byest  = "modified before yesterday";         // modified before yesterday
	var $t_other  = "modified earlier";                  // modified other day

	// Template for texts in your own language (define and uncomment them, then comment other texts)
	// var $t_nosrv  = "";                               // server unreachable
	// var $t_nobase = "";                               // database unreachable
	// var $t_table  = array("","");                     // table (singular, plural)
	// var $t_recs   = "";                               // records
	// var $t_nbrec  = "";                               // number of records
	// var $t_incr   = "";                               // next increment
	// var $t_size   = array("","","");                  // number of size (different units)
	// var $t_update = "";                               // update
	// var $t_comm   = "";                               // table comment
	// var $t_today  = "";                               // modified today
	// var $t_yest   = "";                               // modified yesterday
	// var $t_byest  = "";                               // modified before yesterday
	// var $t_other  = "";                               // modified other day

	// French texts. (décommentez ci-dessous et commentez les textes précédents)
	// var $t_nosrv  = "serveur inaccessible";           // server unreachable
	// var $t_nobase = "base inaccessible";              // database unreachable
	// var $t_table  = array("table","tables");          // table (singular, plural)
	// var $t_recs   = "enregistrements";                // records
	// var $t_nbrec  = "nb enr.";                        // number of records
	// var $t_incr   = "inc.";                           // next increment
	// var $t_size   = array("octets","Ko","Mo");        // size (bytes, Kb, Mb)
	// var $t_update = "maj le";                         // update
	// var $t_comm   = "r&ocirc;le table";               // table comment
	// var $t_today  = "modifi&eacute;e aujourd'hui";    // modified today
	// var $t_yest   = "modifi&eacute;e hier";           // modified yesterday
	// var $t_byest  = "modifi&eacute;e avant-hier";     // modified the day before yesterday
	// var $t_other  = "modifi&eacute;e plus t&ocirc;t"; // modified other day

	// End of texts. Eventual translation stops here
	// --------------------------------------------------------------------------

	// Informations for database connection
	var $server   = "000.000.000.000";  // IP or name of MySQL server
	var $base     = "bbbbbbb";          // database name
	var $user     = "uuuuuuu";          // mysql user
	var $pass     = "ppppppp";          // password for mysql user

	// Format and styles
	var $fmtdath  = "d/m/Y H:i:s";                        // date and hour format
	var $stsep    = "solid 1px #808080";                  // style for separator
	var $sttd     = "font-family:arial; font-size:10pt;"; // style for table data
	var $stt      = "color:#FFFFFF; background-color:#000080; border-right:solid 1px #C0C0C0";   // style for today
	var $sty      = "color:#000000; background-color:#7070FF; border-right:solid 1px #C0C0C0;";  // style for yesterday
	var $stb      = "color:#000000; background-color:#B0B0B0; border-right:solid 1px #C0C0C0;";  // style for the day before yesterday
	var $sto      = "color:#000000; background-color:#FFFFFF; border-right:solid 1px #C0C0C0;";  // style for all other days

	// Count attributes
	var $nbtd     = 0;      // number of tables modified today
	var $nby      = 0;      // number of tables modified yesterday
	var $nbby     = 0;      // number of tables modified before yesterday
	var $nbo      = 0;      // number of tables modified the other days
	var $nbt      = 0;      // total number of tables
	var $size     = 0;      // size of the database
	var $recs     = 0;      // total of records

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Constructor
	// If specific datas for connection are provided, use them
	function nbrecs($server="",$base="",$user="",$pass="") {
		if($server) { $this->server = $server; }
		if($base)   { $this->base   = $base;   }
		if($user)   { $this->user   = $user;   }
		if($pass)   { $this->pass   = $pass;   }
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Define and write styles for the table
	function writestyles() {
		$styles  = "<style type=\"text/css\">\n";
		$styles .= "td,th { $this->sttd }\n";
		$styles .= ".t { $this->stt }\n.y { $this->sty }\n";
		$styles .= ".b { $this->stb }\n.o { $this->sto }\n";
		$styles .= "</style>\n";
		echo $styles;
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	function begintable() {
		echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
		echo "<tr><th>".$this->t_table[0]."</th><th>$this->t_nbrec</th><th>$this->t_incr</th>";
		echo "<th>".$this->t_size[0]."</th><th>$this->t_update</th><th>$this->t_comm</th></tr>\n";
		echo "<tr><td colspan=\"6\" style=\"border-top:$this->stsep\">&nbsp;</td></tr>\n";
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Write the end of the table and informations about totals.
	function endtable() {
		$ko = $this->size/1024; $mo = $ko/1024;
		echo "<tr><td colspan=\"6\" style=\"border-bottom:solid 1px #808080\">&nbsp;</td></tr>\n";
		echo "<tr><td colspan=\"6\" class=\"t\">&nbsp;$this->t_today ($this->nbtd)&nbsp;</td></tr>\n";
		echo "<tr><td colspan=\"6\" class=\"y\">&nbsp;$this->t_yest ($this->nby)&nbsp;</td></tr>\n";
		echo "<tr><td colspan=\"6\" class=\"b\">&nbsp;$this->t_byest ($this->nbby)&nbsp;</td></tr>\n";
		echo "<tr><td colspan=\"6\" style=\"border-bottom:$this->stsep\">&nbsp;$this->t_other ($this->nbo)&nbsp;</td></tr>\n";
		echo "<tr><td colspan=\"6\">&nbsp;$this->base@$this->server: $this->nbt ".$this->t_table[1]." &nbsp;/&nbsp; ";
		echo $this->recs." ".$this->t_recs. " &nbsp;/&nbsp; ";
		echo $this->size." ".$this->t_size[0]." - ";
		echo round($ko,2)." ".$this->t_size[1]." - ";
		echo round($mo,2)." ".$this->t_size[2]."&nbsp;</td></tr>\n";
		echo "</table>\n";
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// display informations about tables
	function writetable() {
		$this->begintable();
		$tday  = mktime(0,0,0,date("m"),date("d"),  date("Y"));   // timestamp today
		$yest  = mktime(0,0,0,date("m"),date("d")-1,date("Y"));   // timestamp yesterday
		$byest = mktime(0,0,0,date("m"),date("d")-2,date("Y"));   // timestamp before yesterday
		$w = " style='border-right:0px'";

		if(!@$link = mysql_pconnect($this->server,$this->user,$this->pass)) { exit("$this->t_nosrv"); }
		if(!@$ok = mysql_select_db($this->base,$link)) { exit("$this->t_nobase"); }
		$result = mysql_query("show table status;",$link);

		while($r = mysql_fetch_object($result)) {
			$strtt = strtotime($r->Update_time);      // date and hour of the last update (for comparison)
			$lud  = date("$this->fmtdath",$strtt);    // date and hour of the last update (for display)
			$tslud = mktime(0,0,0,date("m",$strtt),date("d",$strtt),date("Y",$strtt)); // timestamp of the last update
			switch($tslud) {
				case $tday:  $s = " class=\"t\""; $this->nbtd++; break;
				case $yest:  $s = " class=\"y\""; $this->nby++;  break;
				case $byest: $s = " class=\"b\""; $this->nbby++; break;
				default:     $s = " class=\"o\""; $this->nbo++;  break;
			}
			$this->nbt++; $this->size += $r->Data_length; $this->recs += $r->Rows;
			if($r->Auto_increment) { $ai = $r->Auto_increment; } else { $ai = "&nbsp;"; }
			echo "<tr><td$s>&nbsp;$r->Name&nbsp;</td><td align=\"right\"$s>&nbsp;$r->Rows&nbsp;</td>";
			echo "<td align=\"right\"$s>&nbsp;$ai&nbsp;</td><td align=\"right\"$s>&nbsp;";
			echo "$r->Data_length&nbsp;</td><td$s>&nbsp;$lud&nbsp;</td><td$w>&nbsp;$r->Comment&nbsp;</td></tr>\n";
		}
		$this->endtable();
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Do all
	function showtables() {
		$this->writestyles();
		$this->writetable();
	}
}
?>
Return current item: nbrecs class