php codeigniter 数据排序

application


public function move(){

		$up=intval($this->input->get('up',true));
		$dn=intval($this->input->get('dn',true));
		$sort=intval($this->input->get('sort',true));
		if (!empty($up) && is_int($up) && !empty($sort) && is_int($sort)){
			//echo 'up';首先判断是否存在
			$check_where=array('id'=>$up,'sort'=>$sort);
			$check_msg=$this->pub->get_model_forwhere($check_where,'move_test');
			if ($check_msg){
				$get_two_where=array('sort <='=>$sort);
				$get_two=$this->pub->get_model_forwhere($get_two_where,'move_test','sort','DESC',2);
				if ($get_two && count($get_two)==2){
					//print_r($get_two);
					$update_where_one=array('id'=>$up);
					$data_one=array('sort'=>$get_two[1]['sort']);
					
					$update_where_two=array('id'=>$get_two[1]['id']);
					$data_two=array('sort'=>$sort);
					$tip_one=$this->pub->update_model_forwhere($update_where_one,'move_test',$data_one);
					$tip_two=$this->pub->update_model_forwhere($update_where_two,'move_test',$data_two);
					if ($tip_one && $tip_two){
						echo 'up true';
					}else{
						echo 'up false';
					}
				}else{
					echo 'this is url error up';
				}
			}else{
				echo 'error set';
			}
		}elseif (!empty($dn) && is_int($dn) && !empty($sort) && is_int($sort)){
			$check_where=array('id'=>$dn,'sort'=>$sort);
			$check_msg=$this->pub->get_model_forwhere($check_where,'move_test');
			if ($check_msg){
				$get_two_where=array('sort >='=>$sort);
				$get_two=$this->pub->get_model_forwhere($get_two_where,'move_test','sort','ASC',2);
				if ($get_two && count($get_two)==2){
					//print_r($get_two);
					$update_where_one=array('id'=>$dn);
					$data_one=array('sort'=>$get_two[1]['sort']);
					
					$update_where_two=array('id'=>$get_two[1]['id']);
					$data_two=array('sort'=>$sort);
					$tip_one=$this->pub->update_model_forwhere($update_where_one,'move_test',$data_one);
					$tip_two=$this->pub->update_model_forwhere($update_where_two,'move_test',$data_two);
					if ($tip_one && $tip_two){
						echo 'down true';
					}else{
						echo 'down false';
					}
				}else{
					echo 'this is url error up';
				}
			}else{
				echo 'error set';
			}
		}else{
			//echo '参数传递错误';
		}
		
		$this->data['move']=$this->pub->get_model_forwhere('','move_test','sort','ASC');
		$this->data['count']=$this->pub->get_count('','move_test');
		$this->load->view('test_view',$this->data);
	}

model


/*
	 * @综合条件查询语句模型
	 * @where array or string
	 * @table 表名
	 * @order_by_value 要排序的表字段
	 * @order_by_sort desc or asc
	 * @limit 取几条数据 
	 * @offet 隔几条
	 * @code xiaozhu
	 * */
	function get_model_forwhere($where='',$table,$order_by_value='',$order_by_sort='',$limit='',$offet='')
	{
		if (!empty($where))$this->db->where($where);
		$enterprise_num=$this->db->count_all_results($table);
		if ($enterprise_num>0)
		{
			//if (!empty($order_by))$this->db->order_by("title", "desc"); 
			if (!empty($where)){
				if (!empty($order_by_value) && !empty($order_by_sort))$this->db->order_by($order_by_value, $order_by_sort); 
				if (!empty($limit)){
					if (!empty($offet)){
						$query=$this->db->get_where($table,$where,$limit,$offet);
					}else{
						$query=$this->db->get_where($table,$where,$limit);
					}
				}else{
					$query=$this->db->get_where($table,$where);
				}
				
			}else{
				if (!empty($order_by_value) && !empty($order_by_sort))$this->db->order_by($order_by_value, $order_by_sort); 
				if (!empty($limit)){
					//$query=$this->db->get_where($table,$where,$limit);
					//$query=$this->db->get($table,$limit);
					if (!empty($offet)){
						$query=$this->db->get($table,$limit,$offet);
					}else{
						$query=$this->db->get($table,$limit);
					}
				}else{
					$query=$this->db->get($table);
				}
				
			}
			$some_msg=array();
			foreach ($query->result_array() as $rows)
			{
				$some_msg[]=$rows;
			}
			//print_r($some_msg);
			return $some_msg;
		}
		else
		{
		
			return false;
			
		}
	}
	
	/*
	 * @根据条件语句返回符合条件数据条数
	 * @where array类型 也可为字符串类型
	 * @array('id'=>'something')
	 * @string id,'something'
	 * @code xiaozhu
	 * */
	function get_count($where='',$table){
		
		if (!empty($where))$this->db->where($where);
		$count=$this->db->count_all_results($table);
		return $count;
	}
/*
	 * 根据条件语句更新数据模型
	 * code xiaozhu
	 * */
	function update_model_forwhere($where='',$table,$data)
	{
		$this->db->where($where);
		$enterprise_num=$this->db->count_all_results($table);
		$query=$this->db->where($where);
		if ($enterprise_num>0)
		{
			$this->db->update($table,$data);
			return true;
		}
		else
		{
			$this->db->insert($table,$data);
			return false;
		}
	}


view

    <!DOCTYPE html>
    <html>
    <head>
    <title>test move up down</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="<?php echo base_url().'bootstrap/';?>css/bootstrap.min.css" rel="stylesheet" media="screen">
    </head>
    <body>
    <h1>test</h1>

    <ul class="breadcrumb">
    <li><a href="#">首页</a> <span class="divider">/</span></li>
    <li><a href="#">Library</a> <span class="divider">/</span></li>
    <li class="active">Data</li>
    </ul>
	
    
    <ul>
    <?php 
    if (isset($move) && is_array($move)){
    foreach ($move as $rows){
    ?>
    <li>
    <a><?php echo 'id->'.$rows['id'].'  sort->'.$rows['sort'];?></a>
    <?php //if ();?>
       
    <?php if ($rows['sort']>1){?><a href="<?php echo base_url().'test/p_move?up='.$rows['id'].'&sort='.$rows['sort'];?>">up</a>   <?php }?>
    <?php if ($rows['sort']<$count){?><a href="<?php echo base_url().'test/p_move?dn='.$rows['id'].'&sort='.$rows['sort'];?>">down</a><?php }?>
    </li>
    <?php }}?>
    </ul>
    

    <script src="http://code.jquery.com/jquery.js"></script>
    <script src="<?php echo base_url().'bootstrap/';?>js/bootstrap.min.js"></script>
    </body>
    </html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值