纵有疾风起
人生不言弃

php常用代码段

点击换验证码

<a href="javascript:;"><img onclick="this.src='{:U('Reglog/vcode')}?'+Math.random()" width="160"  src="{:U('Reglog/vcode')}" /></a>

 TP上一条下一条

 $prev=$artical->where("id < $id")->where(array('cid'=>$cid))->order('id desc')->find();        $next=$artical->where("id > $id")->where(array('cid'=>$cid))->order('id asc')->find();
<if condition='$next'>    下一篇:<a href='{:U("Artical/index",array(id=>$next['id']))}' class="f6">{$next.name}</a><br />      <else />   <strong>下一篇:没有了</strong><br/></if><if condition='$prev'>    上一篇:<a href='{:U("Artical/index",array(id=>$prev['id']))}' class="f6">{$prev.name}</a>    <else />    <strong>上一篇:没有了</strong> </if>

 curl 模拟POST、GET请求 可以用于访问接口

//参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies    function curl_request($url,$post='',$cookie='', $returnCookie=0){        $curl = curl_init();        curl_setopt($curl, CURLOPT_URL, $url);        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);        curl_setopt($curl, CURLOPT_REFERER, "http://XXX");        if($post) {            curl_setopt($curl, CURLOPT_POST, 1);            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));        }        if($cookie) {            curl_setopt($curl, CURLOPT_COOKIE, $cookie);        }        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);        curl_setopt($curl, CURLOPT_TIMEOUT, 10);        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        $data = curl_exec($curl);        if (curl_errno($curl)) {            return curl_error($curl);        }        curl_close($curl);        if($returnCookie){            list($header, $body) = explode("\r\n\r\n", $data, 2);            preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);            $info['cookie']  = substr($matches[1][0], 1);            $info['content'] = $body;            return $info;        }else{            return $data;        }    }

EXCEL导入

/**  *  数据导入  * @param string $file excel文件  * @param string $sheet  * @return string   返回解析数据  * @throws PHPExcel_Exception  * @throws PHPExcel_Reader_Exception */  function importExecl($file='', $sheet=0){      $file = iconv("utf-8", "gb2312", $file);   //转码      if(empty($file) OR !file_exists($file)) {          die('file not exists!');      }      require_once './Classes/PHPExcel.php';  //引入PHP EXCEL类      $objRead = new PHPExcel_Reader_Excel2007();   //建立reader对象      if(!$objRead->canRead($file)){          $objRead = new PHPExcel_Reader_Excel5();          if(!$objRead->canRead($file)){              die('No Excel!');          }      }        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');        $obj = $objRead->load($file);  //建立excel对象      $currSheet = $obj->getSheet($sheet);   //获取指定的sheet表      $columnH = $currSheet->getHighestColumn();   //取得最大的列号      $columnCnt = array_search($columnH, $cellName);      $rowCnt = $currSheet->getHighestRow();   //获取总行数        $data = array();      for($_row=1; $_row<=$rowCnt; $_row++){  //读取内容          for($_column=0; $_column<=$columnCnt; $_column++){              $cellId = $cellName[$_column].$_row;              $cellValue = $currSheet->getCell($cellId)->getValue();               //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue();  #获取公式计算的值              if($cellValue instanceof PHPExcel_RichText){   //富文本转换字符串                  $cellValue = $cellValue->__toString();              }                $data[$_row][$cellName[$_column]] = $cellValue;          }      }        return $data;  }

EXCEL导出

//导出function outExecl($data){    //var_dump($data);    require_once './Classes/PHPExcel.php';     // 首先创建一个新的对象  PHPExcel object    $objPHPExcel = new PHPExcel();         // 设置文件的一些属性,在xls文件——>属性——>详细信息里可以看到这些值,xml表格里是没有这些值的    $objPHPExcel          ->getProperties()  //获得文件属性对象,给下文提供设置资源          ->setCreator( "Maarten Balliauw")                 //设置文件的创建者          ->setLastModifiedBy( "Maarten Balliauw")          //设置最后修改者          ->setTitle( "Office 2007 XLSX Test Document" )    //设置标题          ->setSubject( "Office 2007 XLSX Test Document" )  //设置主题          ->setDescription( "Test document for Office 2007 XLSX, generated using PHP classes.") //设置备注          ->setKeywords( "office 2007 openxml php")        //设置标记          ->setCategory( "Test result file");                //设置类别    // 位置aaa  *为下文代码位置提供锚    // 给表格添加数据    $objPHPExcel->setActiveSheetIndex(0)             //设置第一个内置表(一个xls文件里可以有多个表)为活动的                ->setCellValue( 'A1', '姓名' )         //给表的单元格设置数据                ->setCellValue( 'B1', '球队名称' )      //数据格式可以为字符串                ->setCellValue( 'C1', '场上位置' )            //数字型                ->setCellValue( 'D1', '称号资历' )            //                ->setCellValue( 'E1', '籍贯省份' )           //布尔型                ->setCellValue( 'F1', '籍贯城市' )           //布尔型                ->setCellValue( 'G1', '联系电话' )           //布尔型                ->setCellValue( 'H1', '注册状态' )           //布尔型                ->setCellValue( 'I1', '经纪人' )           //布尔型                ->setCellValue( 'J1', '比赛打分' )           //布尔型                ->setCellValue( 'K1', '伤病状态' )           //布尔型                ->setCellValue( 'L1', '比赛时间' );           //布尔型    $i=2;    foreach ($data as $k => $v) {        // echo "1";        // var_dump($v);        if($v['z_state']=='1'){            $v['z_state']="已注册";        }        $objPHPExcel->setActiveSheetIndex(0)             //设置第一个内置表(一个xls文件里可以有多个表)为活动的                ->setCellValue( 'A'.$i, $v['bname'] )         //给表的单元格设置数据                ->setCellValue( 'B'.$i, $v['tname'] )      //数据格式可以为字符串                ->setCellValue( 'C'.$i, $v['position'] )            //数字型                ->setCellValue( 'D'.$i, $v['qualifications'] )            //                ->setCellValue( 'E'.$i, $v['pname'] )           //布尔型                ->setCellValue( 'F'.$i, $v['cname'] )           //布尔型                ->setCellValue( 'G'.$i, $v['phone'] )           //布尔型                ->setCellValue( 'H'.$i, $v['z_state'] )           //布尔型                ->setCellValue( 'I'.$i, $v['name'] )           //布尔型                ->setCellValue( 'J'.$i, $v['score'] )           //布尔型                ->setCellValue( 'K'.$i, $v['b_state'] )           //布尔型                ->setCellValue( 'L'.$i, $v['time'] );           //布尔型        $i++;    }         //得到当前活动的表,注意下文教程中会经常用到$objActSheet    $objActSheet = $objPHPExcel->getActiveSheet();    // 位置bbb  *为下文代码位置提供锚    // 给当前活动的表设置名称    $objActSheet->setTitle('Simple2222');    // 生成2007excel格式的xlsx文件      header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');      header('Content-Disposition: attachment;filename="01simple.xlsx"');      header('Cache-Control: max-age=0');            $objWriter = PHPExcel_IOFactory:: createWriter($objPHPExcel, 'Excel2007');      $objWriter->save( 'php://output');      exit;  }

 PHPExcel包:http://www.jb51.net/codes/194070.html

文章转载于:https://www.cnblogs.com/wordblog/p/7447761.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » php常用代码段
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录