繁体   English   中英

如何在codeigniter中更新列中的所有行

[英]how to update all rows in a column in codeigniter

餐桌公司

在此处输入图片说明

在这里,我必须更新内容列中的所有行

我的模型看起来:

public function updateCompanyAction()
{
    $name = $this->input->POST('name');
    $email = $this->input->POST('email');
    $phone = $this->input->POST('phone');
    $facebook = $this->input->POST('facebook');
    $instagram = $this->input->POST('instagram');
    $google_plus = $this->input->POST('google_plus');
    $twitter = $this->input->POST('twitter');
    $pinterest = $this->input->POST('pinterest');
    $latitude = $this->input->POST('latitude');
    $longitude = $this->input->POST('longitude');



    $data = array(

    'content'=>$name,


    );
    $result=$this->db->update('company', $data);
    if($result)
    {
        return true;
    }
    else
    {
        return false;
    }
}

我应该如何更改数组$ data以及我应该做些什么修改,以将所有内容从第1行更新到第10行。

我是新来的。 预先感谢

我有一个解决方案及其有效的方法,但不知道这是否是正确的方法。

更改了我的模型:

public function updateCompanyAction()
{
    $name = $this->input->POST('name');
    $email = $this->input->POST('email');
    $phone = $this->input->POST('phone');
    $facebook = $this->input->POST('facebook');
    $instagram = $this->input->POST('instagram');
    $google_plus = $this->input->POST('google_plus');
    $twitter = $this->input->POST('twitter');
    $pinterest = $this->input->POST('pinterest');
    $latitude = $this->input->POST('latitude');
    $longitude = $this->input->POST('longitude');


        $data = array(

            array(
                'id' => 1 ,
                'content' => $name
                ),
            array(
                'id' => 2 ,
                'content' => $email
                ),
            array(
                'id' => 3 ,
                'content' => $phone
                ),
            array(
                'id' => 4 ,
                'content' => $facebook
                ),
            array(
                'id' => 5 ,
                'content' => $instagram
                ),
            array(
                'id' => 6 ,
                'content' => $google_plus
                ),
            array(
                'id' => 7 ,
                'content' => $twitter
                ),
            array(
                'id' => 8 ,
                'content' => $pinterest
                ),
            array(
                'id' => 9 ,
                'content' => $latitude
                ),
            array(
                'id' => 10 ,
                'content' => $longitude
                )     
        );


    $result=$this->db->update_batch('company', $data, 'id'); 
    if($result)
    {
        return true;
    }
    else
    {
        return false;
    }
}

我会做这样的事情。 在代码中添加了注释,以尝试使其变得清晰明了。

控制器PHP代码:

function ghjk()
{
    $this->load->helper('text');
    $this->load->model("model_get");

    // Get title and id form database
    // Assuming that $results includes row ID and title
    $results = $this->model_get->getalltitles();

    // Set $data array but leave it empty for now.
    $data = array();

    // Get key and value
    foreach ($results as $key => $value)
    {
        // Clean the slug
        $cleanslug = convert_accented_characters($value['site_title']);

        // Add cleaned title and id to new data array.
        // By using the key value, we create a new array for each row
        // in the already existsing data array that we created earlier.
        // This also gives us the correct formatted array for update_batch()
        // function later.

        $data[$key]['slug']    = url_title($cleanslug,'-',TRUE);
        $data[$key]['site_id'] = $value['site_id'];
    }

    // Print out array for debug
    print_r($data);

    // Update database
    $this->model_get->updateall($data);
} 

模型PHP代码:

function updateall($data = array())
{
    // Check so incoming data is actually an array and not empty
    if (is_array($data) && ! empty($data))
    {
        // We already have a correctly formatted array from the controller,
        // so no need to do anything else here, just update.

        // Update rows in database
        $this->db->update_batch('newsites', $data, 'site_id');
    }
} 

尝试这个,

在控制器的功能中编写此代码

$updateStatus = array();
foreach($_POST as $key=>$value)
{
  // change yourModelName with your model name
  $updateStatus[$key] = $this->yourModelName->updateCompanyAction($key,$value);
}

//check the status of each value
var_dump($updateStatus);

在您的模型中编写此代码

public function updateCompanyAction($key,$value)
{
  $data = array();
  $data['content'] = $value;
  $this->db->where('name',$key);
  return $this->db->update('company',$data);
 }

您的数据库设计不方便存储多个用户数据,因此请尝试按照以下结构创建表。

create table company(id int not null auto_increment primary key ,name 
varchar(150),email varchar(150),phone varchar(150),facebook   
varchar(150),instagram varchar(150),google_plus varchar(150),twitter 
varchar(150),pinterest varchar(150),latitude decimal(18,15),longitude 
decimal(18,15));

将所有具有表列名称的值存储为数组中的键

例如: $data=array('name'=>'anto' ,'email '=>'xx@gmail.com'...........);

您的更新查询是这样的

 $this->db->where('id','some_id');  //here is where condition
 $this->db->update('company',$data);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM