【问题标题】:Automatically filling in sql column data after referencing a substring of another column引用另一列的子串后自动填充sql列数据
【发布时间】:2021-11-27 13:05:44
【问题描述】:

目前我正在使用 Mysql 和 CodeIgniters MVC 框架来填写我的数据。该表记录了已进行的每个状态更改以及更改时间。这是数据库当前的样子:

我在名为 status_from 和 status_to 的表中添加了新列,我希望这些列从 action 列中获取子字符串。

但是现在我如何使用以下代码在我的数据库中显示它:

控制器类:

public function status($status){ 
        $statusar = array('D'=>'Draft','N'=>'Unpublish','Y'=>'Publish','U'=>'Action','L'=>'Unlisted','S'=>'Sold','T'=>'Let');
        if($this->input->post('id')){
            foreach($this->input->post('id') as $key => $id):
            $check = $this->listings_model->loadlisting_check($id);
            $log = "Listing website status changed from ". $statusar[$check->status]." to ".$statusar[$status].". The listing ID is #".$id.".";
            $this->logs_model->insert_log(array('refno'=>$check->refno,'action'=>trim($log)));
            $data=array('status'=>$status);  
            if($status == 'T' || $status == 'Y' || $status == 'S'){
                $pub = 1;
            }else{
                $pub =0;
            }
            $this->listings_model->lpupdate(array('property_publish'=>$pub),$id);

            endforeach; 
        } 
        return true;
    }

listings_model:

function loadlisting_check($id)
    {
        $this->db->select("refno, status, archive");
        $id=$this->db->escape_str($id);
        $cond=array("$this->table_name.$this->primary_key"=>$id);
        $this->db->where($cond); 
        $this->db->from($this->table_name);
        $query = $this->db->get();
        return $query->row();
     } 

logs_model:

public function insert_log($log)
    {
        $log['agent_id'] = $this->session->userdata('clientsessuserid');
        $log['logtime'] = date('Y-m-d H:i:s');
        $this->db->insert($this->table_name, $log);
        return true;
    }

基本上我想在创建每个新条目时用 $statusar[$check->status] 填充我的 status_from 列,并用 $statusar[$status] 填充 status_to 列

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    您可以在 insert_log 方法参数中使用与表中的列名匹配的正确键来传递要添加到数组中的变量

     public function status($status){ 
                $statusar = array('D'=>'Draft','N'=>'Unpublish','Y'=>'Publish','U'=>'Action','L'=>'Unlisted','S'=>'Sold','T'=>'Let');
                if($this->input->post('id')){
                    foreach($this->input->post('id') as $key => $id):
                    $check = $this->listings_model->loadlisting_check($id);
                    $log = "Listing website status changed from ". $statusar[$check->status]." to ".$statusar[$status].". The listing ID is #".$id.".";
                    $this->logs_model->insert_log(array('refno'=>$check->refno,'action'=>trim($log) , 'status_from'=>$statusar[$check->status] ,'status_to'=>$statusar[$status]));
                    $data=array('status'=>$status);  
                    if($status == 'T' || $status == 'Y' || $status == 'S'){
                        $pub = 1;
                    }else{
                        $pub =0;
                    }
                    $this->listings_model->lpupdate(array('property_publish'=>$pub),$id);
        
                    endforeach; 
                } 
                return true;
            }
    

    而且您不需要在模型中添加任何其他内容

    【讨论】:

    • $pub = (int)in_array($status, ['T', 'Y', 'S']);
    猜你喜欢
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多