Insert,Update and Delete function

<?php

class Booking

{

var $imgpath;

function __construct()

{

global $wpdb;
$this->imgpath= ‘../wp-content/plugins/booking/images/’;

}

public function insert($data)

{

$query=”insert into “.$data[‘table’][‘name’].” set “;

$querystring=”;

foreach($data[‘data’] as $tkey=>$tval)

{

$querystring.=””.$tkey.”='”.$tval.”‘,”;

}
$totalimg= count($data[‘image’]);
$imgstring=”;
foreach($data[‘image’] as $imgkey=>$imgval)
{
$imgstring.=””.$imgkey.”='”.$imgval.”‘,”;

$dest=$this->imgpath.$_FILES[$imgkey][‘name’];
move_uploaded_file($_FILES[$imgkey][‘tmp_name’],$dest);
}

$insquery= $query.$querystring.$imgstring.’datetime=now()’;

mysql_query($insquery) or die(mysql_error());

return mysql_insert_id();

}

public function upd($data)

{

$query=”update “.$data[‘table’][‘name’].” set “;

$querystring=”;

foreach($data[‘data’] as $key=>$val)

{

$querystring.=””.$key.”='”.$val.”‘,”;

}

$wherestring=”;

$totalelement= count($data[‘where’]);

$i=1;

foreach($data[‘where’] as $tkey=>$tval)

{

$wherestring.=””.$tkey.”='”.$tval.”‘”;

if($i<$totalelement)

{

$wherestring.=” and “;

$i++;

}

}

$totalimg= count($data[‘image’]);
$imgstring=”;
foreach($data[‘image’] as $imgkey=>$imgval)
{
if($imgval!=”)
{
$imgstring.=””.$imgkey.”='”.$imgval.”‘,”;

$dest=$this->imgpath.$_FILES[$imgkey][‘name’];
move_uploaded_file($_FILES[$imgkey][‘tmp_name’],$dest);
}
}

$updquery= $query.$querystring.$imgstring.’datetime=now() where ‘.$wherestring;

mysql_query($updquery) or die(mysql_error());

}

public function Query($selectquery)

{

$this->query= $selectquery;

}

public function fetchobject()

{

$fetchobject= Array();

$result=mysql_query($this->query) or die(mysql_error());

while($row=mysql_fetch_object($result))

{

$fetchobject[]=$row;

}

return $fetchobject;

}

public function deletes($data)

{

$query=”delete from “.$data[‘table’][‘name’].” where “;

$wherestring=”;

$totalelement= count($data[‘where’]);

$i=1;

foreach($data[‘where’] as $tkey=>$tval)

{

$wherestring.=””.$tkey.”='”.$tval.”‘”;

if($i<$totalelement)

{

$wherestring.=’ and ‘;

$i++;

}

}

$delqry= $query.$wherestring;

mysql_query($delqry) or die(mysql_error());

}

public function mysqlcount()

{

$result=mysql_query($this->query) or die(mysql_error());

$count= mysql_num_rows($result);

return $count;

}

}

?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s