PHP function Encapsulation

Source: Internet
Author: User
Tags ord rtrim

This is a number of custom encapsulated function classes, it is convenient to call, there will be more packaging function updates!

Bullet Box Jump
function alert ($msg, $url = ") {
echo "<script>";
echo "alert (' $msg ');";
if ($url) {
echo "window.location.href= ' $url ';";
}else{
echo "Window.history.go (-1);";
}
echo "</script>";
}


Get a single piece of data
function GetOne ($sql) {
$sql = mysql_query ($sql);
$list = Array ();
if ($sql && mysql_affected_rows () >0) {
$list = Mysql_fetch_assoc ($sql);
}return $list;
}

Get more than one piece of data
function GetALL ($sql) {
$sql = mysql_query ($sql);
$list = Array ();
if ($sql && mysql_affected_rows () >0) {
while ($row = Mysql_fetch_assoc ($sql)) {
$list [] = $row;
}
}return $list;
}

/**
* [delete delete data]
* @param [string] $table [table name]
* @param [string] $where [condition]
* @return [Boolean] [return Result]
*/
//Delete data SQL statement encapsulates
function Delete ($table, $where) {
//construct deleted SQL statement
$sql = "Delete from ' $table ' where $ where; ";
//Execute SQL statement
$res = mysql_query ($sql);
//Determine if execution succeeds
if ($res && mysql_affected_rows () >0) {
return true;
}else{
return false;
}
}

/**
* [Update data]
* @param [string] $table [table name]
* @param [array] $array [data to update]
* @param [string] $w Here [condition]
* @return [type] [return result]
*/
//Update data SQL statement encapsulation
Function Update ($table, $array, $where) {
$str = '; br> foreach ($array as $key = = $value) {
$str. = "'". $key. " ' = '. $value. "',";
}
//Remove the rightmost comma
$str = RTrim ($str, ', ');
//construct updated SQL statement
$sql = "Update ' $table ' SET $str WHERE $where";
Dump ($sql); exit;
$res = mysql_query ($sql);
//Determine if the execution succeeds
if ($res) {
return true;
} else{
return false;
}
}

/**
* [insert inserts data]
* @param [string] $table [table name]
* @param [array] $data [data]
* @return [BOOL] [succeeded]
*/
function Insert ($table, $data) {
$tmp = Array_keys ($data);
$field = ". Implode (", ", $tmp). ' '
$value = "'". Implode ("', '", $data). "'";
$sql = "INSERT into $table ($field) VALUES ($value)";
Dump ($sql); exit;
$res = mysql_query ($sql);
if ($res && mysql_affected_rows () >0) {
return mysql_insert_id ();
}else{
return false;
}
}

Pre-typeset printing
function Dump ($content) {
echo "<pre>";
Print_r ($content);
echo "</pre>";
}


