1、有时辰,在模板里面写变量挺麻烦的,为了快速点儿,写个函数好了。。
<form action= method=post>
<label>变量:</label><input name=value type=text /><br/>
<label>数组:</label><textarea name=codes cols=70 rows=6></textarea><br/>
<input type=submit value=生成 />
</form>
<?php
¥code = ¥_POST[codes];
¥value = ¥.¥_POST[value];
function tplval(¥str, ¥value){
¥regex = /(\[.\])(.)/;
preg_match_all(¥regex, ¥str, ¥matches);
foreach(¥matches[1] as ¥key=>¥val){
¥new .= {.¥value.¥val.}.¥matches[2][¥key].<br/>;
}
echo ¥new;
}
tplval(¥code, ¥value);
?>
2、当你所用的 cms 不带有快捷强大的查询办法时,不妨本身写一个生成sql语句的函数:
¥table[goods] = array(g,); //表别号,以及查的字段
¥table[order] = array(o);//同上
¥table[member] = array(m,id,name,address);
//¥table[member] = id,name,address; 表不取别号
¥where[o.id] = ¥_GET[id];
¥where[o.addTime] = array(between,¥time1,¥time2, and); //array(like,%abc); array(in,¥arr);
¥where[o.price] = array(>,¥price,or);
¥join[left] = array(o.id=g.oid,m.id=o.mid); //默示三张表,依次应用 join left;
//或都只有一个left join 第二个是 right jion时 : ¥join[right] = array(m.id=o.mid);
¥other[order] = array(desc=>o.addTime);
¥otehr[limit] = 5,10;
// 生成查询语句
function sqlSelect(¥where = array(),¥table = array(), ¥join = array(), ¥other = array()){
if(!empty(¥table) && is_array(¥table)){¥sql = SELECT ; }
foreach(¥table as ¥tname => ¥val){
if(is_array(¥val)){
if( == ¥val[1] || empty(¥val[1])){
¥fields .= `¥val[0]`.,;
}else{
¥arr = explode(,,¥val[1]);
¥fields .= `¥val[0]`.`.implode(`,`¥val[0]`.`, ¥arr).`,;
}
¥tableName = `¥tname` AS `¥val[0]`;
}else{
if( == ¥val){
¥fields .= `¥tname`.,;
}else{
¥arr = explode(,, ¥val);
¥fields .= `¥tname`.`.implode(`,`¥tname`.`, ¥arr).`,;
}
¥tableName = `¥tname`;
}
¥tables.= ¥tableName,;
¥tableArr[] = ¥tableName;
}
¥tables = substr(¥tables, 0, -1);
¥fields = substr(¥fields, 0, -1);
if(empty(¥join) && !empty(¥table)){
¥sql .= ¥fields FROM ¥tables;
}elseif(!empty(¥join) && is_array(¥join)){
¥sql .= ¥fields FROM ¥tableArr[0];
¥i = 1;
foreach(¥join as ¥key => ¥sec){
¥key = strtoupper(¥key);
if(is_array(¥sec)){
foreach(¥sec as ¥val){
¥sql .= ¥key JOIN ¥tableArr[¥i] ON ¥val ;
¥i++;
}
}else{
¥sql .= ¥key JOIN ¥tableArr[¥i] ON ¥sec ;
¥i++;
}
}
}
if(is_array(¥where) && !empty(¥where)){
foreach(¥where as ¥field => ¥sec){
¥arr = explode(.,¥field); //多表查询时,带表名的景象
if(count(¥arr)==2){
¥field = `¥arr[0]`.`¥arr[1]`;
}else{
¥field = `¥arr[0]`;
}
if(is_array(¥sec)){
if(isset(¥sec[2])) ¥link = end(¥sec);
else ¥link = and;
¥link = strtoupper(¥link);
¥keyWord = strtoupper(¥sec[0]);
if(BETWEEN == ¥keyWord || NOT BETWEEN == ¥keyWord){
¥wheres .= ¥field ¥keyWord ¥sec[1] AND ¥sec[2] ¥link ;
}elseif(IN == ¥keyWord || NOT IN == ¥keyWord){
if(is_array(¥sec[1])){
¥sec[1] = implode(,, ¥sec[1]);
}
¥wheres .= ¥field ¥keyWord(¥sec[1]) ¥link ;
}else{
¥wheres .= ¥field ¥keyWord ¥sec[1] ¥link ;
}
}else{
¥wheres .= ¥field=¥sec AND ;
}
}
¥sql .= empty(¥table)? substr(¥wheres, 0, -4) : WHERE .substr(¥wheres, 0, -4);
}
if(is_array(¥other) && !empty(¥other)){
if(!empty(¥other[order])){
if(is_array(¥other[order])){
foreach(¥other[order] as ¥key => ¥val){
¥key = strtoupper(¥key);
¥sql .= ORDER BY ¥val ¥key ;
}
}else{
¥sql .= ORDER BY .¥other[order];
}
}
!empty(¥other[limit])? ¥sql .= LIMIT . ¥other[limit] : ;
}
return ¥sql;
}