• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unusual ActiveRecord Result

#1
[eluser]E303[/eluser]
I am trying to get the exhibitionId from exhibitions depending on what exhibition is viewed. Then with that exhibitionsId I want to search the gallery table to see if their is a gallery.
Code:
$url = $this->uri->segment(3);
            $this->db->select('exhibitionsId');
            $this->db->from('exhibitions');
            $this->db->where('url',$url);
            $exhibitions_list = $this->db->get();
            foreach ($exhibitions_list->row() as $row)
            {
                $this->db->select('url, title');
                $this->db->from('galleries');
                $this->db->where('exhibitId', $row['exhibitionsId']);
                $galleryExhibitions_data = $this->db->get();
                return $galleryExhibitions_data->result_array();
            }

Nothing displays like there is no record yet there is. So when I run
Code:
echo $row['exhibitionsId'];
I get the number 2. The correct number should be 25. There is no record with the number 2 so I am not sure what is happening. It seems when I try to echo out anything from the first query I get the number 2 and the result.

Thanks in advance

#2
[eluser]xwero[/eluser]
Try this
Code:
$this->db->select('t2.url, t2.title');
$this->db->from('exhibitions t1, galleries t2');
$this->db->where('t1.url',$this->uri->segment(3));
$this->db->where('t2.exhibitId = t1.exhibitionsId'); // silent join
$query = $this->db->get();
return $query->row();
It's a single sql statement with a silent join which outputs an object with the properties url and title.

If you are using the row method it outputs a one dimensional object so the foreach doesn't work. If you use it, it should be like this
Code:
$url = $this->uri->segment(3);

            $this->db->select('exhibitionsId');
            $this->db->from('exhibitions');
            $this->db->where('url',$url);
            $exhibitions_list = $this->db->get();
            $row = $exhibitions_list->row();

            $this->db->select('url, title');
                $this->db->from('galleries');
                $this->db->where('exhibitId', $row->exhibitionsId);
                $galleryExhibitions_data = $this->db->get();
                return $galleryExhibitions_data->result_array();

#3
[eluser]E303[/eluser]
Thanks that worked.

A quick question. you mentioned that it wouldn't work with a foreach yet this code does work.
Code:
$url = $this->uri->segment(3);
            $this->db->select('artistId');
            $this->db->from('exhibitions');
            $this->db->where('url', $url);
            $artist_list = $this->db->get();
            
            foreach ($artist_list->row() as $row)
            {
                $this->db->select('firstName, lastName, url');
                $this->db->from('artists');
                $this->db->where('artistId', $row['artistId']);
                $artistExhibition_data = $this->db->get();
                return $artistExhibition_data->result_array();
            }

#4
[eluser]xwero[/eluser]
Maybe by coincidence the output of $row['artistId'] matches the right artistId but this will not be true in every case.

Also putting a return in a foreach statement only gets you one row, because return exits the function it's in, which makes the loop unnecessary.

#5
[eluser]E303[/eluser]
makes sense.

Thanks for your help.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.