开发者

Update batch with CodeIgniter

I'm trying to do a small open CMS with CodeIgniter and I'm now working on the categories system.

I'm really stuck with that and after many tries and forum post I didn't solve it.

I have 2 mySQL TABLES

  • #1 : ft_categories (list all categorie's names with 2 fields : category_name and id)
  • #2 : ft_upload_data (list all po开发者_如何转开发sts with fields like id, title, category, date and so on)

I want to UPDATE my #1 TABLE with the datas in an edit categorie's names form (this form is filled with inputs in a loop to edit multiple categories at once)

Here it is :

if ($result != NULL) {
echo form_open('admin/update_categories/');
if (isset($result) && $result != NULL) {

    foreach ($result as $row) {
    echo form_input('category_name[]' ,$row->category_name);
    echo anchor("admin/delete_category/$row->category_name", 'Delete category');
    echo '<br /><br />';
    }

    echo '<br /><br />';
    echo form_submit('','Save');
    echo form_close();

} } else { echo 'NO categories'; }

This is the form with the inputs retrieved from the DB where you can edit the name.

Ok now when you edit the categorie's names you go to the 'update_categories' CONTROLLER to do the UPDATE request

    function update_categories(){

    $i = 0;
    foreach ($this->input->post('category_name') as $cat)
        $data[$i++]['category_name'] = $cat;
        // The $i++ creates a multi-dimensional array to insert
        // multiple rows into the DB.

    $this->admin_model->update_categories($data);

}

This will get the multiples inputs fields to UPDATE the DB. And now I go to the MODEL to UPDATE the data in the DB and HERE'S THE PROBLEM :

    function update_categories($data) {

    ?

I don't know what can I do to update the names correctly with something like an insert_batch but with UPDATE because although I need to UPDATE the TABLE #1, I also NEED to update the name in the field in the TABLE #2 Double UPDATE on 2 Tables and 1 batch UPDATE in the TABLE #1

Obviously I tried to add one more TABLE : TABLE #3 which get the TABLE#1 field id and match it with the TABLE#2 field id but I can't figure it out to do connection between the 3 tables.

Any help would be very very appreciated! Many thanks!! (sorry for my bad english)


Thanks for answer!

Ok I have this third table now I would like to retrieve the 'category_name' to show this within the 'post'

I have this :

    $this->db->order_by('rank', 'asc');

$this->db->select('*');
$this->db->from('ft_upload_data');
$this->db->join('ft_categories', 'assigned_categories.ft_categories_id = assigned_categories.ft_upload_data_id');

$query = $this->db->get();



return $query->result();

But it says there is Unknown column 'assigned_categories.ft_categories_id' in 'on clause'

(assigned_categories is my third TABLE with the post id and the category id matched)

Any idea?


try to use UPDATE_BATCH

$this->db->update_batch();



$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name 2' ,
      'date' => 'My date 2'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name 2' ,
      'date' => 'Another date 2'
   )
);

$this->db->update_batch('mytable', $data, 'title'); 

The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.

hope this help........................

UPDATE 

// Produces: 
// UPDATE `mytable` SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name` END,
// `date` = CASE 
// WHEN `title` = 'My title' THEN 'My date 2'
// WHEN `title` = 'Another title' THEN 'Another date 2'
// ELSE `date` END
// WHERE `title` IN ('My title','Another title')


First you cannot update multiple rows in one query, so there's nothing like insert_batch() for updates - you have to do it in a loop for each row. Next you should use 3rd table to link post id with category id in a one to many relation using foreign keys (InnoDB, worse performance) OR at least use category_id field in TABLE#2 that links to the category id, so you don't need to update this table in case of changes in TABLE#1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