Welcome Guest, Not a member yet? Register   Sign In
Object and Arrays questions
#1

[eluser]mikeymayhem[/eluser]
hey, just a quick one, i currently have this code in my controller which is working fine
Code:
function index()
    {
        $gigs = $this->gig->get_gigs();
        $gigs_list = array();
        
        foreach($gigs->result_array() as $key => $gig)
        {
            $gigs_list[$key] = $gig;
            $gigs_list[$key]['comments'] =  $this->gig->get_comments($gig['gigs_id']);
            $gigs_list[$key]['bands'] = $this->gig->get_bands($gig['gigs_id']);
        }
        
        $data['gigs_list'] = $gigs_list;
        $this->load->view('test',$data);
    }
is it possible to do something like this but returning objects?
I tried this already
Code:
function index()
    {
        $gigs = $this->gig->get_gigs();
        $gigs_list = array();
        
        foreach($gigs->result() as $key => $gig)
        {
            $gigs_list[$key] = $gig;
            $gigs_list[$key]['comments'] = $this->gig->get_comments($gig->gigs_id);
            $gigs_list[$key]['bands'] = $this->gig->get_bands($gig->gigs_id);
        }
        
        $data['gigs_list'] = $gigs_list;
        $this->load->view('test',$data);
    }
but i keep getting this error?

Fatal error: Cannot use object of type stdClass as array in

i am a little unclear why, if anyone that could shed some light on this topic it would greatly appreciated! sorry if this topic has been covered already i did search but didnt find anything specific enough to solve my problem. thanks in advance
#2

[eluser]smilie[/eluser]
Could you try:

$test = $gigs->result();

print_r($test);

and see what you are getting back?

Your code should work tho'...

Cheers,
Smilie
#3

[eluser]mikeymayhem[/eluser]
Hey
the function works as long as i use the result_array() func instead of the result() func

heres what i got back from

$test = $gigs->result()

i think the problem lies in trying to ad more result objects to $gigs_list[] array

Code:
array(2) {
  [0]=>
  object(stdClass)#18 (21) {
    ["id"]=>
    string(1) "6"
    ["user_id"]=>
    string(1) "1"
    ["venue_id"]=>
    string(1) "1"
    ["flyer_id"]=>
    string(1) "1"
    ["headline"]=>
    string(1) "1"
    ["content"]=>
    string(443) "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut tortor nibh. Phasellus suscipit vehicula nisi volutpat consectetur. Aliquam eget condimentum magna. Cras non odio quis ligula placerat tempus in vel enim. Integer luctus libero eu nisi rhoncus at adipiscing est ultrices. Integer ipsum urna, suscipit eu accumsan vitae, fringilla a nisl. Duis quam massa, egestas sit amet facilisis eu, tincidunt eget elit. Suspendisse potenti."
    ["date"]=>
    string(10) "2010-12-31"
    ["start_time"]=>
    string(8) "20:00:00"
    ["end_time"]=>
    string(8) "03:00:00"
    ["views"]=>
    string(3) "134"
    ["created"]=>
    string(19) "2010-12-30 16:08:45"
    ["updated"]=>
    string(19) "2010-12-21 16:08:51"
    ["status"]=>
    string(6) "active"
    ["gig_date"]=>
    string(20) "Friday December 2010"
    ["venue_name"]=>
    NULL
    ["flyer_url"]=>
    string(13) "testflyer.jpg"
    ["flyer_name"]=>
    string(4) "test"
    ["user_username"]=>
    string(10) "MikeWaites"
    ["gig_id"]=>
    NULL
    ["comment_id"]=>
    NULL
    ["num_comments"]=>
    string(1) "0"
  }
  [1]=>
  object(stdClass)#19 (21) {
    ["id"]=>
    string(1) "1"
    ["user_id"]=>
    string(1) "1"
    ["venue_id"]=>
    string(1) "1"
    ["flyer_id"]=>
    string(1) "1"
    ["headline"]=>
    string(19) "Testing the quiries"
    ["content"]=>
    string(331) "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In lacinia, ligula at ultrices viverra, lectus nulla fermentum dolor, quis auctor justo turpis in mi. Nulla facilisi. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Morbi nec odio id velit condimentum rutrum id nec sapien. Nullam"
    ["date"]=>
    string(10) "2010-12-19"
    ["start_time"]=>
    string(8) "18:00:00"
    ["end_time"]=>
    string(8) "12:00:00"
    ["views"]=>
    string(1) "1"
    ["created"]=>
    string(19) "2010-12-19 16:10:11"
    ["updated"]=>
    string(19) "2010-12-19 16:10:14"
    ["status"]=>
    string(6) "active"
    ["gig_date"]=>
    string(20) "Sunday December 2010"
    ["venue_name"]=>
    NULL
    ["flyer_url"]=>
    string(13) "testflyer.jpg"
    ["flyer_name"]=>
    string(4) "test"
    ["user_username"]=>
    string(10) "MikeWaites"
    ["gig_id"]=>
    string(1) "1"
    ["comment_id"]=>
    string(1) "1"
    ["num_comments"]=>
    string(1) "2"
  }
}

