View Full Version : مجموعه ی کلاس ها
vahidqara
پنج شنبه 26 تیر 1393, 16:38 عصر
با سلام ودرود خدمت تمامی عزیزان برنامه نویس بخصوص PHP و مشتقاتش ..
4 کلاس در یک namespace نوشتم گفتم شما عزیزان هم استفاده و البته توسعه و رفع ایراد کنید .. البته انشاالله کاملتر میشه..
کلاس ارتباط با بانک
کلاس Session برای ورود و خروج کاربران
کلاس User
کلاس Upolad
:لبخندساده:
vahidqara
شنبه 28 تیر 1393, 00:05 صبح
کلاس DataBase
class MysqlDataBase {
private $name_host='localhost';
private $user = 'root';
private $password = '';
private $database_name = 'testphp';
protected $connection_string ='';
public function __construct(){
$this->connection();
}
public function connection(){
$this->connection_string = mysqli_connect($this->name_host,$this->user,$this->password);
if(mysqli_errno($this->connection_string)){
die('Con Not Connect To DataBase' .mysqli_error($this->connection_string));
}else{
$SelectDb = mysqli_select_db($this->connection_string,$this->database_name);
if(!$SelectDb){
die('DataBase Selection Faild :' .mysqli_error($this->connection_string));
}
}
}
public function close_connection(){
if(isset($this->connection_string)){
mysqli_close($this->connection_string);
unset($this->connection_string);
}
}
public function query($sql){
$result = mysqli_query($this->connection_string,$sql);
if(!$result){
die('DataBase Query Faild :'.mysqli_error($this->connection_string));
}
return $result;
}
public function fetch_array($result){
return mysqli_fetch_assoc($result);
}
public function fetch_rows($result){
return mysqli_num_rows($result);
}
public function escape($string){
$string = trim($string);
$string = strip_tags($string);
$string = htmlspecialchars($string);
$string = mysql_real_escape_string($string);
return $string;
}
}
vahidqara
شنبه 28 تیر 1393, 00:06 صبح
کلاس Session
class Session{
private $logged_in=false;
public $user_id;
public $username;
public $message;
public function __construct(){
session_start();
$this->check_message();
$this->check_login();
}
public function message($msg=""){
if(!empty($msg)){
$_SESSION['message']=$msg;
}else{
return $this->message;
}
}
private function check_message(){
if(isset($_SESSION['message'])){
$this->message = $_SESSION['message'];
unset($_SESSION['message']);
}else{
$this->message ="";
}
}
public function is_logged_in(){
return $this->logged_in;
}
public function login($user){
if($user){
$this->user_id = $user['id'];
$this->username = $user['username'];
$_SESSION['user_id'] = $this->user_id;
$_SESSION['username']=$this->username;
$this->logged_in = true;
}
}
public function logout(){
unset($_SESSION['user_id']);
unset($_SESSION['username']);
unset($this->username);
unset($this->user_id);
$this->logged_in=false;
}
public function check_login(){
if(isset($_SESSION['user_id'])){
$this->user_id = $_SESSION['user_id'];
$this->logged_in=true;
}else{
unset($this->user_id);
$this->logged_in=false;
}
}
}
vahidqara
شنبه 28 تیر 1393, 00:07 صبح
کلاس User
class User extends MysqlDataBase{
private static $table_name="user";
public $username;
public $password;
public $name;
public static function find_all(){
global $connect;
$result = $connect->query("select * from ".self::$table_name);
return $result;
}
public static function find_by_id($id=0){
global $connect;
$id = intval($id);
$sql = "select * from ".self::$table_name." where id={$id}";
$result = $connect->query($sql);
$found = $connect->fetch_array($result);
return $found;
}
public static function login_user($username,$password){
global $connect;
$username = $connect->escape($username);
$password = $connect->escape(sha1(md5($password)));
$sql = "select * from ".self::$table_name." where username ='$username' and password ='$password'";
$result = $connect->query($sql);
return $result;
}
public function create_user($username,$password,$name){
$username = $this->escape($username);
$password = sha1(md5($this->escape($password)));
$name = $this->escape($name);
$result = $this->query("INSERT INTO ".self::$table_name." (username,password,name) value('$username','$password','$name')");
return $result;
}
public function update_user($id=0){
$username = $this->escape($this->username);
$password = $this->escape($this->password);
$name = $this->escape($this->name);
$id = intval($id);
$sql ="UPDATE ".self::$table_name." SET username='$username',password='$password',name='$n ame' where id='$id'";
$result = $this->query($sql);
return $result;
}
public function delete_user($id=0){
$id = intval($id);
$sql ="DELETE FROM ".self::$table_name." where id='$id'";
$result = $this->query($sql);
return $result;
}
}
vahidqara
شنبه 28 تیر 1393, 00:07 صبح
کلاس Upload
class Image extends MysqlDataBase{
private static $table_name="image";
private $id;
private $filename;
private $type;
private $size;
public $caption;
public $max_size =1048576;
private $tem_path;
private $upload_dir='images';
public $errors=array();
protected $upload_error = array(
UPLOAD_ERR_OK =>'No Errors',
UPLOAD_ERR_INI_SIZE =>'Larger than upload_max_filesize',
UPLOAD_ERR_FORM_SIZE =>'Larger than form MAX_FILE_SIZE',
UPLOAD_ERR_PARTIAL =>'Partial upload',
UPLOAD_ERR_NO_FILE =>'No file',
UPLOAD_ERR_NO_TMP_DIR =>'No Temporary directory',
UPLOAD_ERR_CANT_WRITE =>'Can not write to disk',
UPLOAD_ERR_EXTENSION =>'File upload stopped by extension'
);
public function attach_file($file){
if(!$file || empty($file) || !is_array($file)){
$this->errors[] = 'No file was uploads';
return false;
}elseif($file['error']!= 0){
$this->errors[] = $this->upload_error[$file['error']];
return false;
}else{
$this->filename = $this->escape($file['name']);
$this->tem_path = $this->escape($file['tmp_name']);
$this->size = $this->escape($file['size']);
$this->type = $this->escape($file['type']);
//$this->caption = $file['caption'];
return true;
}
}
public static function find_all_image(){
global $connect;
$result = $connect->query("select * from ".self::$table_name);
return $result;
}
public static function find_by_image_id($id=0){
global $connect;
$id = intval($id);
$result = $connect->query("select * from Image where id='$id'");
$found = $connect->fetch_array($result);
return $found;
}
public function Delete_image($id=0){
$id = intval($id);
$sql ="delete from ".self::$table_name." where id='$id'";
$result = $this->query($sql);
return $result;
}
public function size($size=0){
$this->size = intval($size);
if($this->size < 1024){
return "{$this->size} bytes";
}elseif($this->size < 1048576){
$size_kb = round($this->size/1024);
return "{$size_kb} KB";
}else{
$size_mb = round($this->size/1048576,1);
return "{$size_mb} MB";
}
}
public function image_dir(){
return $this->upload_dir.'/';
}
public function create_image(){
$filename = $this->filename;
$type = $this->type;
$size = $this->size;
$caption = $this->caption;
$type_validate = array('image/gif','image/png','image/jpg','image/jpeg');
if(!empty($this->errors)){
return false;
}
if(strlen($this->caption > 255)){
$this->errors[] ="The Caption The Only be 255 character long";
return false;
}
if(!in_array($this->type,$type_validate)){
$this->errors[]="type file not support..!";
return false;
}
if(empty($this->filename)|| empty($this->tem_path)){
$this->errors[] = 'The file Location not availabel';
return false;
}
$target_path =$this->upload_dir .'/'.$this->filename;
if(file_exists($target_path)){
$this->errors[]="The file {$this->filename} already exists";
return false;
}
if(move_uploaded_file($this->tem_path,$target_path)){
$sql = "INSERT INTO ".self::$table_name."(filename,type,size,caption) value('$filename','$type','$size','$caption')";
$result = $this->query($sql);
return $result;
unset($this->tem_path);
}else{
$this->errors[] = 'The file upload failed';
return false;
}
}
}
MRmoon
شنبه 28 تیر 1393, 04:09 صبح
درود
در کلاس دیتابیس چگونه مقادیر رو تنظیم کنیم:متفکر:
محمد.
vahidqara
شنبه 28 تیر 1393, 11:15 صبح
درود
در کلاس دیتابیس چگونه مقادیر رو تنظیم کنیم:متفکر:
محمد.
سلام و درود ... چون کلاس ها خصوصی برای خودم نوشتم مقادیرو دستی ست کردم ... ولی شما میتونید مقادیر لازم رو Public کنی و از بیرون کلاس ست کنید :لبخندساده:
Mohammadsgh
شنبه 28 تیر 1393, 12:19 عصر
کلاس session:اینم کلاس خیلی خوبیه که آقای جنتی تو پکیج mvc آموزش دادن:لبخندساده:
class Session {
public static function init() {
@session_start();
}
public static function set($key,$value) {
$_SESSION[$key]=$value;
}
public static function get($key) {
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
}else {
return FALSE;
}
}
public static function destory() {
}
}
vahidqara
شنبه 28 تیر 1393, 13:42 عصر
کلاس session:اینم کلاس خیلی خوبیه که آقای جنتی تو پکیج mvc آموزش دادن:لبخندساده:
class Session {
public static function init() {
@session_start();
}
public static function set($key,$value) {
$_SESSION[$key]=$value;
}
public static function get($key) {
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
}else {
return FALSE;
}
}
public static function destory() {
}
}
بله کلاس خوبی هستش .. مچکرم :لبخندساده:
[younes]
شنبه 28 تیر 1393, 14:01 عصر
دوست عزیز این چهار کلاس واقعا برای من کاربردی بود و از دوباره کاری من جلوگیری کرد متشکرم .
vahidqara
شنبه 28 تیر 1393, 17:52 عصر
;2064590']دوست عزیز این چهار کلاس واقعا برای من کاربردی بود و از دوباره کاری من جلوگیری کرد متشکرم .
درود .. خواهش میکنم خوشحالم که کاربردی بود براتون :لبخندساده:
vahidqara
شنبه 28 تیر 1393, 23:52 عصر
کلاس comment
دوستان حتما کم و کاستی وجود دارد دیگه ببخشید اگه چیزی کمه .. و اگه مشکلی داشته باشند کدها ممنون میشم بگید که رفع بشند :لبخندساده:
class Comment extends MysqlDataBase{
private static $table_name="comments";
public $id;
public $image_id;
public $body;
public $created;
public $author;
public static function find_comment($photo_id=0){
global $connect;
$photo_id = intval($photo_id);
$sql = "select * from ".self::$table_name." where image_id={$photo_id} order by created asc";
$result = $connect->query($sql);
return $result;
}
public function create_comment($image_id,$body,$author){
$image_id = intval($image_id);
$body = $this->escape($body);
$created = strftime("%Y-%m-%d %H:%M:%S",time());
$author = $this->escape($author);
$sql = "INSERT INTO ".self::$table_name."(image_id,created,author,body) value('$image_id','$created','$author','$body')";
$result = $this->query($sql);
return $result;
}
public static function find_all(){
global $connect;
$result = $connect->query("select * from ".self::$table_name);
return $result;
}
public static function find_by_id($id=0){
global $connect;
$id = intval($id);
$sql = "select * from ".self::$table_name." where id={$id}";
$result = $connect->query($sql);
$found = $connect->fetch_array($result);
return $found;
}
public function delete_comment($id=0){
$id = intval($id);
$sql ="DELETE FROM ".self::$table_name." where id='$id'";
$result = $this->query($sql);
return $result;
}
}
vahidqara
پنج شنبه 02 مرداد 1393, 17:45 عصر
کلاس ساده با PDO..
class db{
public $name_host ='localhost';
public $username = 'root';
public $password ='';
public $db_name ='testphp';
protected $connection_string='';
public function __construct(){
$this->connection();
}
public function connection(){
try{
$this->connection_string = new PDO("mysql:host=$this->name_host;dbname=$this->db_name;",$this->username,$this->password);
return $this->connection_string;
}
catch(PDOException $ex){
print "Can not Connect To DataBase..!".$ex->getMessage();
exit();
}
}
public function close_connection(){
$this->connection_string = null;
}
public function escape($string){
$string = trim($string);
$string = strip_tags($string);
$string = htmlspecialchars($string);
return $string;
}
}
Mohammadsgh
پنج شنبه 02 مرداد 1393, 20:07 عصر
یه کلاس برا curl بزار:لبخند:(شوخی)
vahidqara
پنج شنبه 02 مرداد 1393, 20:21 عصر
یه کلاس برا curl بزار:لبخند:(شوخی)
:لبخند::لبخند:
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.