Login   Register  
PHP Classes
elePHPant
Icontem

File: datamanage/php_inc/myclass.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Mathew  >  Data Manager  >  datamanage/php_inc/myclass.php  >  Download  
File: datamanage/php_inc/myclass.php
Role: Class source
Content type: text/plain
Description: class1
Class: Data Manager
Retrieve and store MySQL records using Web forms
Author: By
Last change:
Date: 8 months ago
Size: 15,513 bytes
 

Contents

Class file image Download
<?php
class myclass 
{
	var $DBASE 			= "datbase";      // Set name of database to use
	var $USER 			= "root";			  // Set database username
	var $PASS 			= "password";				  // Set darabase R/W password
	var $SERVER 		= "localhost";	  // Set server name
	var $CONN="";
	var $HOST = "";

	function __construct() 	{
		$user = $this->USER;
		$pass = $this->PASS;
		$server = $this->SERVER;
		$dbase = $this->DBASE;
		$conn = mysql_connect($server,$user,$pass) or die("Connection attempt failed - ".mysql_error());
		if(!$conn) {
			$this->error("Connection attempt failed");
		}
		if(!mysql_select_db($dbase)) {
			$this->error("Dbase Select failed");
		}
		$this->CONN = $conn;
		return true;
	}

	function close()
	{
		$conn = $this->CONN ;
		$close = mysql_close($conn);
		if(!$close) {
			$this->error("Connection close failed");
		}
		return true;
	}
	function error($text)
	{
		$no = mysql_errno();
		$msg = mysql_error();
		exit;
	}
	function select ($sql="")
	{
		if(empty($sql)) { return false; }
		if(!preg_match("/^select/",$sql) && !preg_match("/^show/",$sql))
		{
			echo "wrongquery<br>$sql<p>";
			echo "<H2>Wrong function silly!</H2>\n";
			return false;
		}
		if(empty($this->CONN)) { return false; }
		$conn = $this->CONN;
		$results = mysql_query($sql,$conn);
		if( (!$results) or (empty($results)) ) {
			return false;
		}
		$count = 0;
		$data = array();
		while ( $row = mysql_fetch_array($results))
		{
			foreach($row as $key=>$value)
			{
				$row[$key] = htmlentities($row[$key]);
			}
			$data[$count] = $row;
			$count++;
		}
		mysql_free_result($results);
		return $data;
	}

	function affected($sql="")
	{
		if(empty($sql)) { return false; }
		if(!preg_match("/^select/",$sql))
		{
			echo "wrongquery<br>$sql<p>";
			echo "<H2>Wrong function silly!</H2>\n";
			return false;
		}
		if(empty($this->CONN)) { return false; }
		$conn = $this->CONN;
		$results = mysql_query($sql,$conn);
		if( (!$results) or (empty($results)) ) {
			return false;
		}
		$tot=0;
		$tot=mysql_affected_rows();
		return $tot;
	}

	function sql_query($sql="")
	{	if(empty($sql)) { return false; }
		if(empty($this->CONN)) { return false; }
		$conn = $this->CONN;
		$results = mysql_query($sql,$conn) or die("query fail".mysql_error()); 
		if(!$results)
		{   $message = "Query went bad!";
			$this->error($message);
			return false;
		}		
		if(!preg_match("/^select/",$sql)){
			return true; }
		else {
			$count = 0;
			$data = array();
			while ( $row = mysql_fetch_array($results))	{
				foreach($row as $key=>$value)
				{
					$row[$key] = htmlentities($row[$key]);
				}
				$data[$count] = $row;
				$count++;
			}

			mysql_free_result($results);
			return $data;
	 	}
	}	


	function getfields($table)
	{
		//echo $table;
		$fields = mysql_list_fields($this->DBASE, $table); 
		$columns = mysql_num_fields($fields); 
		for ($i = 0; $i < $columns; $i++) { 
		   $arr[]= mysql_field_name($fields, $i); 
		}
		return $arr;
	}

	function selectBox($query,$display,$value,$selected)
	{
		$sql = $query;
		if($result = mysql_query($sql))
		{
			$i=0;	
			while($row = mysql_fetch_array($result ))
			{
				$sel = ($row[$value] == $selected) ? "selected='selected'" : "";
				echo "<option  value='".htmlentities($row[$value])."' $sel >".htmlentities($row[$display])."</option>";
			}
		}
	}

//ends the class over here

}

//=================================================== Class for page display =====================================
//=================================================== Class for page display =====================================
class myPager extends myclass
{
	private $pagingLink = "";
	private $query = "";
	private $ResultSet="";
	private $PerPage="";
	private $pages = "";
	function __construct($query,$ResultSet=NULL,$PerPage=NULL)
	{
		$this->query = $query;
		$this->ResultSet = $ResultSet;
		$this->PerPage = $PerPage;
	}
	function __destruct()
	{
	}

