I have a very complicated funciton in one of my CI models so I try to optimise it and make it more robust.I'm not sure where exactly the problem is so I'll paste both the original(working) variant and what I have done to make it better. I think in the most part it should work but obv. I've made a mistake somewhere along the way.
Here is the original function:
public function get($data)
{
if (isset($data))
{
if (isset($data['sort']))
{
$sort = json_decode($data['sort'], true);
$this->db->order_by($sort[0]['property'], $sort[0]['direction']);
}
if (isset($data['query']) && $data['query'] != '')
{
$fields = json_decode($data['fields'], true);
$where = $fields[0] . " LIKE '%" . $data['query'] . "%'";
unset($fields[0]);
foreach ($fields as $field)
{
$where .= ' OR ' . $field . ' LIKE ' . "'%" . $data['query'] . "%'";
}
$this->db->select('id, email, firstname, lastname, usertype, ts_created, ts_last_login, position');
$this->db->from('users');
$this->db->where($where);
$this->db->limit($data['limit'], $data['start']);
$query = $this->db->get();
$result = $query->result_array();
}
else
{
$this->db->select('id, email, firstname, lastname, usertype, ts_created, ts_last_login, position');
$this->db->from('users');
$this->db->limit($data['limit'], $data['start']);
$query = $this->db->get();
$result = $query->result_array();
}
if ($result != null)
{
return $result;
}
else
{
return null;
}
}
else
{
$query = $this->db->select('id, email, firstname, lastname, usertype, ts_created, ts_last_login, position');
$query = $this->db->get('users');
$result = $query->result_array();
return $result;
}
}
And here is what I've done:
public function get($data)
{
if (isset($data))
{
if (isset($data['sort']))
{
$sort = json_decode($data['sort'], true);
$orderCoulmn = $sort[0]['property'];
$orderDir = $sort[0]['direction'];
}
$limit = $data['limit'];
$start = $data['start'];
}
$this->db->select('id, email, firstname, lastname, usertype, ts_created, ts_last_login, position');
/* if (!empty($where))
{
$this->db->where($where);
}*/
if (isset($data['query']) && $data['query'] != '' )
{
$fields = json_decode($data['fields'], true);
//$this->db->like($fields[0], $data['query']);
//unset($fields[0]);
foreach ($fields as $filed)
{
$this->db->or_like($field, $data['query']);
}
}
if (!empty($limit) && !empty($start))
{
$this->db->limit($limit, $start);
}
if (!empty($orderColumn) && !empty($orderDir))
{
$this->db->order_by($orderColumn, $orderDir);
}
$query = $this->db->get('users');
$result = $query->result_array();
return $result;
}
Any ideas where could be the problem in my code (the second one)? Thanks Leron