2

I am developing my first big application using codeigniter and need a little help as I am fairly new to it all. I know how to get records out of the DB and display them, but I now have to get results from two tables and display them. I pass $data to the view, it works fine for $data['prop'] but I can't get the rest.

$data['prop'] = $this->ManageProperty_model->get_property_details($p_id);
$data['img'] = $this->ManageProperty_model->get_property_images($p_id);
$this->load->model('ManageBranch_model');
$data['branch'] = $this->ManageBranch_model->get_branch_details($p_id);

I usually echo out results like so:
<?php foreach ($prop as $p) : ?>
<?php echo $p->ID; ?>
<?php endforeach; ?>
Even if there is only 1 row being returned as I am not sure of another way.do I need a foreach for img and branch?

2 Answers 2

2

How about using a join?

I dont know whats in your model but if you try something like

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

You can put a where statement in there too if you need something particular.

This means you have a more complex model but less loops and arrays and messy stuff in the view which is what you want.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, currently you're just getting one or the other. You're probably better to put it into a multilevel array like $data['property']['prop'].

2 Comments

How would you display this? and do I always have to have a foreach loop. In this case I know there will only be one result returned. Seems daft to have a loop in there
First off, you never "know" that only one result will be returned unless you put a LIMIT 0,1 on it. Second, why are you using a foreach loop then? Shouldn't your ManageProperty_model take care of that and simply return the object so you can say $prop->id, $img->image_link, etc?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.