	function countRow()
	{
		$res = mysql_query($this->query);

		$count = mysql_num_rows($res);
		return $count;
	}

	function getData()
	{
			$Result_Set = $this->ResultSet;
			$Per_Page = $this->PerPage;
			$query = $this->query;
			if($Per_Page>0)
			{
				if (empty($Result_Set))
				{
					$Result_Set=0;
					$query.=" LIMIT $Result_Set, $Per_Page";
				}
				else
				{
					$Result_Set=$Result_Set;
					$query.=" LIMIT $Result_Set, $Per_Page";
				}
			}

			myclass::__construct();
			$data = myclass::select($query);
			return $data;
	}
	function showLinks($links=NULL)
	{
			$Total=$this->countRow();
			$Result_Set = $this->ResultSet;
			$Per_Page = $this->PerPage;
			$strR = "";
			if ($Total>0 && $Per_Page > 0)
			{
				if ($Result_Set<$Total && $Result_Set>0)
				{
					$Res1=$Result_Set-$Per_Page;
					$strR .="<A HREF='?Result_Set=$Res1$links' 'class='link'>[ Previous ]</A>";
				}
				$Pages=$Total / $Per_Page;
				if ($Pages>1)
				{
					for ($b=0,$c=1; $b < $Pages; $b++,$c++)
					{
						$Res1=$Per_Page * $b;
						$dash = " ";
						if($Pages == $c) { $dash = "";}
						if($Res1==$Result_Set)
						{
							$strR .="<A HREF='?Result_Set=$Res1$links' class='link' style='color:#000000;'>$c</A> $dash \n";
						}
						else
						{
							$strR .="<A HREF='?Result_Set=$Res1$links' class='link'>$c $dash </A> \n";
						}
					}
				}
				if ($Result_Set>=0 && $Result_Set<$Total)
				{
					$Res1=$Result_Set+$Per_Page;
					if ($Res1<$Total)
					{
						$strR .=" <A HREF='?Result_Set=$Res1$links' class='link'>[ Next ]</A>";
					}
				}
			}
			else
			{
				return false;
			}
			return $strR;
	}
	// single number paging
	function showLinksOne($links=NULL)
	{
			$Total=$this->countRow();
			$Result_Set = $this->ResultSet;
			$Per_Page = $this->PerPage;
			$strR = "";
			if ($Total>0 && $Per_Page > 0)
			{
					$Res1=$Result_Set-$Per_Page;
					if($Res1 <= 0) { $Res1 = 0;}
					$strR .="<div style='text-align:left; float:left; width:40px;'>
					<A HREF='?Result_Set=$Res1$links'>[Previous]</A></div>";
					$Res2=$Result_Set+$Per_Page;
					if($Res2 >= $Total) {$Res2 = $Total-1;}
					$strR .="<div style='text-align:right; float:right;width:40px;'>
					<A HREF='?Result_Set=$Res2$links'>[Next]</A></div>";
			}
			else
			{
				return false;
			}
			return $strR;
	}
	// show the Pging based on each page how many numbers can be shown i.e set $inPage=20;
	function showLinksSet($inPage,$linkp=NULL)
	{
		$Total=$this->countRow();
		$Result_Set = $this->ResultSet;
		$Per_Page = $this->PerPage;
		//$inPage = 20;
		   $Pages = $Total / $Per_Page;
		   $pVal = $Pages;
		   $bstart = 0;
		   $cstart = 1;
			if($Pages>$inPage) 
			{ 
				$Pages=$inPage;
				$currentPage = $Result_Set/$Per_Page;	  
			}
			if($currentPage>=$inPage)
			{
				$x = $currentPage-($currentPage%$inPage);
				$Pages = $x+$inPage;
				if($Pages > $pVal)
				{
					$Pages = $pVal;
				}
				$cstart = $x+1;
				$bstart = $cstart-1;
			}
			
			$backVal = $Result_Set-($inPage*$Per_Page);
			if($backVal<=0)
			{
				$backVal = 0;
			}
			$nextVal = $Result_Set+($inPage*$Per_Page);
			if($nextVal>=$Total)
			{
				$nextVal = $Pages*$Per_Page;
				($nextVal>=$Total)? $nextVal = 0 : $nextVal=$nextVal; 
			}
		if ($Total>0)
		   {
			 //$strR .="&nbsp;<A HREF='?Result_Set=$backVal$linkp' class='linkp'><b><<</b></A>&nbsp;";	   
			 if ($Result_Set<$Total && $Result_Set>0)
			{
				 $Res1=$Result_Set-$Per_Page;
				$strR .="<A HREF='?Result_Set=$Res1$linkp' class='page-no-link'>[ Previous ]</A>&nbsp;";
			}
		   if ($Pages>1)
			{
				  for ($b=$bstart,$c=$cstart; $b < $Pages; $b++,$c++)
						  {
						  $Res1=$Per_Page * $b;
									  if($Res1==$Result_Set)
									  {
										$strR .="<A HREF='?Result_Set=$Res1$linkp' class='page-no-link'><b>$c</b></A> \n";
									  }
									  else
									  {
										 $strR .="<A HREF='?Result_Set=$Res1$linkp' class='page-no-link'>$c</A> \n";
									  }
						  }
				  }
		   if ($Result_Set>=0 && $Result_Set<$Total)
				  {
					  $Res1=$Result_Set+$Per_Page;
					  if ($Res1<$Total)
					 {
						 $strR .=" <A HREF='?Result_Set=$Res1$linkp' class='page-no-link'>[ Next ]</A> &nbsp;";
					 }
				  }
				  //$strR .="<A HREF='?Result_Set=$nextVal$linkp' class='linkp'><b>>></b></A>";
		   }
		echo $strR;
	}
	// set in proxi place inc trainee display
	function showLinksSetAjax($inPage,$div,$file,$linkp=NULL,$fol=NULL)
	{
		$Total=$this->countRow();
		$Result_Set = $this->ResultSet;
		$Per_Page = $this->PerPage;
		//$inPage = 20;
		   $Pages = $Total / $Per_Page;
		   $pVal = $Pages;
		   $bstart = 0;
		   $cstart = 1;
			if($Pages>$inPage) 
			{ 
				$Pages=$inPage;
				$currentPage = $Result_Set/$Per_Page;	  
			}
			if($currentPage>=$inPage)
			{
				$x = $currentPage-($currentPage%$inPage);
				$Pages = $x+$inPage;
				if($Pages > $pVal)
				{
					$Pages = $pVal;
				}
				$cstart = $x+1;
				$bstart = $cstart-1;
			}
			
			$backVal = $Result_Set-($inPage*$Per_Page);
			if($backVal<=0)
			{
				$backVal = 0;
			}
			$nextVal = $Result_Set+($inPage*$Per_Page);
			if($nextVal>=$Total)
			{
				$nextVal = $Pages*$Per_Page;
				($nextVal>=$Total)? $nextVal = 0 : $nextVal=$nextVal; 
			}
		if ($Total>0)
		   {
			 //$strR .="&nbsp;<A HREF='?Result_Set=$backVal$linkp' class='linkp'><b><<</b></A>&nbsp;";	   
			 if ($Result_Set<$Total && $Result_Set>0)
			{
				 $Res1=$Result_Set-$Per_Page;
				$strR .="<A HREF=\"javascript:getpaging('$div','$file','?Result_Set=$Res1$linkp','$fol')\" class='linkp'>[ Previous ]</A>&nbsp;";
			}
		   if ($Pages>1)
			{
				  for ($b=$bstart,$c=$cstart; $b < $Pages; $b++,$c++)
						  {
						  $Res1=$Per_Page * $b;
									  if($Res1==$Result_Set)
									  {
										$strR .="<A HREF=\"javascript:getpaging('$div','$file','?Result_Set=$Res1$linkp','$fol')\" class='linkp' style='color:#000000;'>$c</A> \n";
									  }
									  else
									  {
										 $strR .="<A HREF=\"javascript:getpaging('$div','$file','?Result_Set=$Res1$linkp','$fol')\" class='linkp'>$c</A> \n";
									  }
						  }
				  }
		   if ($Result_Set>=0 && $Result_Set<$Total)
				  {
					  $Res1=$Result_Set+$Per_Page;
					  if ($Res1<$Total)
					 {
						 $strR .=" <A HREF=\"javascript:getpaging('$div','$file','?Result_Set=$Res1$linkp','$fol')\" class='linkp'>[ Next ]</A> &nbsp;";
					 }
				  }
				  //$strR .="<A HREF='?Result_Set=$nextVal$linkp' class='linkp'><b>>></b></A>";
		   }
		echo $strR;
	}
	
