使用CodeIgniter更新批处理 [英] Update batch with CodeIgniter

查看:433
本文介绍了使用CodeIgniter更新批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用CodeIgniter做一个小型的开放CMS,我现在正在处理类别系统。



我真的很困惑,很多尝试和论坛帖子我没有解决它。



我有2个mySQL TABLES




  • #1:ft_categories(列出所有包含2个字段的名称:
    category_name和id)

  • #2:ft_upload_data ,category,
    date等)



我想要更新我的#1 TABLE与编辑分类



这是:

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

这是从数据库中检索输入的表单,您可以在其中编辑名称。 p>

现在,当您编辑类别名称时,您会前往'update_categories'CONTROLLER执行UPDATE要求



  function update_categories(){

$ i = 0;
foreach($ this-> input-> post('category_name')as $ cat)
$ data [$ i ++] ['category_name'] = $ cat;
// $ i ++创建了一个多维数组,用于将多个行插入到DB中
//。

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

}

这将获得多次输入字段UPDATE DB。
现在我去MODEL更新数据库中的数据和这里的问题:

  function update_categories $ data){


我不知道如何正确更新名称insert_batch但是具有UPDATE,因为虽然我需要更新表#1,但是我也需要更新表#2中的字段中的名称
2个表上的Double UPDATE和表1中的1个批UPDATE strong>



显然,我试图再添加一个TABLE:TABLE#3,它获得TABLE#1字段ID并与TABLE#2字段ID匹配,但我可以't计算出来做3个表之间的连接。



任何帮助将非常感谢!非常感谢!! (对不起我的英语)






感谢您的回答!



Ok我现在有第三个表现在我想检索'category_name'在'post'中显示这个



我有这个:

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

但它说'on子句'中有未知列'assigned_categories.ft_categories_id'



(assigned_categories是我的第三个TABLE,其中包含相应的信息ID和类别ID)



任何想法?

$尝试使用 UPDATE_BATCH



pre> $ this-> db-> update_batch();



$ data = array(
array(
'title'=>'My title',
'name' >'My Name 2',
'date'=>'My date 2'
),
array(
'title'=&
'name'=>'另一个名称2',
'date'=>'另一个日期2'

);

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

第一个参数将包含表名称,第二个参数是关联数组,第三个参数是where键。



希望此帮助........................

  UPDATE 

//产生:
// UPDATE`mytable` SET`name` = CASE
// WHEN`title` ='我的标题'THEN'我的名字2'
// WHEN`title` ='另一个标题'THEN'另一个名字2'
// ELSE` name` END,
//`date` = CASE
// WHEN`title` ='我的标题'THEN'我的日期2'
// WHEN`title` ='另一个标题'THEN'另一个日期2'
// ELSE`date` END
// WHERE`title` IN('My title','Another title')


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 posts 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')

这篇关于使用CodeIgniter更新批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