i think i may have some problems with my query to as its only returning 2 results even though i have 24 in the db. I cant for the life of me figure out why! heres my query.... its raw sql at the moment!
Code:
function get_gigs()
    {
        $sql = "SELECT `gigs`.*, DATE_FORMAT(`date`,'%W %M %Y') AS gig_date, `venues`.`name` AS `venue_name`, `flyers`.`img_url` AS `flyer_url`, `flyers`.`img_name` AS flyer_name,
                `users`.`username` AS `user_username`,    
                `comments_gigs`.`gig_id`, `comments_gigs`.`comment_id`,
                COUNT(comments.id) AS num_comments
                FROM (`gigs`)
                LEFT OUTER JOIN `venues` venues ON `venues`.`id` = `gigs`.`venue_id`
                LEFT OUTER JOIN `flyers` flyers ON `flyers`.id = `gigs`.flyer_id
                LEFT OUTER JOIN `users` users ON `users`.id = `gigs`.user_id
                LEFT OUTER JOIN `comments_gigs` comments_gigs ON `comments_gigs`.gig_id = `gigs`.id
                LEFT JOIN `comments` comments ON `comments`.id = `comments_gigs`.comment_id GROUP BY(gig_id)
                ORDER BY gigs.created DESC";
        
        return $this->db->query($sql);
    }

Thanks alot for your help!
#4

[eluser]smilie[/eluser]
Hm, I think that in case of result() (which returns the stdObject) you have 2 levels, so try:

Code:
function index()
    {
        $gigs = $this->gig->get_gigs();
        $gigs_list = array();
        
        foreach($gigs->result() as $key => $gig)
        {
            foreach($gig as $key2=>$val2)
            {
               $gigs_list[$key2] = $gig;
               $gigs_list[$key2]['comments'] = $this->gig->get_comments($val2->gigs_id);
               $gigs_list[$key2]['bands'] = $this->gig->get_bands($val2->gigs_id);
            }
        }
        
        $data['gigs_list'] = $gigs_list;
        $this->load->view('test',$data);
    }

Regarding your query, if you copy & paste it into PHPMyAdmin - do you get all 24 results back?

Cheers,
Smilie
#5

[eluser]mikeymayhem[/eluser]
No i only get those same two results! its so weird i cant see anything wrong with the query(obv there is but i have tunnel vision from looking at it so much now!)

ill give that a try and get staright back to you!
#6

[eluser]mikeymayhem[/eluser]
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: controllers/home.php

Line Number: 27

Fatal error: Cannot use object of type stdClass as array in /Users/mikewaites/Sites/subvertdestroy/application/controllers/home.php on line 27

Sad unfortunately it didnt work!! hmmm might have to stick with result_array() hopefully ill figure it out one day!

with that query though, if i remove all the joins with comments_gigs and comments i get all the results!!




Theme © iAndrew 2016 - Forum software by © MyBB