|
|
|
รบกวนช่วยอธิบายโค๊ดหน้า function ให้หน่อยค่ะ พอดีไม่เข้าใจโค๊ดค่ะ |
|
|
|
|
|
|
|
Code (PHP)
<?php
class MyClass{
public function selQuery($sqlTxt){
if(preg_match('/SELECT/', strtoupper($sqlTxt))){
$result = mysql_query($sqlTxt);
$num = mysql_num_rows($result);
if($num > 0){
$arrRec = array();
while($rs = mysql_fetch_assoc($result)){
array_push($arrRec,$rs);
}
return $arrRec;
}else{
echo null;
}
}else{
echo null;
}
}
public function query($sql){
if(@mysql_query($sql))
return true;
else
return false;
}
public function chkUniqData($tablename,$field,$data){
$result = mysql_query("Select $field From $tablename Where $field = '$data'");
$num = mysql_num_rows($result);
if($num >= 1)
return false;
else
return true;
}
public function random_id($len){
srand((double)microtime()*10000000);
$chars = "0123456789";
$ret_str = "";
$count = 0;
$chars2 = "";
while($count<strlen($chars)){
for($i=0;$i<$len;$i++){
$chars2 .= $chars[$count];
}
$count++;
}
$num = strlen($chars2);
for($i = 0; $i < $len; $i++)
{
$ret_str.= $chars2[rand()%$num];
$ret_str.="";
}
return $ret_str;
}
public function getDateFormat($timestamp, $type = "d"){
$d = date("d", $timestamp);
$m = date("m", $timestamp);
$y = date("Y", $timestamp)+543;
$h = date("H", $timestamp);
$i = date("i", $timestamp);
$s = date("s", $timestamp);
$thai_m = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฏาคม","สิงหาคม",
"กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
if($type == "d"){
return $d." ".$thai_m[$m-1]." ".$y;
}else if($type == "dt"){
return $d." ".$thai_m[$m-1]." ".$y.", เวลา ".$h.":".$i."น.";
}
}
public function uploadPhoto($arrFile,$name,$dirnm,$limit=0,$option='w'){
$array_last=explode(".", $arrFile['name']);
$c=count($array_last)-1;
$filename=strtolower($array_last[$c]);
$photo_name = $name.'.'.$filename;
$chk=false;
$arrAuthorList=array('jpg','jpeg','gif','png');
foreach($arrAuthorList as $key){
if($key==$filename){
$chk=true;
break;
}
}
if($chk){
copy($arrFile['tmp_name'], $dirnm.$photo_name);
$images = $dirnm.$photo_name;
$img_size=GetimageSize($images);
if($option=='w' AND $limit>0){
if($img_size[0]>$limit){
$img_width=$limit;
$img_height=round($img_width*$img_size[1]/$img_size[0]);
}else{
$img_width=$img_size[0];
$img_height=$img_size[1];
}
}else if($option=='h' AND $limit>0){
if($img_size[1]>$limit){
$img_height=$limit;
$img_width=round($img_height*$img_size[0]/$img_size[1]);
}else{
$img_width=$img_size[0];
$img_height=$img_size[1];
}
}else{
$img_width=$img_size[0];
$img_height=$img_size[1];
}
if($filename=="jpg" or $filename=="jpeg"){
$newImg=ImageCreateFromJPEG($images);
$newX=ImagesX($newImg);
$newY=ImagesY($newImg);
$images_fin=ImageCreateTrueColor($img_width, $img_height);
ImageCopyResampled($images_fin, $newImg, 0, 0, 0, 0, $img_width+1, $img_height+1, $newX, $newY);
ImageJPEG($images_fin, $dirnm.$photo_name);
}else if($filename=="gif"){
$newImg=ImageCreateFromGIF($images);
$newX=ImagesX($newImg);
$newY=ImagesY($newImg);
$images_fin=ImageCreateTrueColor($img_width, $img_height);
ImageCopyResampled($images_fin, $newImg, 0, 0, 0, 0, $img_width+1, $img_height+1, $newX, $newY);
ImageGIF($images_fin, $dirnm.$photo_name);
}else if($filename=="png"){
$newImg=ImageCreateFromPNG($images);
$newX=ImagesX($newImg);
$newY=ImagesY($newImg);
$images_fin=ImageCreateTrueColor($img_width, $img_height);
ImageCopyResampled($images_fin, $newImg, 0, 0, 0, 0, $img_width+1, $img_height+1, $newX, $newY);
ImagePNG($images_fin, $dirnm.$photo_name);
}
ImageDestroy($newImg);
ImageDestroy($images_fin);
unlink($arrFile['tmp_name']);
return $photo_name;
}else{
return null;
}
}
public function randomNum($num){
srand((double)microtime()*10000000);
$n = rand()%$num;
return $n;
}
public function randNonUniqe($num){
srand((double)microtime()*10000000);
$a = array();
while (count($a) < $num) {
$n = rand()%$num;
if (!in_array($n, $a)) array_push($a,$n);
}
return $a;
}
public function getItemsFromCart($cart){
if (isset($cart)) {
$items = explode(',',$cart); //ใช้คอมม่าเป็นตัวแยกข้อมูล
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; //นำค่าที่ได้มาเก็บในaray
}
return $contents;
}else{
return "";
}
}
}
?>
Tag : PHP
|
|
|
|
|
|
Date :
2012-03-22 16:51:27 |
By :
hikarujun |
View :
841 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จับใส่ tag php หน่อยก็ดีน่ะครับ ดูยากจัง
Code (PHP)
?php
class MyClass{
public function selQuery($sqlTxt){
if(preg_match('/SELECT/', strtoupper($sqlTxt))){
$result = mysql_query($sqlTxt);
$num = mysql_num_rows($result);
if($num > 0){
$arrRec = array();
while($rs = mysql_fetch_assoc($result)){
array_push($arrRec,$rs);
}
return $arrRec;
}else{
echo null;
}
}else{
echo null;
}
}
public function query($sql){
if(@mysql_query($sql))
return true;
else
return false;
}
public function chkUniqData($tablename,$field,$data){
$result = mysql_query("Select $field From $tablename Where $field = '$data'");
$num = mysql_num_rows($result);
if($num >= 1)
return false;
else
return true;
}
public function random_id($len){
srand((double)microtime()*10000000);
$chars = "0123456789";
$ret_str = "";
$count = 0;
$chars2 = "";
while($count<strlen($chars)){
for($i=0;$i<$len;$i++){
$chars2 .= $chars[$count];
}
$count++;
}
$num = strlen($chars2);
for($i = 0; $i < $len; $i++)
{
$ret_str.= $chars2[rand()%$num];
$ret_str.="";
}
return $ret_str;
}
public function getDateFormat($timestamp, $type = "d"){
$d = date("d", $timestamp);
$m = date("m", $timestamp);
$y = date("Y", $timestamp)+543;
$h = date("H", $timestamp);
$i = date("i", $timestamp);
$s = date("s", $timestamp);
$thai_m = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฏาคม","สิงหาคม",
"กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
if($type == "d"){
return $d." ".$thai_m[$m-1]." ".$y;
}else if($type == "dt"){
return $d." ".$thai_m[$m-1]." ".$y.", เวลา ".$h.":".$i."น.";
}
}
public function uploadPhoto($arrFile,$name,$dirnm,$limit=0,$option='w'){
$array_last=explode(".", $arrFile['name']);
$c=count($array_last)-1;
$filename=strtolower($array_last[$c]);
$photo_name = $name.'.'.$filename;
$chk=false;
$arrAuthorList=array('jpg','jpeg','gif','png');
foreach($arrAuthorList as $key){
if($key==$filename){
$chk=true;
break;
}
}
if($chk){
copy($arrFile['tmp_name'], $dirnm.$photo_name);
$images = $dirnm.$photo_name;
$img_size=GetimageSize($images);
if($option=='w' AND $limit>0){
if($img_size[0]>$limit){
$img_width=$limit;
$img_height=round($img_width*$img_size[1]/$img_size[0]);
}else{
$img_width=$img_size[0];
$img_height=$img_size[1];
}
}else if($option=='h' AND $limit>0){
if($img_size[1]>$limit){
$img_height=$limit;
$img_width=round($img_height*$img_size[0]/$img_size[1]);
}else{
$img_width=$img_size[0];
$img_height=$img_size[1];
}
}else{
$img_width=$img_size[0];
$img_height=$img_size[1];
}
if($filename=="jpg" or $filename=="jpeg"){
$newImg=ImageCreateFromJPEG($images);
$newX=ImagesX($newImg);
$newY=ImagesY($newImg);
$images_fin=ImageCreateTrueColor($img_width, $img_height);
ImageCopyResampled($images_fin, $newImg, 0, 0, 0, 0, $img_width+1, $img_height+1, $newX, $newY);
ImageJPEG($images_fin, $dirnm.$photo_name);
}else if($filename=="gif"){
$newImg=ImageCreateFromGIF($images);
$newX=ImagesX($newImg);
$newY=ImagesY($newImg);
$images_fin=ImageCreateTrueColor($img_width, $img_height);
ImageCopyResampled($images_fin, $newImg, 0, 0, 0, 0, $img_width+1, $img_height+1, $newX, $newY);
ImageGIF($images_fin, $dirnm.$photo_name);
}else if($filename=="png"){
$newImg=ImageCreateFromPNG($images);
$newX=ImagesX($newImg);
$newY=ImagesY($newImg);
$images_fin=ImageCreateTrueColor($img_width, $img_height);
ImageCopyResampled($images_fin, $newImg, 0, 0, 0, 0, $img_width+1, $img_height+1, $newX, $newY);
ImagePNG($images_fin, $dirnm.$photo_name);
}
ImageDestroy($newImg);
ImageDestroy($images_fin);
unlink($arrFile['tmp_name']);
return $photo_name;
}else{
return null;
}
}
public function randomNum($num){
srand((double)microtime()*10000000);
$n = rand()%$num;
return $n;
}
public function randNonUniqe($num){
srand((double)microtime()*10000000);
$a = array();
while (count($a) < $num) {
$n = rand()%$num;
if (!in_array($n, $a)) array_push($a,$n);
}
return $a;
}
public function getItemsFromCart($cart){
if (isset($cart)) {
$items = explode(',',$cart); //ใช้คอมม่าเป็นตัวแยกข้อมูล
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; //นำค่าที่ได้มาเก็บในaray
}
return $contents;
}else{
return "";
}
}
}
?>
|
|
|
|
|
Date :
2012-03-22 16:54:30 |
By :
mangkunzo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ยาวขนาดนี้จะให้อธิบายหมดเหรอครับ เอาเป็นว่าถามจุด ๆ แล้วกันครับ
|
|
|
|
|
Date :
2012-03-22 20:43:01 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|