[eluser]TheFuzzy0ne[/eluser]
I just pulled this out of the active record class and tweaked it slightly:
./application/core/MY_Model.php
Code:
class MY_Model extends CI_Model {
function build_where($conditions, $type = 'OR', $escape = TRUE)
{
$where = array();
foreach ($conditions as $k => $v)
{
echo "$k => $v<br />";
$prefix = (count($where) == 0) ? '' : $type . ' ';
if (is_null($v) && ! $this->db->_has_operator($k))
{
// value appears not to have been set, assign the test to IS NULL
$k .= ' IS NULL';
}
if ( ! is_null($v))
{
if ($escape === TRUE)
{
$k = $this->db->_protect_identifiers($k, FALSE, $escape);
$v = ' '.$this->db->escape($v);
}
if ( ! $this->db->_has_operator($k))
{
$k .= ' = ';
}
}
else
{
$k = $this->db->_protect_identifiers($k, FALSE, $escape);
}
$where[] = $prefix.$k.$v;
}
return implode(' ', $where);
}
}
So from your model method, you'd do something like this:
Code:
$this->db->where('bp_hostel.status', 'A');
$sub_where = array(
'bp_hostel.postal_code LIKE' => '%Vic%',
'bp_region.name LIKE' => '%Vic%',
'bp_hostel.town LIKE' => '%Vic%',
'bp_state.name LIKE' => '%Vic%',
);
$this->db->where('(' . $this->build_where($sub_where) . ')', NULL, FALSE);
$this->db->order_by('view_count', 'desc');
However, I think it would be just as easy to do this instead:
Code:
$this->db->where('bp_hostel.status', 'A');
$sub_where = '`bp_hostel`.`postal_code` LIKE "%Vic%" OR ';
$sub_where .= '`bp_region`.`name` LIKE "%Vic%" OR ';
$sub_where .= '`bp_hostel`.`town` LIKE "%Vic%" OR ';
$sub_where .= '`bp_state`.`name` LIKE "%Vic%"';
$this->db->where($sub_where, NULL, FALSE);
Your mileage may vary with the first method, but the second method is a lot more robust. The Active Record class has been designed to help you with simple queries. If you have a complex query, you'd probably be better off just writing the SQL by hand.
Hope this helps.