0

I'm trying to get data from one database with a condition where id equals to current logged in tenant (tenant_id),

$this->data['props'] = $this->property_m->get_by( array ('id' => $this->session->userdata['tanant_id'] ));

and then get some values from a field and save in an array,

if(count($this->data['props']) > 0){
    $my_array = array();
    foreach ($props as $prop){
        $my_array['id'] = $prop->tenant_id;
    }
}

First problem here - $my_array consists only 1 value. ( I need it to be multiple ) How can I do that ?
Second problem - I need to select data from another table which would look for data fulfilling the condition in that array, as,

$this->data['reports'] = $this->report_m->get_by($my_array);

which would say,

SELECT * FROM  reports WHERE ('id = 1'); // or 2 or 3 (value from prop->tenant)

but I need it to be,

SELECT * FROM reports WHERE ('id = 1 OR id = 2 OR id = 3'); 
2
  • 1
    1. Don't overwrite key id every iteration. 2. Where id IN () Commented Dec 14, 2013 at 16:46
  • Please tell me how not to overwrite key id Commented Dec 14, 2013 at 16:53

1 Answer 1

1

Doing:

$my_array = array();
foreach ($props as $prop){
    $my_array['id'] = $prop->tenant_id;
}

You're just overwriting id key of $my_array. Use this:

$my_array = array();
foreach ($props as $prop){
    $my_array[] = $prop->tenant_id;
}
// print_r($my_array); 

Use where field in () condition:

SELECT * FROM reports WHERE id IN (1, 2, 3);

Assuming you have:

$my_array = array(1,2,3);
// query string:
$q_str = 'SELECT * FROM reports WHERE id IN (' . implode(',', $my_array) . ')';
Sign up to request clarification or add additional context in comments.

Comments

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.