⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Put extra data from another table in one view



Racing Chocobo
  • profile picture
  • Member

Posted 26 July 2013 - 23:41 PM

Hi I have some question about multiple tables handling in one time.

 

First this is my database structure:

[attachment=625:db1.PNG]

 

some example of data:

[attachment=628:00.PNG] ---------- [attachment=629:33.PNG]            

 

and I hope I can get something like this:

[attachment=627:22.PNG]

 

 

which means I need to join the table item and item_text and show it together

I'm trying to do something like this:

$crud->set_table('items');
$crud->columns('id', 'content1', 'content2', 'content3', 'create_time');

$crud->callback_column('content1',array($this,'getContent1'));
$crud->callback_column('content2',array($this,'getContent2'));
$crud->callback_column('content3',array($this,'getContent3'));


function getContent1($row) {
        $sql = "SELECT t.content FROM item_text t WHERE t.item_id = $row->id AND t.lang_type = 1";
        $result = $this->db->query($sql)->row();
        $title = $result->title;              
        return $title;
}
function getContent2($row) {
        $sql = "SELECT t.content FROM item_text t WHERE t.item_id = $row->id AND t.lang_type = 2";
        $result = $this->db->query($sql)->row();
        $title = $result->title;              
        return $title;
}
function getContent3($row) {
        $sql = "SELECT t.content FROM item_text t WHERE t.item_id = $row->id AND t.lang_type = 3";
        $result = $this->db->query($sql)->row();
        $title = $result->title;              
        return $title;
}

but it seems that the object of "$this" for the 'content1', 'content2', 'content3' will be a null object.

Any suggestion how to do this one?

 

Thank you very much^^

 

 

 


davidoster
  • profile picture
  • Member

Posted 27 July 2013 - 00:16 AM

Possibly because you need to use the $this->load->database(); See here: http://ellislab.com/codeigniter/user-guide/database/examples.html

But I suggest you move these functions to a custome model and call from them and return data back to the callback function.

 

Also the set_relation wouldn't work for you?


Racing Chocobo
  • profile picture
  • Member

Posted 27 July 2013 - 00:52 AM

Hi Davidoster, thank you very much for the reply

 

Sure, I've already load the database before inside the code (I just didn't put it before, sorry)

$this->db = $this->load->database();
$crud = new grocery_CRUD();
$crud->set_subject('Item');
$crud->set_theme('datatables');

$crud->set_table('items');
$crud->columns('id', 'content1', 'content2', 'content3', 'create_time');

$crud->callback_column('content1',array($this,'getContent1'));
$crud->callback_column('content2',array($this,'getContent2'));
$crud->callback_column('content3',array($this,'getContent3'));

But since I set the table into 'Item' table, and inside 'Item' table it doesn't have any 'content1', 'content2' , ...

so '$this' object always null inside my function.

 

I've consider to use the set_relation before, but not pretty sure how to do that since it should have more than one data for each 'item' in 'item_text'

and also in the 'item_text' it doesn't have any 'content1', 'content2', ... but only 'content', so I donno how to to that...

 

I'm pretty curious with your suggestion, 'move the function to the custom model' since I think my current method sounds really stupid...

However I also donno how to do that (your suggestion) so I just use this low method for current, though I also didn't work for me now...


davidoster
  • profile picture
  • Member

Posted 27 July 2013 - 06:01 AM

Check here for a custom model. It's really easy.


Racing Chocobo
  • profile picture
  • Member

Posted 27 July 2013 - 08:03 AM

Hi,

 

Is there any other method instead of using that custom model?

Since if use the custom model, we can't use grocery crud model itself?

even if I create a custom model that extend grocery crud model, I'm not pretty sure how to get my final result correctly.

Maybe some method to put a parameter while using the callback_column function?

 

If I can put the current table of row id to the callback_column, I can take care the rest.

 

Thank you~


davidoster
  • profile picture
  • Member

Posted 27 July 2013 - 10:34 AM

Hi Davidoster, thank you very much for the reply

 

Sure, I've already load the database before inside the code (I just didn't put it before, sorry)

$this->db = $this->load->database();
$crud = new grocery_CRUD();
$crud->set_subject('Item');
$crud->set_theme('datatables');

$crud->set_table('items');
$crud->columns('id', 'content1', 'content2', 'content3', 'create_time');

$crud->callback_column('content1',array($this,'getContent1'));
$crud->callback_column('content2',array($this,'getContent2'));
$crud->callback_column('content3',array($this,'getContent3'));

But since I set the table into 'Item' table, and inside 'Item' table it doesn't have any 'content1', 'content2' , ...

so '$this' object always null inside my function.

 

I've consider to use the set_relation before, but not pretty sure how to do that since it should have more than one data for each 'item' in 'item_text'

and also in the 'item_text' it doesn't have any 'content1', 'content2', ... but only 'content', so I donno how to to that...

 

I'm pretty curious with your suggestion, 'move the function to the custom model' since I think my current method sounds really stupid...

However I also donno how to do that (your suggestion) so I just use this low method for current, though I also didn't work for me now...

 

 

Sorry I didn't notice that. The $this->load->database();

needs to be inside the callback function.


Racing Chocobo
  • profile picture
  • Member

Posted 27 July 2013 - 14:42 PM

Hm I find that we don't need to put the $this->load->database() inside the callback function.

I've solved the problem by this one:

 

getContent1($primary_key , $row){}

 

Sorry I forget to put the $primary_key and $row as the parameter inside the function.

 

Thank you very much!


davidoster
  • profile picture
  • Member

Posted 27 July 2013 - 15:08 PM

Hm I find that we don't need to put the $this->load->database() inside the callback function.

I've solved the problem by this one:

 

getContent1($primary_key , $row){}

 

Sorry I forget to put the $primary_key and $row as the parameter inside the function.

 

Thank you very much!

 

 

Ooook!!! Good to know what was the actual problem!