简体   繁体   中英

how to update all rows in a column in codeigniter

table company

在此处输入图片说明

Here I had to update all rows in the content column

my model looks:

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

How should I change the array $data and what all modifications should I done, to update all the content from row 1 to row 10.

I am new to this. Thanking in advance

I got a solution and its working, but don't know is it a proper way or not.

changed My model:

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

I would do something like this. Added comments to the code to try and make it clear how it works.

Controller PHP Code:

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

Model PHP Code:

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

try this,

Write this code in your controller's function

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

Write this code in your model

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

your database design is not convenient to store more than one user data so try to create the table following structure .

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

store the all value with table column name as key in array

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

and your update query is like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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