Wednesday, September 19, 2007

Get Current Week Using PHP

Only copy an dpaste it you get current week

$currentweek= (int)((date("d") + date('w',mktime(0,0,0,date("m"),1,date("Y"))) - 1 )/7)+1 ;

Tuesday, September 18, 2007

php db class


/**********************************************************************************************/

//////////////THIS DB CLASS CREATED BY ZIYA///////////

/*
1) Functions Description :

(a) setup_conn() [This function used to establish a connection to database]
(b) db_connect() [This function is also used to establish a connection to database]
(c) select_db() [This function is used to select database]
(d) query() [This function is used to execute a query and get result from it]
(e) fetch_array() [This function is used to fetch an array of query result]
(f) num_rows() [This function is responsible for give num of rows]
(g) last_insertid() [This function is give last inserted id]
(h) insert_record() [This function is used to insert a record]
(i) update_record() [This function is used to select record]
(j) delete_record() [This function is used to delete records according to condition]
(k) select_record() [This function is used to select records according to OR condition]
(l) delete_all_record() [This function is used to delete all records from a table]
(m) select_all_record() [This function is used to select all records from a table]
(n) fetchSingleRow($result) [For fetch single record]
(o) executeQuery($query)
(p) select_records() [This function is used to select records according to AND condition]
*/
/***********************************************************************************************/

class db
{

var $hostname;// Hostname that the object should connect to

var $username;// Username that the object should use

var $password;// Password that the object should use

var $database;// Database that the object should use

var $query_num;// counts the total queries that the object has done.

var $con;//contains connection object.

function setup_conn($hostname,$username,$password)
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
}

function db_connect()
{
$con = mysql_connect($this->hostname,$this->username,$this->password)or die(mysql_error());/*Open a non persistant connection to the server.*/
if (!$con)
{
echo 'Connection to database server at: '.$this->hostname.' failed.';
return false;
}
else
{
return $con;
}
}

function db_pconnect()
{
$result = mysql_pconnect($this->hostname,$this->username,$this->password);/*Open a persistant connection to the server. */

if (!$result)
{
echo 'Connection to database server at: '.$this->hostname.' failed.';
return false;
}
return $result;
}

function select_db($database,$con)
{
$this->database = $database;
$this->con=$con;
if (!mysql_select_db($this->database,$this->con))
{
'Selection of database: '.$this->database.' failed.';
return false;
}
}

function query($query)
{
$result = mysql_query($query) or die("Query failed:$query

" . mysql_error());
return $result;
}



function return_query_num()
{
return $this->query_num;
}

function num_rows($result)
{
return mysql_num_rows($result);
}

function last_insertid()
{
return mysql_insert_id();
}

function fetchSingleRow($result)
{
return mysql_fetch_row($result);
}

function delete_all_record($table)
{
$delete_query = "DELETE FROM $table";
$delete_result = mysql_query($delete_query)or mysql_error();

$flag = mysql_affected_rows();
if($flag != 0)
return true;
else
return false;
}

function fetch_array($result)
{
return mysql_fetch_array($result);
}

function select_all_record($table)
{
$select_query = "SELECT * FROM ".$table;
$select_result = mysql_query($select_query)or mysql_error();
return $select_result;
}

function select_record($conditon,$table)
{
$len = count($conditon);
$i = 0;
$cond = "";
$sql = "select * from ".$table." where ";

foreach ($conditon as $key => $value) {
if($len - 1 == $i)
$cond.= $key."='".$value."'";
else
$cond.= $key."='".$value."' and ";
$i++;
}

$sql = $sql.$cond;
//die($sql);
return mysql_query($sql);
}

function get_row($rs){

$this->row = mysql_fetch_object($rs);
return $this->row;
}

function get_first_id($tbl,$id){

$rs = $this->query("SELECT ".$id." FROM ".$tbl." WHERE ".$id." > 0 ORDER BY ".$id);
$this->row = mysql_fetch_object($rs);
return $this->row->$id;
}

function get_child_id($tbl,$ParentId,$Cid=""){
$this->res = $this->query("SELECT * from $tbl WHERE $ParentId = '$Cid'");
$this->row = $this->fetch_array($this->res);
return $this->row[Cid];
}

function get_parent_id($tbl,$Cid_Field,$Cid=""){
$this->res = $this->query("SELECT * from $tbl WHERE $Cid_Field = '$Cid'");
$this->row = $this->fetch_array($this->res);
return $this->row[ParentId];
}

function select_record_with_limit($conditon,$table,$limit)
{
$len = count($conditon);
$i = 0;
$cond = "";
$sql = "select * from ".$table." where ";

foreach ($conditon as $key => $value) {
if($len - 1 == $i)
$cond.= $key."='".$value."'";
else
$cond.= $key."='".$value."' and ";
$i++;
}

echo $sql = $sql.$cond." ".$limit;
return mysql_query($sql);
}

function insert_record($qry,$table)
{
$len = count($qry);
$i = 0;
$field = "";
$data = "";

foreach ($qry as $key => $value) {
if($len - 1 == $i)
{
$field.= $key;
$data.= "'".$value."'";
}
else
{
$field.= $key.",";
$data.= "'".$value."',";
}
$i++;
}

$sql = "insert into ".$table."(".$field.")values(".$data.")";
//die($sql);
mysql_query($sql)or die(mysql_error());
//return $this -> last_insertid();
}


function update_record($sql,$conditon,$table)
{
$len = count($sql);
$i = 0;
$data = "";

foreach ($sql as $key => $value) {
if($len - 1 == $i)
$data.= $key."='".$value."'";
else
$data.= $key."='".$value."',";
$i++;
}

$lim = count($conditon);
$j = 0;
$cond = "";

foreach ($conditon as $key => $value) {
if($lim - 1 == $j)
$cond.= $key."='".$value."'";
else
$cond.= $key."='".$value."' and ";
$j++;
}

$sql = "update ".$table." set ".$data." where ".$cond;
//echo "Test".$sql;die();
if(mysql_query($sql))return 1;
//return mysql_affected_rows();
}

function delete_record($conditon,$table)
{
$len = count($conditon);
$i = 0;
$cond = "";
$sql = "delete from ".$table." where ";

foreach ($conditon as $key => $value) {
if($len - 1 == $i)
$cond.= $key."='".$value."'";
else
$cond.= $key."='".$value."' and ";
$i++;
}

$sql = $sql.$cond;//die($sql);
mysql_query($sql);
return mysql_affected_rows();
}
}
$dbobj = new db();
?>