首页 > 编程技术 > php

cakephp2.X多表联合查询join及使用分页查询的方法

发布时间:2017-7-6 23:49

小编推荐的这篇文章介绍了cakephp2.X多表联合查询join及使用分页查询的方法,非常实用,有兴趣的同学快看看吧

格式化参数:

 代码如下 复制代码

publicfunctiongetconditions($data){

  $this->loadModel("Cm.LoginHistory");

  $conditions=array();

  foreach($dataas$key=>$val){

    if($key=='start_date'){

      $conditions['LoginHistory.logintime >=']=trim($val);

    }elseif($key=='end_date'){

      $conditions['LoginHistory.logouttime <=']=trim($val);

    }elseif($key=='selectvsoftid'and$val!=''){

      $conditions['LoginHistory.LOGINSUBSYSTEM LIKE']='%'.trim($val).'%';

    }elseif($key=='username'and$val!=''){

      $conditions['LoginHistory.USERNAME LIKE']='%'.trim($val).'%';

    }elseif($key=='vdevicename'and$val!=''){

      $conditions['LoginHistory.WINDOWNAME LIKE']='%'.trim($val).'%';

    }elseif($key=='selectvsoftid'and$val!=''){

      $conditions['LoginHistory.PHDEVICENAME LIKE']='%'.trim($val).'%';

    }

  }

  return$conditions;

}

分页查询:

 代码如下 复制代码

$fields=array('LoginHistory.windowname','LoginHistory.loginsubsystem','VirtualDevice.PHDEVICEID','LoginHistory.userid','VirtualDevice.SYSTYPEID','UserInfo.USERNAME','"SUM"(LoginHistory.LOGOUTTIME-LoginHistory.LOGINTIME) moument','UserInfo.USERNAME','DeviceInfo.DEVICENAME');

$group=array('LoginHistory.windowname','LoginHistory.loginsubsystem','LoginHistory.userid','UserInfo.USERNAME','DeviceInfo.DEVICENAME','VirtualDevice.SYSTYPEID','VirtualDevice.PHDEVICEID');

$this->paginate =array('conditions'=>$conditions,'fields'=>$fields,'group'=>$group,'limit'=>9,'joins'=>array(

   array(

     'alias'=>'UserInfo',

     'table'=>'SMM_USERINFO',

     'type'=>'LEFT',

     'conditions'=>' LoginHistory.USERID=UserInfo.USERID ',

   ),

   array(

     'alias'=>'VirtualDevice',

     'table'=>'ET_NMVIRTUALDEVICEINFO',

     'type'=>'LEFT',

     'conditions'=>' LoginHistory.WINDOWNAME=VirtualDevice.DEVICENAME ',

   ),

   array(

     'alias'=>'DeviceInfo',

     'table'=>'ET_NMDEVICEINFO',

     'type'=>'LEFT',

     'conditions'=>' VirtualDevice.PHDEVICEID=DeviceInfo.DEVICEID ',

   ),

 ));

$LoginHistory=$this->paginate('LoginHistory');

$this->set('loginhistory',$LoginHistory);

小编推荐的这篇文章介绍了PHP获取中国时间(上海时区时间)及美国时间的方法,非常实用,有兴趣的同学快来看看吧

中国时间:

 

 代码如下 复制代码

/**

 * 获取中国时间,即上海时区时间

 * @param <type> $format

 * @return <type>

 */

functiongetChinaTime($format="Y-m-d H:i:s") {

  $timezone_out= date_default_timezone_get();

  date_default_timezone_set('Asia/Shanghai');

  $chinaTime=date($format);

  date_default_timezone_set($timezone_out);

  return$chinaTime;

}

echogetChinaTime();//输出当前时间,如:2017-02-23 11:50:50

 

美国时区:

America/New_York 美国东部

封装了另外一个方法:

 

 代码如下 复制代码

/**

 * 时间格式化

 * @param string $dateformat 时间格式

 * @param int $timestamp 时间戳

 * @param int $timeoffset 时区偏差

 * @return string

 */

functionqgmdate($dateformat='Y-m-d H:i:s',$timestamp='',$timeoffset= 8) {

  if(empty($timestamp)) {

    $timestamp= time();

  }

  $result=gmdate($dateformat,$timestamp+$timeoffset* 3600);

  return$result;

}

//应用举例:获取美国时间

echoqgmdate('Y-m-d H:i:s','', -4);//输出美国时间,如:2017-02-22 23:51:17

 

小编推荐的这篇文章介绍了利用php生成验证码的教程,非常实用,有兴趣的同学快来看看吧。

 

 代码如下复制代码

<?php

