数组分页函数

数组分页  


/**  
 * 数组分页函数
 * $array   查询出来的所有数组  
 * $page   当前第几页  
 * $page_size   每页多少条数据  
 * order 0 - 不变     1- 反序  
 */
function page_array($array, $page = 1, $page_size = 20, $order = 0)
{
    // 判断当前页面是否为空 如果为空就表示为第一页面    
    $page = (empty($page)) ? '1' : $page;
    // 计算每次分页的开始位置  
    $start = ($page - 1) * $page_size;

    if ($order == 1) {
        $array = array_reverse($array);
    }
    $pagedata = array_slice($array, $start, $page_size);
    return $pagedata;
}

php导入cvs

导入  cvs  

    // 导入
    public function upload_export()
    {
        if ($_POST['dosubmit']) {

            try {
                $hattach = new hattach();
                $tmpPath = $hattach->uploadfile($_FILES['excel'], array('csv'));
                $myfile = file($tmpPath);
                $i = 0;
                $row_data = array();
                for ($i = 0; $i < count($myfile); $i++) {
                    // 过滤第一条
                    if ($i == 0) {
                        continue;
                    }
                    $encoding = mb_detect_encoding($myfile[$i], array('GB2312', 'GBK', 'UTF-16', 'UCS-2', 'UTF-8', 'BIG5', 'ASCII'));
                    $r = explode(',', $myfile[$i]);
                    $row_data[] = array(
                        'mobile' => iconv($encoding, 'UTF-8', $r[0]),
                        'dateline' => $r[1] ? strtotime(iconv($encoding, 'UTF-8', $r[1])) : time(),
                        'contact' => iconv($encoding, 'UTF-8', $r[2]),
                        'company' => iconv($encoding, 'UTF-8', $r[3]),
                        'organize' => iconv($encoding, 'UTF-8', $r[4]),
                        'job' => iconv($encoding, 'UTF-8', $r[5]),
                        'area' => iconv($encoding, 'UTF-8', $r[6]),
                        'work_year' => iconv($encoding, 'UTF-8', $r[7]),
                        'skill' => iconv($encoding, 'UTF-8', $r[8]),
                        'cy' => iconv($encoding, 'UTF-8', $r[9]),
                        'cy_direction' => iconv($encoding, 'UTF-8', $r[10]),
                        'video' => iconv($encoding, 'UTF-8', $r[11]),
                        'video_name' => iconv($encoding, 'UTF-8', $r[12]),
                        'remark' => iconv($encoding, 'UTF-8', $r[13]),
                        'utm_source' => iconv($encoding, 'UTF-8', $r[14]),
                        'utm_medium' => iconv($encoding, 'UTF-8', $r[15]),
                        'utm_campaign' => iconv($encoding, 'UTF-8', $r[16]),
                        'utm_content' => iconv($encoding, 'UTF-8', $r[17]),
                        'utm_term' => iconv($encoding, 'UTF-8', $r[18]),
                        'source' => iconv($encoding, 'UTF-8', $r[19]),
                        'mobile_status' => iconv($encoding, 'UTF-8', $r[20]),
                        'progress' => $r[21] ? iconv($encoding, 'UTF-8', $r[21]) : 0,
                        'username_remark' => iconv($encoding, 'UTF-8', $r[22]),
                        'ownership' => iconv($encoding, 'UTF-8', $r[23]),
                    );
                }

                foreach ($row_data as $key => $value) {
                    // 缺少手机或者联系人自动过滤
                    if (!$value['mobile'] || intval($value['mobile']) < 100 || !$value['contact']) {
                        continue;
                    }
                    srv_spl_partner_collection::I()->save_collection($value);
                    $ownership=str_replace(array("\n", "\r"), '',  $value['ownership']);
                    if ($ownership){
                        srv_spl_partner_collection::I()->update_collection(array('mobile' => $value['mobile']), array('status' => 2));
                    }
                    
                }
            } catch (Exception $e) {
                hmsg($e->getMessage());
            }
            hmsg('操作成功', '', 1000, 'edit');
        } else {
            include admtpl('admin_partner', 'upload_export');
        }
    }

