Welcome Guest, Not a member yet? Register   Sign In
Passing Arrays between Models
#1

[eluser]Fielder[/eluser]
My model runs this query
Code:
// ** MODEL **
function getBusinessbyName($request)
{
  $this->db->where('bus_name',$request);
  $results = $this->db->get('bus');
  if ($results->num_rows() > 0)
  {
      return $results->result_array();
  }
}

and it will return any number of rows.

However, within each of the rows returned, I only need the bus_id field from each row. So back in my controller I'm running
Code:
// ** CONTROLLER **
$results = $this->business->getBusinessbyName($bus_name);

foreach ($results as $row)
{
     $bus_id[] = $row['bus_id'];
}

$query = $this->contract->getAllContractsbyBus($bus_id);

Then I want to pass that new $bus_id[] array into another model function called getAllContractsbyBus()
Code:
// ** MODEL **
function getAllContractsbyBus($bus_id)
{
        $this->db->select('*');
        $this->db->from('contract');
        $this->db->join('bus','bus.bus_id = contract.bus_id','left');
        $this->db->join('employees','employees.employee_rep = contract.employee_rep','left');
        $this->db->where('bus_id',$row[]);
        foreach ($bus_id as $row)
        {
            $this->db->where('bus_id',$row['bus_id']);
        }
        $results = $this->db->get();
        if ($results->num_rows() > 0)
        {
            return $results->result_array();
        }
}

but I don't think it's running the right way. Am I on the right track?
In fact, I want the WHERE clause to be OR instead of AND.
#2

[eluser]gtech[/eluser]
check out the
Code:
$this->db->where_in();

function in the [url="http://ellislab.com/codeigniter/user-guide/database/active_record.html#select"]user guide..[/url] It allows you to select using an array, so you can abolish your loop.
Code:
$this->db->where_in('bus.bus_id', $bus_id);

it should simplitfy your process..

you can also check the array comming into the model by

Code:
print_r($bus_id);

this will print the array to the browser.. if your happy it is correct you can remove the print_r

hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB