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

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

后台须要循环作N*10000次级别的工做时候,好比,发送邮件,推送通知。能够先把全部数据导入数据表(数据库操做所需的时间1~2秒),而后前台循环发送请求,每一个请求,能够执行一次或屡次操做,好比一次请求发10封邮件,10个通知等(这些控制,根据需求来定),这样,就能够把任务作成一种相似进度条的效果,而且能够直接展现给用户看,提升用户体验。php

 

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

手册:html

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

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

<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>

达到如下效果:web

  

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

phpQrCode 官网:http://phpqrcode.sourceforge.net/ 数据库

使用:引入phpqrcode.phpapp

封装的代码:

/**
* 创建文件夹
*
* @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();
相关文章
相关标签/搜索