views:

26

answers:

2

I am trying to display results from a database where the results are contained in three tables. How do I echo out the results?

$p-> works, but $img-> or $branch-> doesn't. What am I doing wrong?

Example code is below

Sample controller:

$p_id = $this->uri->segment(3);

$this->load->model('One_model');
$data['prop'] = $this->One_model->get_details($p_id);
$data['img'] = $this->One->get_images($p_id);

$this->load->model('Two_model');
$data['branch'] = $this->Two_model->get_details($p_id);

$this->load->view('a_test_view', $data);

A Sample View

<?php foreach ($property as $p):?>
<p><?php echo $p->SUMMARY; ?></p>
<p>We have <?php echo "$img->num_photos"; ?> photos</p>
<p>Branch is <?php echo $branch->name; ?>. Telephone <?php echo $branch->tel; ?></p>
<ul>
<li><?php echo $p->FEATURE1; ?></li>
<li><?php echo $p->FEATURE2; ?></li>
<li><?php echo $p->FEATURE3; ?></li>
<li><?php echo $p->FEATURE4; ?></li>
<li><?php echo $p->FEATURE5; ?></li>
<li><?php echo $p->FEATURE6; ?></li>
<li><?php echo $p->FEATURE7; ?></li>
<li><?php echo $p->FEATURE8; ?></li>
<li><?php echo $p->FEATURE9; ?></li>
<li><?php echo $p->FEATURE10; ?></li>
</ul>
<?php endforeach; ?>

A: 

You know you can edit your origonal question :)

A join would work well in this situation too.

$this->db->from('property');
$this->db->join('branch', 'branch.id = property.pid');
$this->db->where('property.pid ==', $p_id);

If you need help with the particular query you can edit your question and add that in and give some more details of what your models are doing.

I am sure you can do this all in one function in the model.

Kieran Andrews
Cheers Kieran, I thought my original question was badly worded but couldnt find a way to edit it :)Thanks for your help, I will give the above a try
Craig Ward
A: 

If $branch and $img are both (i'm assuming) arrays themselves and you want to access the branch telephone number you'd need to do $branch[0]->tel to access the first element in the array.

Similarly if you want to count how many images there are you'll need to do count($img)

WeeJames