	function showLinksForm($inPage,$linkp=NULL)
	{
		$Total=$this->countRow();
		$Result_Set = $this->ResultSet;
		$Per_Page = $this->PerPage;
		//$inPage = 20;
		   $Pages = $Total / $Per_Page;
		   $pVal = $Pages;
		   $bstart = 0;
		   $cstart = 1;
			if($Pages>$inPage) 
			{ 
				$Pages=$inPage;
				$currentPage = $Result_Set/$Per_Page;	  
			}
			if($currentPage>=$inPage)
			{
				$x = $currentPage-($currentPage%$inPage);
				$Pages = $x+$inPage;
				if($Pages > $pVal)
				{
					$Pages = $pVal;
				}
				$cstart = $x+1;
				$bstart = $cstart-1;
			}
			
			$backVal = $Result_Set-($inPage*$Per_Page);
			if($backVal<=0)
			{
				$backVal = 0;
			}
			$nextVal = $Result_Set+($inPage*$Per_Page);
			if($nextVal>=$Total)
			{
				$nextVal = $Pages*$Per_Page;
				($nextVal>=$Total)? $nextVal = 0 : $nextVal=$nextVal; 
			}
		if ($Total>0)
		   {
			 //$strR .="&nbsp;<A HREF='?Result_Set=$backVal$linkp' class='linkp'><b><<</b></A>&nbsp;";	   
			 if ($Result_Set<$Total && $Result_Set>0)
			{
				 $Res1=$Result_Set-$Per_Page;
				$strR .="<A HREF=\"javascript:pagingSubmit('$Res1');\" class='linkp'>[ Previous ]</A>&nbsp;";
			}
		   if ($Pages>1)
			{
				  for ($b=$bstart,$c=$cstart; $b < $Pages; $b++,$c++)
						  {
						  $Res1=$Per_Page * $b;
									  if($Res1==$Result_Set)
									  {
										$strR .="<A HREF=\"javascript:pagingSubmit('$Res1');\" class='linkp' style='color:#000000;'>$c</A> \n";
									  }
									  else
									  {
										 $strR .="<A HREF=\"javascript:pagingSubmit('$Res1');\" class='linkp'>$c</A> \n";
									  }
						  }
				  }
		   if ($Result_Set>=0 && $Result_Set<$Total)
				  {
					  $Res1=$Result_Set+$Per_Page;
					  if ($Res1<$Total)
					 {
						 $strR .=" <A HREF=\"javascript:pagingSubmit('$Res1');\" class='linkp'>[ Next ]</A> &nbsp;";
					 }
				  }
				  //$strR .="<A HREF='?Result_Set=$nextVal$linkp' class='linkp'><b>>></b></A>";
		   }
		echo $strR;
	}
	
}


