// sortArr,对无限分类的数组递归排序
protected function sortCat($arr,$cat_id = 0,$lev = 0) {
static $tree = array();
foreach($arr as $k=>$v) {
if($v['parent_id'] == $cat_id) {
$v['lev'] = $lev;
$tree[] = $v;
$this->sortCat($arr,$v['cat_id'],$lev + 1);
}
}
return $tree;
}
// 增加自动拼接sql语句的操作
public function autoExecute($table,$data) {
$sql = '';
if(empty($table) || empty($data)) {
return false;
}
$fields = implode(',',array_keys($data));
$values = implode("','",array_values($data));
$sql .= 'insert into '.$table.'( '.$fields.') values (\'';
$sql .= $values.'\')';
return $this->query($sql);
}
//读取级联目录
function rec_dir($path,$lev = 0) {
if(is_dir($path)) {
$dh = opendir($path);
while(($d = readdir($dh))!==false) {
if($d=='.'||$d=='..') { continue; }
echo " ",str_repeat(" ",$lev),$d,"<br />";
if(is_dir($path.'/'.$d)) {
rec_dir($path.'/'.$d,$lev+1);
}}}}
//路径初始化
define('ROOT_PATH',str_replace('\\','/',str_replace('init.php','',__FILE__)));
//yii framework将Excel读入mysql
<h1>About Excel</h1>
<form name="frmBatchSettle" action="test/ex" method="post" enctype="multipart/form-data">
<input type="file" name="batchFile" value="">
<input type="submit" value="上传">
</form>
//test/ex.php
<?php
if(isset($_FILES['batchFile']) && $_FILES['batchFile']['error']==0)
{
spl_autoload_unregister(array('YiiBase','autoload')); //注销YII的自动加载,PHPexcel有自己的加载,根据具体代码环境
include_once('PHPExcel.php'); //按照自己的路径导入类文件
include_once('PHPExcel/IOFactory.php');
$objPHPExcel = PHPExcel_IOFactory::load($_FILES['batchFile']['tmp_name']); //导入上传的excel文件的数据逻辑
$objWorksheet = $objPHPExcel->getActiveSheet(0); //获取excel中sheet(0)的数据
spl_autoload_register(array('YiiBase','autoload')); //重新启用YII的自动载入
$EXCEL_VALUE = array();
foreach ($objWorksheet->getRowIterator() as $row) //遍历数据
{
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $cell)
{
$EXCEL_VALUE[] = $cell->getValue();
} }
$data = array();
$NumKey = 0;
for($i=0;$i<count($EXCEL_VALUE)/2;$i++)
{
for($j=0;$j<2;$j++)
{
$data[$i][] = $EXCEL_VALUE[$NumKey];
$NumKey++;
}}
$successNum = 0 ;
for($i=0;$i<count($data);$i++){
$intUserId = Yii::app()->user->id;
$intfileId = intval($data[$i][0]);
$strContent = strval($data[$i][1]);
if($aa=bizFilecomments::addComment($intUserId, $intfileId, $strContent)){
$successNum++;
}
}
echo "成功读入",$successNum,"条记录"; }
?>
//preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$Val)中文正则