CodeIgniter Forums

Full Version: Outputting data problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]skribe[/eluser]
Hi folks:

I'm adapting this CRUD example and I'm having some difficulties trying to determine how to output my data. Basically what it should do is output the contact details of a person which are held in three tables

Person:
id
name

Contact_Type:
id
type

Person_contact (join):
person_id
contact_type_id
details

Contact type allows me to add different ways to contact people (email, phone, european swallows). Details holds the actual address.

These are the additions. They're producing a Message: Undefined property: CI_DB_mysql_result error

Controller:
Code:
function view($id){

        // get person details
        $data['contact'] = $this->person_model->get_person_contact($id);
        // load view
        $this->load->view('personview', $data);
    }

Model:
Code:
function get_person_contact($id){
        $this->db->select('ct.type, pc.details');
        $this->db->from('person_contact pc');
        $this->db->where('pc.person_id', '$id');
        $this->db->join('contact_type ct', 'pc.contact_id = ct.id');
        $data['query']= $this->db->get();
        return $data;
}

View:
Code:
<?php foreach ($contact as $contact){ ?>
                <tr>
                    <td valign="top">&lt;?php echo "$contact->contact_type.type" ?&gt;</td>
                    <td>&lt;?php echo $contact->person_contact.details ?&gt;</td>
                </tr>
            &lt;?php } ?&gt;

Any suggestions?

El Forum

[eluser]mi6crazyheart[/eluser]
Change u'r model to this...
Code:
function get_person_contact($id){
        $this->db->select('ct.type, pc.details');
        $this->db->from('person_contact pc');
        $this->db->where('pc.person_id', '$id');
        $this->db->join('contact_type ct', 'pc.contact_id = ct.id');
        $query = $this->db->get();
        return $query->result();
}

VIEW should be
Code:
foreach($contact as $item):
echo $item->ct.type ;
echo $item->pc.details;
endforeach;

El Forum

[eluser]skribe[/eluser]
Thanks for the help. Unfortunately it's not returning any data. Even the foreach is failing to produce the html rows so $contact must be empty.

El Forum

[eluser]mi6crazyheart[/eluser]
Just before foreach() in VIEW add these line to check weather u'r getting any data from Db or not..
Code:
echo(count($contact)); // it'll show no. of records u'r getting from DB
echo '<br>';
print_r($contact); // It'll show u all data which of those records

foreach($contact as $item):
echo $item->ct.type ;
echo $item->pc.details;
endforeach;

El Forum

[eluser]skribe[/eluser]
It's definitely empty.
It returns:
0
Array ( )

El Forum

[eluser]mi6crazyheart[/eluser]
Then i think there might've some problem in sql query. May be query is not able to match with any records...

El Forum

[eluser]skribe[/eluser]
[quote author="mi6crazyheart" date="1293533880"]Then i think there might've some problem in sql query. May be query is not able to match with any records...[/quote]

I considered that. This query runs fine in mysql:

Code:
SELECT ct.type, pc.details FROM person_contact pc LEFT JOIN contact_type ct on pc.contact_id = ct.id WHERE pc.person_id = '1';

I substitute 1 with $id in php.

I've substituted the active records query with this and end up with the same no result.

Thanks for the help so far btw.

El Forum

[eluser]cideveloper[/eluser]
Take out the quotes on $id in the where clause.
you have '$id' which means you are looking for '$id' in your database not the passed value

enabling profiling helps a lot when you need to debug stuff

Code:
function get_person_contact($id){
        $this->db->select('ct.type, pc.details');
        $this->db->from('person_contact pc');
        $this->db->where('pc.person_id', $id);
        $this->db->join('contact_type ct', 'pc.contact_id = ct.id');
        $query = $this->db->get();
        return $query->result();
}

El Forum

[eluser]skribe[/eluser]
Thanks guys. Removing the quotes fixed it.

El Forum

[eluser]InsiteFX[/eluser]
Try this, you did not add the left at the end of your join!

Code:
$this->db->join('contact_type ct', 'pc.contact_id = ct.id', 'left');

InsiteFX