php : 开发记录(2017-03-10)

0.后台 循环N*10000次操作的简单处理

后台需要循环做N*10000次级别的工作时候,比如,发送邮件,推送通知。可以先把所有数据导入数据表(数据库操作所需的时间1~2秒),然后前台循环发送请求,每个请求,可以执行一次或多次操作,比如一次请求发10封邮件,10个通知等(这些控制,根据需求来定),这样,就可以把任务做成一种类似进度条的效果,并且可以直接展示给用户看,提高用户体验。

 

1.empty(trim($ch_url) :报错

手册:

在 PHP 5.5 之前,empty() 仅支持变量;任何其他东西将会导致一个解析错误。换言之,下列代码不会生效: empty(trim($name))。 作为替代,应该使用trim($name) == false.

2.手机信息提示框(你懂的,复制,粘贴)

<script type='text/javascript' src='resource/js/lib/jquery-1.11.1.min.js'></script>
<style type='text/css'>
	#poptip { position: fixed; top:40%;left:50%;width:160px;margin-left:-80px;height: 27px;background:#000; opacity: 0.7;filter:alpha(opacity=0.7); color:#fff;z-index: 999;  border-radius:5px;-webkit-border-radius:5px;-moz-border-radius:5px;}
	#poptip_content { position: fixed; top:40%;left:50%;width:160px;margin-left:-80px; height: 27px; color:#fff;text-align:center;font-size:14px;z-index: 999999}
</style>
<script language='javascript'>
function tip(msg,autoClose){
	var div = $("#poptip");
	var content =$("#poptip_content");
	if(div.length<=0){
		div = $("<div id='poptip'></div>").appendTo(document.body);
		content =$("<div id='poptip_content'>" + msg + "</div>").appendTo(document.body);
	}else{
		content.html(msg);
		content.show(); div.show();
	}
	if(autoClose) {
		setTimeout(function(){
			content.fadeOut(500);
			div.fadeOut(500);
		},1000);
	}
}
function tip_close(){
	$("#poptip").fadeOut(500);
	$("#poptip_content").fadeOut(500);
}
</script>

达到以下效果:

  

3.使用PHP QR Code生成二维码

phpQrCode 官网:http://phpqrcode.sourceforge.net/ 

使用:引入phpqrcode.php

封装的代码:

/**
* 建立文件夹
*
* @param string $aimUrl
* @return viod
*/
function createDir($aimUrl) {
    $aimUrl = str_replace('', '/', $aimUrl);
    $aimDir = '';
    $arr = explode('/', $aimUrl);
    $result = true;
    foreach ($arr as $str) {
        $aimDir .= $str . '/';
        if (!file_exists($aimDir)) {
            $result = mkdir($aimDir,0777,true);
        }
    }
    return $result;
}

/* --------------------------------------------------------------------
* 创建二维码
* @param content 二维码内容
* @param logoPath 图片文件,或者url
* @param fileName 生成的文件名
* @param errorCorrectionLevel 容错级别 :L、M、Q、H
*/
function create_qrcode($content, $logoPath = FALSE, $fileName = FALSE, $errorCorrectionLevel = 'M'){

    $errorCorrectionLevels = array('L', 'M', 'Q', 'H');
    $errorCorrectionLevel = in_array($errorCorrectionLevel, $errorCorrectionLevels) ? $errorCorrectionLevel : "M";

    $matrixPointSize = 6; // 点的大小:1到10

    if($logoPath == FALSE || empty($logoPath)){

        //生成二维码图片
        QRcode::png($content, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }
    else{

        $path = ATTACHMENT_ROOT . 'temp/qrcode/' . date('Ymd', time());
        $fileName = $path . '/' . $fileName;
        createDir($path);

        $QR = $fileName;

        QRcode::png($content, $QR, $errorCorrectionLevel, $matrixPointSize, 2);

        $QR = imagecreatefromstring(file_get_contents($QR));

        $logo = imagecreatefromstring(file_get_contents($logoPath));

        $QR_width = imagesx($QR); // 二维码图片宽度

        $QR_height = imagesy($QR); //二维码图片高度

        $logo_width = imagesx($logo); //logo图片宽度

        $logo_height = imagesy($logo);  //logo图片高度

        $logo_qr_width = $QR_width / 5;

        $scale = $logo_width/$logo_qr_width;

        $logo_qr_height = $logo_height/$scale;

        $from_width = ($QR_width - $logo_qr_width) / 2;

        //重	新组合图片并调整大小
        imagecopyresampled(
            $QR,
            $logo,
            $from_width,
            $from_width,
            0, 0,
            $logo_qr_width,
            $logo_qr_height,
            $logo_width,
            $logo_height);
        }

        // 输出图片
        Header("Content-type:image/png");
        // header("Content-type:image/png");
        // header("Content-Disposition:attachment; filename=meimei.png");
        ImagePng($QR);
}

4.导出 Excel

关键性代码是:使用 header 即可导出数据

Tip : 这种方式采用 ajax 就不合适了,最好就是一个链接。

header("Content-type:text/csv");
header("Content-Disposition:attachment; filename=" .  date('Y-m-d', time()) . "-代金券核销数据.csv");

剩下的工作,就是拼接字符串了。

例子:

<?php
$html = "\xEF\xBB\xBF";
$filter = array(
			'uid' => 'ID',
			'realname' => '姓名',
			'carno' => '车牌号码',
			'title' => '标题',
			'discount' => '面额',
			/* ... 等等 */
		);
foreach ($filter as $title) {
	$html .= $title . "\t,";
}
$html .= "\n";
foreach ($exports as $k => $v) { // exports 为所有需要导出的数据

	foreach ($filter as $key => $title) {

		if ($key == 'uid') {
			$html .= $v['uid'] . "\t, ";
		}
		elseif ($key == 'realname') {
			$html .= $v['realname']. "\t, ";
		}
		elseif ($key == 'carno') {
			$html .= $v['carno']. "\t, ";
		}
		elseif ($key == 'title') {
			$html .= $v['title']. "\t, ";
		}
		elseif ($key == 'discount') {
			$html .= $v['discount']. "\t, ";
		}
	}

	$html .= "\n";
}

header("Content-type:text/csv");
header("Content-Disposition:attachment; filename=" .  date('Y-m-d', time()) . "-代金券核销数据.csv");
echo $html;
exit();

  

 

 

 

 

 

posted @ 2017-03-10 22:20  KeenLeung  阅读(409)  评论(0编辑  收藏  举报