我想在页面上显示几个数据 用户可以点多选框加入比较 最后根据判断 在查询语句里 where条件 and拼接
每选中一个就加一个and
select * from xxx where id =1 and id=2 and.............
这个怎么拼接
THINKPHP的
每选中一个就加一个and
select * from xxx where id =1 and id=2 and.............
这个怎么拼接
THINKPHP的
然后选一个条件就$sql.=' and id=1'
$sql.=' and id=2'
$sql = "where 1=1";
如
if($id){
$sql .= ' and id='.$id;
}
if($type){
$sql .= ' and type='.$type;
}
.......//以此类推
先不说id是不是唯一性(暂且不论)
id=1 and id=2 and id=3 你确定id能同时等1、2、3吗?
或者你是想说id等于1或者等于2或者等3
id=1 or id=2 or id=3 或者写为id in (1,2,3)
* 指定查询条件 支持任意多个where 连贯操作 by chenrenqi
* @access public
* @param mixed $where 条件表达式
* @param mixed $parse 预处理参数
* @return Model
*/
public function where ( $where , $parse = null )
{
if (! is_null($parse) && is_string($where))
{
if (! is_array($parse))
{
$parse = func_get_args();
array_shift($parse);
}
$parse = array_map(array($this->db,'escapeString'), $parse);
$where = vsprintf($where, $parse);
} elseif (is_object($where))
{
$where = get_object_vars($where);
}
if (is_string($where) && '' != $where)
{
$map = array();
$oldWhere=$this->options['where']['_string'] ;
if($oldWhere)
{
$where=$oldWhere.' and '.$where; //此处代码增强,让字符串条件可以多个
}
$map['_string'] = $where;
$where = $map;
}
if (isset($this->options['where']))
{
$this->options['where'] = array_merge($this->options['where'], $where);
} else
{
$this->options['where'] = $where;
}
return $this;
}---------------
正式发布的产品中,我们还有对此完全兼容Model原来的where版本。
$this->MyModel->where('xx',22);
$this->MyModel->where('yy',44);.
用户每选择一个条件,你就增加一个where就可以了。
用ThinkPHP编程,还在写SQL语句?哪说明你连ThinkPHP的5%功能都没有用到。用THinkPHP,你可以完全告别Model和SQL语句。
还在写SQL语句的同学。水平还在很菜啊。我给你一小段代码:你需要的功能就可以如下实现,完全不需要任何SQL:
$this->_model->table(' xxx_table_name')->where('id',1)->where('id2',22);我给你在段完整的model代码:只需要在你的action中实例化成_model。就行可以轻易实现你要的功能了。/**
* 通用的Model类。可以进行基本的增删查改动作。
*/
class CommonModel extends Model
{
// 模型名称
protected $name='';
protected $_actionTableName; public function __construct($name='',$tablePrefix='',$connection='')
{
parent::__construct($name,$tablePrefix,$connection);
//var_dump($this->fields); $this->_actionTableName=$name;
}
public function whereID($idValue)
{ $id=$this->getPk();
return $this->where("$id='$idValue'");
}
public function where2($field,$value,$operator='=')
{
return $this->where("$field $operator '$value'");
}
public function where_and($field,$value,$operator='=')
{
return $this->where("$field $operator '$value'");
}
/**
* 指定查询条件 支持多个where ,同时支持 字段名,字段值功能,同时还支持model原生where功能
* @access public
* @param mixed $field字段名或者 条件表达式,或者数组
* @param mixed $value,为字段值或者$parse 预处理参数
* @return Model
*/
public function where($field,$value,$operator='=')
{
if(!$field)
return false;
$param=func_get_args();
$param_len=func_num_args();
switch ($param_len)
{
case 0:
return false;
case 1:
return $this->where_more($field);
case 2:
default:
if(is_array($field))
return call_user_func_array($this->where_more,$param);//数组是调用原生的
if(is_string($field) && strpos($field,'%')!==false)
return call_user_func_array($this->where_more, $param);//调用原生的
if(is_string($field) && $param_len>1)
{
return $this->where2($field, $value,$operator);
}else
{
echo 'system error where';
}
}
}
/**
* 原php中table方法有问题,在重新设置表时没有重建整个表信息,此方法弥补此错误。
* @param string $tableName 为空是切换到action主表
* @return Model对象
*/
public function table($tableName='')
{
if(!$tableName)
$tableName=$this->_actionTableName;
if(!$tableName)
return $this;//还没有表
$this->trueTableName=$tableName;
$this->flush();
return $this;
}
/**
* 指定查询条件 支持任意多个where 连贯操作
* @access public
* @param mixed $where 条件表达式
* @param mixed $parse 预处理参数
* @return Model
*/
public function where_more( $where , $parse = null )
{
if (! is_null($parse) && is_string($where))
{
if (! is_array($parse))
{
$parse = func_get_args();
array_shift($parse);
}
$parse = array_map(array($this->db,'escapeString'), $parse);
$where = vsprintf($where, $parse);
} elseif (is_object($where))
{
$where = get_object_vars($where);
}
if (is_string($where) && '' != $where)
{
$map = array();
$oldWhere=@$this->options['where']['_string'] ;
if($oldWhere)
{
$where=$oldWhere.' and '.$where; //此处代码增强,让字符串条件可以多个
}
$map['_string'] = $where;
$where = $map;
}
if (isset($this->options['where']))
{
$this->options['where'] = array_merge($this->options['where'], $where);
} else
{
$this->options['where'] = $where;
}
return $this;
}
/**
* 左连接
* @param string $link_table_name 连接的表名
* @param string $main_field_name 要连接的主表名中字段名
* @param string $link_field_name 为要建立连接中表的字段名
* @return 返回自己对象.
*/
public function joinOn($link_table_name,$main_field_name,$link_field_name='')
{
if($link_field_name=='')
{
$link_field_name=$main_field_name;
}
if(!strpos($link_field_name, '.')) //自带表名了 在自连接时,必须要自带表名才可以确定是要连接的哪个字段
{
$link_field_name="$link_table_name.$link_field_name";
}
if(!strpos($main_field_name, '.')) //自带表名了 在自连接时,必须要自带表名才可以确定是要连接的哪个字段
{
$main_field_name="$this->trueTableName.$main_field_name";
}
return $this->join(" $link_table_name on $main_field_name=$link_field_name");
}
/**
* 自连接
* @param string $link_field_name 要自连接的表名
* @param string $newFieldName 连接后新字段名(用于从结果中取出新字段)
* @return 将原表的所有字段加新字段,返回。
*/
public function joinOwner($link_field_name,$newFieldName)
{
$tableAlias='cc857'; //随机的字符串做表另名
$table_name=$this->trueTableName;
$this->field("$table_name.*,$tableAlias.dept_name as $newFieldName");
return $this->joinOn("$table_name $tableAlias",$link_field_name,"$tableAlias.{$this->getPk()}");
}
/**
* 将原表中的user_id字段($mainFieldName指定)变为nick_name字段对应输出.
* @param string $mainFieldName 主表的要连接的字段名
* @param string $leftTableName user表名
* @param string $linkFieldName user表名中user_id字段名
* @return Ambigous <Model, CommonModel>
*/
public function joinUserTable($mainFieldName='user_id',$linkTableName='user',$linkFieldName='user_id',$nick_name_field='nick_name')
{
$this->field("{$this->trueTableName}.*,{$linkTableName}.{$nick_name_field} as {$nick_name_field}");
return $this->joinOn($linkTableName,$mainFieldName,$linkFieldName);
} /**
* 删除数据,如果有 is_deleted字段,则会设置此字段为1,不会真正删除此条记录。
* 如果无此字段,则还是用传统删除方法
* is_deleted字段 名字在 配置文件conf/config.php 中定义 DELETE_FIELD_NAME
* @access public
* @param mixed $options 表达式
* @return mixed
*/
public function delete($options=array()) {
$deleted_field=C('DELETE_FIELD_NAME');//标记删除字段
if($deleted_field && in_array($deleted_field, $this->fields,true)) //分析参数,设置成where条件。
{
if(empty($options) && empty($this->options['where'])) {
// 如果删除条件为空 则删除当前【数据对象data】所对应的记录
if(!empty($this->data) && isset($this->data[$this->getPk()]))
return $this->delete($this->data[$this->getPk()]);//根据主键删除记录
else
return false;
}
if(is_numeric($options) || is_string($options)) {
// 根据主键删除记录
$pk = $this->getPk();
if(strpos($options,',')) {
// $where[$pk] = array('IN', $options);
$this->where2($pk,$options,'IN');
}else{
// $where[$pk] = $options;
// $pkValue = $options;
$this->where2($pk,$options);
}
// $options = array();
// $options['where'] = $where;
// $this->where($where);
}
return $this->setField($deleted_field,'1');
}else
{
return parent::delete($options);
}
}
/**
* 查询数据集,会自动 处理删除标记字段
* @access public
* @param array $options 表达式参数
* @return mixed
*/
final public function select($options=array()) {
$deleted_field=C('DELETE_FIELD_NAME');//标记删除字段
if($deleted_field && in_array($deleted_field, $this->fields,true)) //分析参数,设置成where条件。
{
$this->where2($deleted_field,0);
}
return parent::select($options);
}
/**
*
* @param unknown $str
*/
function where_or($str)
{
//test
$a=M('a');
$data2['a']=1;
$data2['b']=2;
$data2['c']=3;
$data2['_logic'] = 'OR';
$data['_complex'] =$data2; $data['d']=4;
$a->where($data)->select();
dump($a);
}
/** 指定查询字段 支持字段排除
* @access public
* @param mixed $field 支持数组和字符串
* @param boolean $except 是否排除
* @return Model */
public function field( $field , $except = false )
{
if (true === $field)
{ // 获取全部字段
$fields = $this->getDbFields();
$field = $fields ? $fields : '*';
} elseif ($except)
{ // 字段排除
if (is_string($field))
{
$field = explode(',', $field);
}
$fields = $this->getDbFields();
$field = $fields ? array_diff($fields, $field) : $field;
}
if (is_string($field))
{
$field = explode(',', $field);
}
$old_field=$this->options['field'];
if (is_string($old_field))
{
$old_field = explode(',', $old_field);
}
if($this->options['field'])
{
$this->options['field'] =implode(',', array_merge_value($old_field,$field)); //此行让field 具备多次field功能,并去掉重复字符串
}
else
{
$this->options['field'] =$field;
}
return $this;
}
}-----------
本类还已经实现了比较复杂的左连接,自连接功能。都是一行搞定非常复杂的 SQL语句,且完全屏蔽了写SQL。
写sql多不安全啊。
首先你的checkbox的命名应该是name="id[]"这种,这样post传过来的$_POST['id']就可以直接使用
初始化一个数组$where = array();
$where = $_POST['id']
最后用$map['id']=>array('in', $where);
具体业务再自己处理