php导出csv

csv  php  excel  

    //导出订单
    private function _export_collection($data_v)
    {
        $data = array();
        $up_status = $this->up_status();
        $progress_status = $this->progress_status();
        $mobile_status = $this->mobile_status();
        $cy_status=$this->cy_status();
        $data[] = array('手机号码', 'uid', '提交时间', '联系人', '公司名称', '机构类型', '当前职位', '所在地区', '工作年限', '擅长领域', '是否创业', '创业方向', '是否有课', '课程名称', '备注', '操作记录', '状态', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', '来源', '电话有效性', '进展', '跟进备注', '分配对象');
        foreach ($data_v as $key => $mobile) {
            $i = 1;
			$data[] = array(
				"\t" . $v['mobile'],
				$v['uid'],
				date('Y-m-d H:i:s', $v['dateline']),
				$v['contact'],
				$v['company'],
				$v['organize'],
				$v['job'],
				$v['area'],
				$v['work_year'],
				$v['skill'],
				$v['cy'] >= 1 ? $cy_status[$v['cy']] : '',
				$v['cy_direction'],
				$v['video'] >= 1 ? $cy_status[$v['video']] : '',
				$v['video_name'],
				$v['remark'],
				$mobile['collection_log'][0]['dateline'] ? sprintf('%s %s %s',date('Y-m-d H:i:s', $mobile['collection_log'][0]['dateline']) , $mobile['collection_log'][0]['username'], str_replace(array(','),'|',$mobile['collection_log'][0]['remark'])) : '',
				$up_status[$v['status']],
				$v['utm_source'],
				$v['utm_medium'],
				$v['utm_campaign'],
				$v['utm_content'],
				$v['utm_term'],
				$v['source'],
				$v['mobile_status'] > 1 ? $mobile_status[$v['mobile_status']] : '',
				$v['progress'] == 0 ? '' : $progress_status[$v['progress']]['name'],
				$v['username_remark'],
				$v['ownership'],
			);
			$i = $i + 1;
        }
        fx_phpexcel_utl::export_csv($data);
    }


    // CVS导出速度快
    public static function export_csv($data)
    {
        $string = "";
        foreach ($data as $key => $value) {
            foreach ($value as $k => $val) {
                $value[$k]= str_replace(array(","), '|',  $value[$k]);
                $value[$k] = iconv('utf-8', 'gbk//IGNORE',  str_replace(array("\n", "\r"), '',  $value[$k]));
            }
            $string .= implode(",", str_replace(PHP_EOL, '',  $value)) . "\n"; //用英文逗号分开
        }
        $filename = date('Ymd') . '.csv'; //设置文件名
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=" . $filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo $string;
    }

redis并控制

并发控制  redis  


/**
 * 限制某个请求多少秒内,可以请求多少次
 * @param $key value
 * @param $expire 失效时间
 * @return $num 次数
 */
function request_limit($key, $expire, $num)
{
    $redis_get = fx_redis_service::I()->get($key);
    if (!$redis_get) {
        fx_redis_service::I()->incr($key);
        fx_redis_service::I()->expire($key, $expire);
    } else {
        $total = fx_redis_service::I()->incr($key);
        // 高并发的时候过期时间会变成-1,所以需要重置过期时间,否则这个IP永远不能请求
        $ttl = fx_redis_service::I()->ttl($key);
        if ($ttl < 1){
            fx_redis_service::I()->expire($key, $expire);
        }
        if (intval($total) > $num) {
            fx_log::logs2file(__FILE__, __FUNCTION__, $_GET, $_POST, $key . '请求太频繁..', '', fx_utl::hip(false));
            $json = json_encode(array('result' => 1, 'msg' => '请求太频繁'), JSON_UNESCAPED_UNICODE);
            echo $json;
            exit();
        }
    }
}

微信web置顶功能

微信  php  

WEB微信扫码登陆后置顶

伯乐常用语法调用

php  伯乐  

伯乐phpMVC框架开发,常用语法使用.