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

时间:2016-07-16 05:46:33

标签: php codeigniter

公司

enter image description here

在这里,我必须更新内容

中的所有行

我的模特看起来:

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行。

我是新手。提前感谢

4 个答案:

答案 0 :(得分:1)

我得到了一个解决方案及其工作方式,但不知道这是否正确。

改变了我的模特:

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;
    }
}

答案 1 :(得分:0)

我会做这样的事情。为代码添加了注释,以尝试明确其工作原理。

<强>控制器 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');
    }
} 

答案 2 :(得分:0)

试试这个,

将此代码写入控制器的功能

$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);
 }

答案 3 :(得分:0)

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

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));

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

中的键

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

,您的更新查询就像这样

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