Welcome Guest, Not a member yet? Register   Sign In
How to reference the value of a result in a second query
#1

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...
Reply
#2

(This post was last modified: 12-12-2015, 04:15 PM by solidcodes.)

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...
No SEO spam
Reply
#3

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB