CodeIgniter Forums

Full Version: How to reference the value of a result in a second query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone, I am new to PHP and codeigniter...

I have two tables

one stores the name of different id and organization name

second table contains people information with id for the organization they belong to

I want to translate the name of the organization for each person from the organization table matching it on the organization id

I know how to write the query however, I am unable to figure out how to reference the organization id as part of a result set i.e.

$org_ID = $row->org_ID;
$query = $this->db->get_where('organization', array('orgID' => $org_ID); <<<<<< this does not work and I do not know how
foreach ($query->result() as $r) <<<<< reference the $row->org_ID in the query
{?>
<td><?php echo $r->orgName;?></td> <<<<<<<< this is what I ultimately want to name of the organization and not it's
<?php } ?> <<<<<<<< record identifier.

Can anyone show me how to reference the data in the result set in a second query?

I would be so thankful...
You can use one to many relationship, so the table must look like this below,

Code:
Organization
-------------------------------------
org_id | person_id | more columsn here ....      
-------------------------------------
        
People
-------------------------------------
person_id | more columsn here ....      
-------------------------------------


The Query
Code:
function get_record_by_id($person_id)
    {
            $q = $this->db->select('org_id, person_id, organization')
                             ->from('organization')
                             ->where('person_id', $person_id)
                             ->limit(1)
                             ->get();

            if($q->num_rows() > 0) {
                    foreach ($q->result() as $row) {
                            //$data[] = $row;
                            $data = $row->organization;
                    }

                    return $data;
            }
    }


Not tested buy try...
(12-12-2015, 04:04 PM)solidcodes Wrote: [ -> ]You can use one to many relationship, so the table must look like this below,

Code:
       
Organization
-------------------------------------
org_id | person_id | more columsn here ....      
-------------------------------------
       
People
-------------------------------------
person_id | more columsn here ....      
-------------------------------------  
       
       
The Query
Code:
       
   function get_record_by_id($person_id)
   {
           $q = $this->db->select('org_id, person_id, organization')
                            ->from('organization')
                            ->where('person_id', $person_id)
                            ->limit(1)
                            ->get();

           if($q->num_rows() > 0) {
                   foreach ($q->result() as $row) {
                           //$data[] = $row;
                           $data = $row->organization;
                   }

                   return $data;
           }
   }
       
       
Not tested buy try...

thank you very much for your help.. sorry it took so long to get back here but life happens.. again thanks much