/* 
add, update from for same fields as table field name.

$saveForm = new saveForm(table,$_POST);
$saveForm->addData();
$saveForm->updateData(in where db field,in frm filed);

*/

// == Save form============================================
class saveForm extends myclass
{
	private $table = "";
	private $post = array();
	
	function __construct($table,$post)
	{
		$this->table = $table;
		$this->post = $post;
	}
	function __destruct()
	{
		$this->table = "";
		$this->post = "";
	}
	function addData()
	{
		$tablefields = $this->getfields($this->table);
		$fields = implode(',',$tablefields);
		$value = "";
		$postVal = $this->post;
		$lastVal = count($tablefields)-1;
		$val = $tablefields;
		for($i=0; $i<count($tablefields); $i++)
		{
			if(isset($postVal[$val[$i]]))
			{
				$value.= "'".$postVal[$val[$i]]."'";
			}
			elseif(!isset($postVal[$val[$i]]))
			{
				$value.= "''";
			}
			if($i < $lastVal)
			{
				$value.= ",";
			} 
		}
		
		$query = "insert into " . $this->table . "($fields) values($value)";
		myclass::__construct();
		if(myclass::sql_query($query))
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
	

	function updateData($setField,$setVal) // with field
	{
		$tablefields = $this->getfields($this->table);
		$fields = implode(',',$tablefields);
		$value = "";
		$postVal = $this->post;
		$lastVal = count($tablefields)-1;
		$val = $tablefields;
		for($i=1; $i<count($tablefields); $i++)
		{
			if(isset($postVal[$val[$i]]))
			{
				$value.= ($value!="") ? "," : "";
				$value.= $val[$i]."='".$postVal[$val[$i]]."'";
			}
		}
		
		$query = "update " . $this->table . " set $value ,$setField=$setField where $setField='".$postVal[$setVal]."'";
		myclass::__construct();
		if(myclass::sql_query($query))
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}

	function updateDataMultiple($setField,$setVal) // with value 
	{
		$tablefields = $this->getfields($this->table);
		$fields = implode(',',$tablefields);
		$value = "";
		$postVal = $this->post;
		$lastVal = count($tablefields)-1;
		$val = $tablefields;
		for($i=1; $i<count($tablefields); $i++)
		{
			if(isset($postVal[$val[$i]]))
			{
				$value.= ($value!="") ? "," : "";
				$value.= $val[$i]."='".$postVal[$val[$i]]."'";
			}
		}
		$where = "";
		for($i=0; $i<count($setField); $i++)
		{
			$where .= " and ".$setField[$i] . "='".$setVal[$i]."'";
		}
		$query = "update " . $this->table . " set $value where 1=1 $where";
		myclass::__construct();
		if(myclass::sql_query($query))
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
}
//=============================================
?>