/**
*
* character intercept
* @param string $string
* @param int $start
* @param int $length
* @param str ing $charset
* @param string $dot
*
* @return String
*/
Function str_cut (& $string, $start, $length , $charset = "Utf-8", $dot = ' ... ') {
if (function_exists (' mb_substr ')) {
if (Mb_strlen ($string, $charset) > $len gth) {
return mb_substr ($string, $start, $length, $charset). $dot;
}
Return Mb_substr ($string, $start, $length, $charset);

}else if (function_exists (' iconv_substr ')) {
if (Iconv_strlen ($string, $charset) > $length) {
Return I Conv_substr ($string, $start, $length, $charset). $dot;
}
Return Iconv_substr ($string, $start, $length, $charset);
}

$charset = Strtolower ($charset);
Switch ($charset) {
Case "Utf-8":
Preg_match_all ("/[\x01-\x7f]|[ \xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]| [\xe1-\xef] [\X80-\XBF] [\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]| [\xf1-\xf7] [\X80-\XBF] [\X80-\XBF] [\x80-\xbf]/], $string, $ar); if (Func_num_args () >= 3) {if (count ($ar [0]) > $length) {return join ("", Array_slice ($ar [0], $start, $length)). $dot; } return Join ("", Array_slice ($ar [0], $start, $length));} else {return join ("", Array_slice ($ar [0], $start));} Break Default: $start = $start * 2; $length = $length * 2; $strlen = strlen ($string); for ($i = 0; $i < $strlen; $i + +) {if ($i >= $start && $i < ($start + $length)) {if (Ord (SUBSTR) ($ String, $i, 1) > 129) $tmpstr. = substr ($string, $i, 2); else $tmpstr. = substr ($string, $i, 1); } if (Ord (substr ($string, $i, 1)) > 129) $i + +; } if (strlen ($TMPSTR) < $strlen) $tmpstr. = $dot; return $tmpstr; }}


/**
* [uploads file upload function]
* @param [string] $name [Name of form Field]
* @param [string] $catalog [path saved by file]
* @param array $type [allowed file types to upload]
* @param integer $size [file size allowed to upload]
* @return [Array] [ERROR 1 upload failed 2 upload succeeded]
*/
function uploads ($name, $catalog, $type =array (' jpg ', ' jpeg ', ' gif ', ' png '), $size =1048576) {
$status = $_files[$name] [' ERROR '];
if ($status >0) {
Switch ($status) {
Case 1:
$res [' msg '] = "File upload exceeds maximum 2M";
$res [' err '] = 1;
return $res;
Break
Case 2:
$res [' msg '] = "File upload exceeds max_file_size size";
$res [' err '] = 1;
return $res;
Break
Case 3:
$res [' msg '] = "File upload failed";
$res [' err '] = 1;
return $res;
Break
Case 4:
$res [' msg '] = ' Please select file ';
$res [' err '] = 1;
return $res;
Break
Default
Break
}
}
if ($_files[$name [' Size ']> $size) {
$res [' msg '] = ' upload file exceeds specified size ';
$res [' err '] = 1;
return $res;
}
$ext = PathInfo ($_files[$name [' name '],pathinfo_extension);

if (!in_array ($ext, $type)) {
$res [' msg '] = ' Please upload the specified file type ';
$res [' err '] = 1;
return $res;
}
The first approach
$catalog = RTrim ($catalog, '/');
$dir = $catalog;
if (!is_dir ($dir)) {
mkdir ($dir, 0777,true);
}
do{
$file = Time (). Mt_rand (1000,9999);

$filename = $file. '. '. $ext;

$newname = $dir. '/'. $filename;

}while (Is_file ($dir. '/'. $filename));
Move_uploaded_file ($_files[$name [' Tmp_name '], $dir. '/'. $filename);
$res [' msg '] = ' file upload succeeded ';
$res [' err '] = 2;
$res [' filename '] = $filename;
$res [' name '] = $filename;
return $res;
}


Functions for encapsulating thumbnails
function Small ($file, $widths, $heights, $address) {
$filename = "$file";
$info = getimagesize ($filename);

Get the width of the picture
$width = $info [0];
Get pictures of high
$height = $info [1];

Open picture
if ($info [2]==1) {
$parent = Imagecreatefromgif ($filename);
}else if ($info [2]==2) {
$parent = Imagecreatefromjpeg ($filename);
}else if ($info [2]==3) {
$parent = Imagecreatefrompng ($filename);
}

$son _width = $widths;
$son _height = $heights;
Equal scale Scaling
$son _height = ceil (($height * $son _width)/$width);

w/h = S_w/s_h
New Image
$son = Imagecreatetruecolor ($son _width, $son _height);

Imagecopyresized ($son, $parent, 0,0,0,0, $son _width, $son _height, $width, $height);
$path = PathInfo ($filename, pathinfo_extension);
$time = time ();
$pathname = $time. Mt_rand (1000,9999). $path;

$save = $address. $pathname;
if ($info [2]==1) {imagegif ($son, $save);} else if ($info [2]==2) {imagejpeg ($son, $save);} else if ($info [2]==3) {imagepng ($son, $save);} Imagedestroy ($son); Imagedestroy ($parent); return $pathname; }

/**
* Get the real IP address of the user
*
* @access Public
* @return String
*/
function Real_ip ()
{
static $realip = NULL;

if ($realip!== NULL)
{
return $realip;
}

if (Isset ($_server))
{
if (Isset ($_server[' http_x_forwarded_for '))
{
$arr = Explode (', ', $_server[' http_x_forwarded_for ');

/* Take the first non-unknown valid IP string in X-forwarded-for */
foreach ($arr as $ip)
{
$ip = Trim ($IP);

if ($ip! = ' Unknown ')
{
$realip = $ip;

Break;
}
}
}
ElseIf (isset ($_server[' http_client_ip '))
{
$realip = $_server[' http_client_ip '];
}
Else
{
if (isset ($_server[' remote_addr '))
{
$realip = $_server[' remote_addr '];
}
Else
{
$realip = ' 0.0.0.0 ';
}
}
}
Else
{
if (getenv (' http_x_forwarded_for '))
{
$realip = getenv (' http_x_forwarded_for '); br>}
ElseIf (getenv (' http_client_ip '))
{
$realip = getenv (' http_client_ip ');} else {$realip = getenv (' Remot E_addr '); }} preg_match ("/[\d\.") {7,15}/", $realip, $ONLINEIP); $realip =!empty ($onlineip [0])? $ONLINEIP [0]: ' 0.0.0.0 '; return $realip;}

/**
* [ArraySort Infinite Pole classification function]
* @param [Type] $arr [description]
* @param integer $parentid [description]
* @return [Type] [description]
*/
function ArraySort ($arr, $id, $pid, $parentid =0) {
$list =array ();
foreach ($arr as $key = = $v) {
if ($v [$pid]== $parentid) {
$tmp = ArraySort ($arr, $id, $pid, $v [$id]);
if ($tmp) {
$v [' submenu '] = $tmp;
}
$list []= $v;
}
}
return $list;
}

PHP function Encapsulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.