/**

 * php生成验证码

 * @param $width 画布宽

 * @param $height 画布高

 * @param $vcodelen 验证码长度

 * @param $pointnum 干扰像素点数量

 * @param $linenum 干扰线条数量

 *

 * 思路:创建验证码画布,生成并填充背景色,生成验证码内容/干扰像素点/线,填充到画布,输出。

 */

 $width= 100;

 $height= 30;

 $vcodelen= 4;

 $pointnum= 200;

 $linenum= 3;

 // 创建画布

 $image= imagecreatetruecolor($width,$height);

 // 创建色块

 $bgcolor= imagecolorallocate($image, 255, 255, 255);

 // 填充画布背景色

 imagefill($image, 0, 0,$bgcolor);

 // 验证码内容

 for($i=0;$i<$vcodelen;$i++) {

  // 字体大小

  $fontsize= 5;

  // 字体颜色,颜色在限定范围内随机

  $fontcolor= imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));

  $data='abcdefghijklmnopqrstuvwxyz0123456789'

  // 验证码内容在以上字符串内随机截取

  $fontcontent=substr($data, rand(0,strlen($data)),1);

  // 字符串显示位置

  $x= ($i*$width/4)+rand(5,15);

  $y= rand(5,10);

  // 字符串填充图片

  // imagestring的字体大小可选1-5,字体再大需要用imagettftext函数(需要字体文件)

  imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);

  // imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, '/font/Geneva.dfont', $fontcontent);

 }

 // 干扰像素点

 for($i=0;$i<$pointnum;$i++) {

  $pointcolor= imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));

  // 画布填充像素点函数

  imagesetpixel($image, rand(0,$width), rand(0,$height),$pointcolor);

 }

 // 干扰线条

 for($i=0;$i<$linenum;$i++) {

  $linecolor= imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));

  // 画布填充线条函数

  imageline($image, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height),$linecolor);

 }

 // 图片输出格式

 header('content-type: image/png');

 // 输出验证码图片

 imagepng($image);

 // 销毁画布

 imagedestroy($image);

?>

 

小编给大家推荐的这篇文章介绍了php生成图片缩略图功能示例,非常实用,有兴趣的同学快看看吧

本文实例讲述了php生成图片缩略图功能。分享给大家供大家参考,具体如下:

完整代码如下:

 

 代码如下 复制代码

<?php

/*

 * Created on 2011-3-18

 *

 * To change the template for this generated file go to

 * Window - Preferences - PHPeclipse - PHP - Code Templates

*/

/*构造函数-生成缩略图+水印,参数说明:

$srcFile-图片文件名,

$dstFile-另存文件名,

$markwords-水印文字,

$markimage-水印图片,

$dstW-图片保存宽度,

$dstH-图片保存高度,

$rate-图片保存品质*/

makethumb("1.jpg","aa/b.jpg","50","50");

functionmakethumb($srcFile,$dstFile,$dstW,$dstH,$rate= 100,$markwords= null,$markimage= null) {

  $data=GetImageSize($srcFile);

  switch($data[2]) {

    case1:

      $im= @ImageCreateFromGIF($srcFile);

    break;

    case2:

      $im= @ImageCreateFromJPEG($srcFile);

    break;

    case3:

      $im= @ImageCreateFromPNG($srcFile);

    break;

  }

  if(!$im)returnFalse;

  $srcW= ImageSX($im);

  $srcH= ImageSY($im);

  $dstX= 0;

  $dstY= 0;

  if($srcW*$dstH>$srcH*$dstW) {

    $fdstH=round($srcH*$dstW/$srcW);

    $dstY=floor(($dstH-$fdstH) / 2);

    $fdstW=$dstW;

  }else{

    $fdstW=round($srcW*$dstH/$srcH);

    $dstX=floor(($dstW-$fdstW) / 2);

    $fdstH=$dstH;

  }

  $ni= ImageCreateTrueColor($dstW,$dstH);

  $dstX= ($dstX< 0) ? 0 :$dstX;

  $dstY= ($dstX< 0) ? 0 :$dstY;

  $dstX= ($dstX> ($dstW/ 2)) ?floor($dstW/ 2) :$dstX;

  $dstY= ($dstY> ($dstH/ 2)) ?floor($dstH/ s) :$dstY;

  $white= ImageColorAllocate($ni, 255, 255, 255);

  $black= ImageColorAllocate($ni, 0, 0, 0);

  imagefilledrectangle($ni, 0, 0,$dstW,$dstH,$white);// 填充背景色

  ImageCopyResized($ni,$im,$dstX,$dstY, 0, 0,$fdstW,$fdstH,$srcW,$srcH);

  if($markwords!= null) {

    $markwords= iconv("gb2312","UTF-8",$markwords);

    //转换文字编码

    ImageTTFText($ni, 20, 30, 450, 560,$black,"simhei.ttf",$markwords);//写入文字水印

    //参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容

     

  }elseif($markimage!= null) {

    $wimage_data=GetImageSize($markimage);

    switch($wimage_data[2]) {

      case1:

        $wimage= @ImageCreateFromGIF($markimage);

      break;

      case2:

        $wimage= @ImageCreateFromJPEG($markimage);

      break;

      case3:

        $wimage= @ImageCreateFromPNG($markimage);

      break;

    }

    imagecopy($ni,$wimage, 500, 560, 0, 0, 88, 31);//写入图片水印,水印图片大小默认为88*31

    imagedestroy($wimage);

  }

  ImageJpeg($ni,$dstFile,$rate);

  ImageJpeg($ni,$srcFile,$rate);

  imagedestroy($im);

  imagedestroy($ni);

}

?>

 

标签:[!--infotagslink--]

您可能感兴趣的文章: