Welcome Guest, Not a member yet? Register   Sign In
How to access data returned from model?
#1

[eluser]pejo[/eluser]
Kind of a newby question and maybe more PHP than codeigniter, but here goes. I'm trying to access a value (pubid) returned by model so I can pass it to another model function, this is all inside my controller. Don't know how to reference that $data['pubdates'] object here? Have no problem in view using ($pubdates) but problem is here in controller not sure how to access value in the pubdates array, getting index not defined?
Code:
// controller function
function search($pd)
{
$this->load->model('Pages_model');
$data['pubdates'] = $this->Pages_model->GetPubdates(array('pubdate' => $pd));
$data['pages'] = $this->Pages_model->GetPages(array('pubid' => $data['pubdates']['pubid']));
$this->load->view('pubs/pubdates',$data);
}
class Pages_model extends Model {

    function __construct()
    {
        parent::__construct();
    }
    function GetPubdates($options = array())
    {
        if(isset($options['pubdate']))
        {
    $this->db->where('pubdate', $options['pubdate']);
    $query = $this->db->get("pubdates");
    return $query->result();
    }
    }
    function GetPages($options = array())
    {
        if(isset($options['pubid']))
        {
    $this->db->where('pubid', $options['pubid']);
    $query = $this->db->get("pages");
    return $query->result();
    }
        
    }
}
BTW this is a master detail scenario, I'm trying to pass pubid from Pubdates to the GetPages function in model.
Thanks.
#2

[eluser]louisl[/eluser]
I think for your usage you should be using return $query; not $query->result();
#3

[eluser]Nick_MyShuitings[/eluser]
If you do a var_dump of $data['pubdates'] you will find that it is most likely an array with various rows (if you only want one row then use ->row() instead of ->result()...

either way, if you use result then you would access it like this: $data['pubdates'][0]->pubid; or in a foreach etc.

If that doesn't make sense just do a var_dump of $data['pubdates'] so you can see what it is you are working with before you try and pass it along.
#4

[eluser]pejo[/eluser]
Thanks. Took your suggestion to do var_dump, this is what I got:
array(1) { ["pubdates"]=> array(0) { } }

So it appears as if the nested array is empty, yet when you pass it through to the View the data is accessible via $pubdates. Don't quite get that. I did try the $data['pubdates'][0]->pubid syntax but that gave property error.

Anyway I think I will look at changing what is returned rather than spend more time on this object.

Thanks for your suggestions.
#5

[eluser]pejo[/eluser]
It finally kicked in what I was dealing with here and kinda saying to myself, Dah! Did a var_dump that returned some data and realized $data is an array of object stdClass. Then it clicked in that I had to manipulate it the same way I did in the View, with a foreach loop. So changed the controller function to use foreach - there would only be 1 record anyway as pubdate is a unique key. Now it works as I wanted.
Code:
function search($pd)
{
  $pubid = 0;
  $this->load->model('Pages_model');
  $data['pubdates'] = $this->Pages_model->GetPubdates(array('pubdate' => $pd));
  foreach($data['pubdates'] as $pubdate)
  {
    $pubid = $pubdate->pubid;
  }
  $data['pages'] = $this->Pages_model->GetPages(array('pubid' => $pubid));
  $this->load->view('pubs/pubdates',$data);
}




Theme © iAndrew 2016 - Forum software by © MyBB