Jump to content


Put extra data from another table in one view

join callback_column

  • Please log in to reply
7 replies to this topic

#1 Racing Chocobo

Racing Chocobo

    Member

  • Members
  • PipPip
  • 28 posts

Posted 26 July 2013 - 11:41 PM

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

 

First this is my database structure:

db1.PNG

 

some example of data:

00.PNG ---------- 33.PNG             

 

and I hope I can get something like this:

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^^

 

 

 



#2 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 27 July 2013 - 12:16 AM

Possibly because you need to use the $this->load->database(); See here: http://ellislab.com/...e/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?


Edited by davidoster, 27 July 2013 - 12:18 AM.
update

____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#3 Racing Chocobo

Racing Chocobo

    Member

  • Members
  • PipPip
  • 28 posts

Posted 27 July 2013 - 12: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...



#4 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 27 July 2013 - 06:01 AM

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


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#5 Racing Chocobo

Racing Chocobo

    Member

  • Members
  • PipPip
  • 28 posts

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~



#6 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

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.


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#7 Racing Chocobo

Racing Chocobo

    Member

  • Members
  • PipPip
  • 28 posts

Posted 27 July 2013 - 02: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!



#8 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 27 July 2013 - 03: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!


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr






Also tagged with one or more of these keywords: join, callback_column

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users