Hi all,
I have a quick question about refactoring php code. Below are three functions. The first two appear quite similar, and only differ with one if statement. The third combines the first two through use of a flag. Is this the best practice? Here it seems okay to use a flag, but what if we need to add more flags in the future? What is the best practice?
Thanks.
function check_contact_email($email)
{
$this->db->select('COUNT(login) AS count');
$this->db->from('users');
$this->db->where('email', $email);
$query = $this->db->get();
$row = $query->row();
return ($row->count > 0);
}
function check_contact_email_id($email)
{
$this->db->select('COUNT(login) AS count');
$this->db->from('users');
$this->db->where('email', $email);
$this->db->where('user_id !=', $_POST['user_id']);
$query = $this->db->get();
$row = $query->row();
return ($row->count > 0);
}
function check_contact_email($email, $id = FALSE)
{
$this->db->select('COUNT(login) AS count');
$this->db->from('users');
$this->db->where('email', $email);
if ($id) $this->db->where('user_id !=', $_POST['user_id']);
$query = $this->db->get();
$row = $query->row();
return ($row->count > 0);
}