Outputting data problem

#1
[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?

#2
[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;

#3
[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.

#4
[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;

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

#6
[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...

#7
[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.

#8
[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();
}

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

#10
[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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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