මේ තියෙන්නෙ මම ලියපු class එකක්
මේ ෆෝම් එක
මෙතනදි තමයි class එක කෝල් වෙන්නෙ
මට තියෙන ප්රශ්නෙ මේ ලූප් එක හරියට දුවනව ඒ වුනාට ඩේටාබේස් එකට එක රෝ එකයි වැටෙන්නෙ
උදවු කරන අයට 5 ගානෙ සලකනු ලැබේ
PHP:
<?php
include_once get_include_path().'cls/database/cls_dbconnection.php';
include_once get_include_path().'cls/misc/cls_images.php';
include_once get_include_path().'cls/misc/cls_session.php';
include_once get_include_path().'cls/business/cls_users.php';
class _ads
{
public $_adid = "";
public $_userid = "";
public $_subcatid = "";
public $_typeid = "";
public $_title = "";
public $_description = "";
public $_price = "";
public $_city = "";
public $_pictures = null;
public $_message = "";
public function _open()
{
try
{
$classDB = new database();
$dbcon = $classDB->_getDBconnection();
$resultset = null;
if($dbcon)
{
$resultset = $dbcon->prepare("SELECT * FROM tbl_ads WHERE _adid=:_adid AND _status=1;");
$resultset->execute(array(':_adid'=>$this->_adid));
$rows = $resultset->fetch(PDO::FETCH_ASSOC);
$this->_adid = $rows['_adid'];
$this->_userid = $rows['_userid'];
$this->_subcatid = $rows['_subcatid'];
$this->_typeid = $rows['_typeid'];
$this->_title = $rows['_title'];
$this->_description = $rows['_description'];
$this->_price = $rows['_price'];
$this->_city = $rows['_city'];
$resultset = null;
$resultset = $dbcon->prepare("SELECT * FROM tbl_ads_images WHERE _adid=:_adid;");
$resultset->execute(array(':_adid'=>$this->_adid));
if(!$resultset->rowCount()==0)
{
$this->_pictures = $resultset;
}
$dbcon = null;
$classDB->close_db();
return(true);
}
}
catch(exception $e)
{
$this->_message="Unexpected error:_open";
return (false);
}
}
public function _save($userinfo="")
{
try
{
$classDB = new database();
$classImage = new _images();
$dbcon = $classDB->_getDBconnection();
$resultset = null;
if($dbcon)
{
$dbcon->beginTransaction();
//Check user
$resultset = $dbcon->prepare("SELECT * FROM tbl_users WHERE _userid=:_userid AND _status=1;");
$resultset->execute(array(':_userid'=>$this->_userid));
if($resultset->rowCount()!==1)
{
$this->_message="Invalid user!!";
$dbcon->rollBack();
return (false);
}
//Generate new id
$resultset = $dbcon->prepare("SELECT _adid FROM tbl_parameters;");
$resultset->execute();
$rows = $resultset->fetch(PDO::FETCH_ASSOC);
$this->_adid = $rows['_adid']+1;
$resultset=null;
//Update new id
$resultset = $dbcon->prepare("UPDATE tbl_parameters SET _adid=:_adid;");
$resultset->execute(array(':_adid'=>$this->_adid));
if($resultset->rowCount()!==1)
{
$this->_message="System error: Parameter cannot be updated.";
$dbcon->rollBack();
return (false);
exit;
}
$resultset=null;
$resultset = $dbcon->prepare("INSERT INTO tbl_ads (_adid, _userid, _subcatid, _typeid, _title, _description, _price, _city, _status) VALUES (?,?,?,?,?,?,?,?,1);");
$resultset->bindParam(1, $this->_adid);
$resultset->bindParam(2, $this->_userid);
$resultset->bindParam(3, $this->_subcatid);
$resultset->bindParam(4, $this->_typeid);
$resultset->bindParam(5, $this->_title);
$resultset->bindParam(6, $this->_description);
$resultset->bindParam(7, $this->_price);
$resultset->bindParam(8, $this->_city);
$resultset->execute();
if($resultset->rowCount()==1)
{
$resultset = null;
$uploadingpath = get_include_path().'images';
$allowedFileTypes = array("image/jpg","image/jpeg","image/bmp","image/gif","image/png");
for($i=0; $i<count($this->_pictures['name']); $i++)
{
echo($i);
$tmpFilePath = $this->_pictures['tmp_name'][$i];
if($tmpFilePath != "")
{
$fileType = $this->_pictures['type'][$i];
$ext = end((explode(".", $this->_pictures['name'][$i])));
$filename = $_SESSION['_user']['id'].'-'.time().'-'.$i.'.'.$ext;
$shortname = $this->_pictures['name'][$i];
$filePath = "$uploadingpath/uploaded/$filename";
if(!in_array($fileType,$allowedFileTypes))
{
$this->_message = 'Image type not allowed! Please upload JPG, JPEG, BMP, GIF or PNG images';
$dbcon->rollBack();
return(false);
exit;
}
if ($this->_pictures['error'][$i] > 0)
{
$this->_message = 'Invalid Image! Unreadable or Damaged picture';
$dbcon->rollBack();
return(false);
exit;
}
if(!move_uploaded_file($tmpFilePath, $filePath))
{
$this->_message = 'Internal Error! Please contact webmaster';
$dbcon->rollBack();
return(false);
}
//100px resized images
$classImage->_load("$uploadingpath/uploaded/$filename");
$classImage->_resizeToWidth(100);
$classImage->_save("$uploadingpath/processed/100/$filename");
//400px resized images
$classImage->_load("$uploadingpath/uploaded/$filename");
$classImage->_resizeToWidth(400);
$classImage->_save("$uploadingpath/processed/400/$filename");
//800px resized images
$classImage->_load("$uploadingpath/uploaded/$filename");
$classImage->_resizeToWidth(800);
$classImage->_save("$uploadingpath/processed/800/$filename");
$resultset = $dbcon->prepare("INSERT INTO tbl_ads_images (_adid, _imagepath, _order) VALUES (_adid=:_adid,_imagepath=:_imagepath,_order=:_order);");
$resultset->execute(array(
':_adid' => $this->_adid,
':_imagepath' => $filename,
':_order' => $i
));
}
}
}
$dbcon->commit();
Return (True);
exit;
}
else
{
$this->_message="No active database connection ";
return (false);
exit;
}
}
catch(exception $e)
{
$this->_message="Unexpected error:_open ".$e;
//$dbcon->rollBack();
return (false);
exit;
}
}
public function _update()
{
}
public function _delete()
{
}
}
?>
මේ ෆෝම් එක
PHP:
<form class="form-horizontal" enctype="multipart/form-data" method="post">
<fieldset>
<div class="form-group">
<label class="col-md-3 control-label">Category <sup>*</sup></label>
<div class="col-md-8">
<select title="Select Category" name="_subcatid" id="category-group" class="selectpicker" required=""><!--#category-optionlist#--></select>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Add Type</label>
<div class="col-md-8">
<label class="radio-inline" for="radios-0"><input name="_typeid" id="radios-0" value="0" checked="checked" type="radio">Private </label>
<label class="radio-inline" for="radios-1"><input name="_typeid" id="radios-1" value="1" type="radio">Business </label>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="Adtitle">Ad title <sup>*</sup></label>
<div class="col-md-8">
<input id="Adtitle" name="_title" placeholder="Ad title" class="form-control input-md" required="" type="text">
<span class="help-block">A great title needs at least 60 characters. </span>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="textarea">Describe ad <sup>*</sup></label>
<div class="col-md-8">
<textarea class="form-control" id="textarea" name="_description" placeholder="Describe what makes your ad unique" required=""></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="Price">Price <sup>*</sup></label>
<div class="col-md-4">
<div class="input-group"><span class="input-group-addon">Rs.</span>
<input id="Price" name="_price" class="form-control" placeholder="Price" required="" type="text">
</div>
</div>
<div class="col-md-4">
<div class="checkbox">
<label><input type="checkbox">Negotiable </label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="seller-Location">Location <sup>*</sup></label>
<div class="col-md-8">
<input id="autocomplete-ajax" name="_city" class="form-control" required="">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="textarea"> Pictures</label>
<div class="col-md-8">
<div class="mb10">
<input id="_pictures" name="_pictures[]" type="file" class="file" accept="image/*" multiple="">
</div>
<p class="help-block">Add up to 5 photos. Maximum 2Mb</p>
</div>
</div>
<!--#sellerinformations#-->
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<div class="termbox mb10">
<label class="checkbox-inline" for="checkboxes-1"><input name="checkboxes" id="checkboxes-1" value="1" type="checkbox" required="">I have read and agree to the <a href="terms-conditions.html">Terms & Conditions</a></label>
</div>
<p class="help-block" id="errorcode"><!--#output_message#--></p>
<div style="clear:both"></div>
<input type="submit" class="btn btn-success btn-lg" title="Register" value="Post Ad"/>
</div>
</div>
</fieldset>
</form>
මෙතනදි තමයි class එක කෝල් වෙන්නෙ
PHP:
if(isset($_SESSION['_user']))
{
$classads->_adid = 0;
$classads->_userid = $_SESSION['_user']['id'];
$classads->_subcatid = $_POST['_subcatid'];
$classads->_typeid = $_POST['_typeid'];
$classads->_title = $_POST['_title'];
$classads->_description = $_POST['_description'];
$classads->_price = $_POST['_price'];
$classads->_city = $_POST['_city'];
$classads->_pictures = &$_FILES['_pictures'];
if($classads->_save())
{
//header('Location: /user/manage-ads.html');
}
else
{
display_message($classads->_message);
}
}
මට තියෙන ප්රශ්නෙ මේ ලූප් එක හරියට දුවනව ඒ වුනාට ඩේටාබේස් එකට එක රෝ එකයි වැටෙන්නෙ
PHP:
for($i=0; $i<count($this->_pictures['name']); $i++)
{
echo($i);
$tmpFilePath = $this->_pictures['tmp_name'][$i];
if($tmpFilePath != "")
{
$fileType = $this->_pictures['type'][$i];
$ext = end((explode(".", $this->_pictures['name'][$i])));
$filename = $_SESSION['_user']['id'].'-'.time().'-'.$i.'.'.$ext;
$shortname = $this->_pictures['name'][$i];
$filePath = "$uploadingpath/uploaded/$filename";
if(!in_array($fileType,$allowedFileTypes))
{
$this->_message = 'Image type not allowed! Please upload JPG, JPEG, BMP, GIF or PNG images';
$dbcon->rollBack();
return(false);
exit;
}
if ($this->_pictures['error'][$i] > 0)
{
$this->_message = 'Invalid Image! Unreadable or Damaged picture';
$dbcon->rollBack();
return(false);
exit;
}
if(!move_uploaded_file($tmpFilePath, $filePath))
{
$this->_message = 'Internal Error! Please contact webmaster';
$dbcon->rollBack();
return(false);
}
//100px resized images
$classImage->_load("$uploadingpath/uploaded/$filename");
$classImage->_resizeToWidth(100);
$classImage->_save("$uploadingpath/processed/100/$filename");
//400px resized images
$classImage->_load("$uploadingpath/uploaded/$filename");
$classImage->_resizeToWidth(400);
$classImage->_save("$uploadingpath/processed/400/$filename");
//800px resized images
$classImage->_load("$uploadingpath/uploaded/$filename");
$classImage->_resizeToWidth(800);
$classImage->_save("$uploadingpath/processed/800/$filename");
$resultset = $dbcon->prepare("INSERT INTO tbl_ads_images (_adid, _imagepath, _order) VALUES (_adid=:_adid,_imagepath=:_imagepath,_order=:_order);");
$resultset->execute(array(
':_adid' => $this->_adid,
':_imagepath' => $filename,
':_order' => $i
));
}
}
උදවු කරන අයට 5 ගානෙ සලකනු ලැබේ

Last edited:




