I am using the code below in a Code Igniter model to pull data from a database. However my data base has 2 Cust_Phone fields. I would like to set it up so that when the user enters a number phone fields are searched. I think there is a way to do this in the mysql query using sime thing like
select distinct Phone from
(SELECT Cust_Phone1 AS Phone
FROM Customer
UNION
SELECT Cust_Phone2 AS Phone
FROM Customer) as t where Phone <> ''
Inside my other select query, but I am unsure how to go about this or if I am even doing so in the proper way.
--- CodeIgniter Model Function ---
public function customerQuery($First,$Last,$Zip,$Phone) {
$sql = "SELECT * FROM Customer WHERE Cust_First LIKE ? AND Cust_Last LIKE ? AND Cust_Zip LIKE ? AND Cust_Phone1 Like ?";
if($First == null || $First == '') {
$First = '%';
}else{
$First = '%' . $First . '%';
}
if($Last == null || $Last == '') {
$Last = '%';
}else{
$Last = '%' . $Last . '%';
}
if($Zip == null || $Zip == '') {
$Zip = '%';
}else{
$Zip = '%' . $Zip . '%';
}
if($Phone == null || $Phone == '') {
$Phone = '%';
}else{
$Phone = '%' . $Phone . '%';
}
$query = $this->db->query($sql, array($First,$Last,$Zip,$Phone));
return $query->result();
}