首页 > php开发

PHP中递归函数返回值使用介绍(ecshop无限分类)

在 ecshop 二次开发中做产品分类索引时,要根据分类 id 取得所属顶级分类 id 。第一个反应就是用递归递出来,于是写了递归函数如下:

function getCatTopId($cat_id)
{
    if ($cat_id)
    {
        $res = Array();
        $sql = 'SELECT cat_id, parent_id'
             . ' FROM ' . $GLOBALS['ecs']->table('category')
             . ' WHERE cat_id = ' . $cat_id . ' AND is_show = 1';

阅读全文

php session工作原理分析

众所周知,http协议是一个无状态协议,简单来说就是,web服务器是不知道现在连接上来的人到底是哪个人,为了满足选择性发送信息的需求,在http的基础上做了很多扩展来达到这个目的,如数字签名、cookie、session等。
web服务器或者web程序如何能够知道现在连接上来的是谁?要解决这个问题,首先需要在服务器端和客户端建立一一对应关系,下边我通过抓取http的内容来说明这种对应关系是如何建立的。
我使用的是一个叫做httplook的http包嗅探工具,然后在本地web服务器的根目录下建立一个叫test.php的文件,地址是:http://localhost/test.php,一切就绪以后我通过浏览器反复打开这个页面。

阅读全文

phpexcel读写xls文件实现程序

<?php
include_once('PHPExcel.php');
//read excel file;
$PHPExcel = new PHPExcel();    
$PHPReader = new PHPExcel_Reader_Excel5();
$PHPExcel = $PHPReader->load('/home/yuanjianjun/taobao_cat.xls');
$currentSheet = $PHPExcel->getSheet(0);
$allColumn = $currentSheet->getHighestColumn();
$allRow = $currentSheet->getHighestRow();
for($currentRow = 1; $currentRow<=$allRow; $currentRow++){
   for($currentColumn='A'; $currentColumn<=$allColumn; $currentColumn++){  
    $address = $currentColumn.$currentRow;  
    echo $currentSheet->getCell($address)->getValue()."t";  
   }
   echo "n";
}

阅读全文

php中向数组中插入一元素程序代码

<?php
/**
* 逆序二维数组插入一元素
*
* @author WadeYu
* @date 2012-05-30
*/
$aSorted = array(
array(1, 100),
array(2, 90),
array(3, 80),
array(4, 70),
array(5, 60),
array(6, 50),
array(7, 40),
array(8, 40),
array(9, 40),
array(10, 20),
);
$aInsert = array(11, 40);
$maxCmpIdx = 0;
$cnt = 0;
$maxCnt = 10;
foreach ($aSorted as $idx => $arr){
if ($arr[0] == $aInsert[0]){
$maxCmpIdx = $idx;
}
$cnt++;
}
if ( !$maxCmpIdx){
$maxCmpIdx = $cnt++;
}
$aSorted[$maxCmpIdx] = $aInsert;
for ($i = $maxCmpIdx; $i > 0; $i--){
if ($aSorted[$i][1] > $aSorted[$i-1][1]){
$aTmp = $aSorted[$i-1];
$aSorted[$i-1] = $aSorted[$i];
$aSorted[$i] = $aTmp;
continue ;
}
break;
}
for ($i = $cnt; $i > $maxCnt; $i--){
unset($aSorted[$i-1]);
}
print_r($aSorted);

阅读全文