I have many nested ifs and conditions in my code but it is not giving the desired output. What is the best way to write the following code :
$driver_code = $this->input->post('filter_driver_code');
$unit_code = $this->input->post('filter_unit_code');
$fuel_type = $this->input->post('filter_fuel');
$date_to = $this->input->post('date_to');
$date_from = $this->input->post('date_from');
if (isset($date_from) and isset($date_to) and empty($unit_code) and empty($driver_code) and empty($fuel_type)) {
$sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
$result = $this->db->query($sql);
} elseif (isset($driver_code) and isset($unit_code) and isset($fuel_type) and isset($date_from) and isset($date_to)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
$result = $this->db->query($sql);
} elseif (empty ($date_from) and empty ($date_to) and isset ($unit_code) and isset ($driver_code) and isset ($fuel_type)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
$result = $this->db->query($sql);
} else {
$sql="SELECT * FROM FUEL_USAGE";
$result = $this->db->query($sql);
}
It does not give the right output.
Try all check with empty() may be isset() gives you problem cause on post isset for all variable will return true even values blank
if (!empty($driver_code) && !empty($unit_code) && !empty($fuel_type) && !empty($date_from) && !empty($date_to)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
$result = $this->db->query($sql);
}elseif (!empty($date_from) && !empty($date_to) && empty($unit_code) && empty($driver_code) && empty($fuel_type)) {
$sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
$result = $this->db->query($sql);
} elseif (empty($date_from) && empty($date_to) && !empty($unit_code) && !empty($driver_code) && !empty($fuel_type)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
$result = $this->db->query($sql);
} else {
$sql="SELECT * FROM FUEL_USAGE";
$result = $this->db->query($sql);
}
or try in short way
$sql = "SELECT * FROM fuel_usage where 1 ";
if (!empty($driver_code)) {
$sql .= " AND driver_code='$driver_code' ";
}
if (!empty($unit_code)) {
$sql .= " AND unit_code='$unit_code' ";
}
if (!empty($fuel_type)) {
$sql .= " AND fuel_type='$fuel_type' ";
}
if (!empty($date_from) && !empty($date_to)) {
$sql .= " AND date between '$date_from' and '$date_to' ";
}
$result = $this->db->query($sql);
if (!empty($driver_code) && !empty($unit_code) && !empty($fuel_type) && !empty($date_from) && !empty($date_to)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type' and (date between '$date_from' and '$date_to')";
$result = $this->db->query($sql);
}
elseif(!empty ($unit_code) && !empty ($driver_code) && !empty ($fuel_type)) {
$sql = "SELECT * FROM fuel_usage where driver_code='$driver_code' AND unit_code='$unit_code' AND fuel_type='$fuel_type'";
$result = $this->db->query($sql);
}
elseif (!empty($date_from) and !empty($date_to)) {
$sql = "SELECT * FROM fuel_usage where (date between '$date_from' and '$date_to') ";
$result = $this->db->query($sql);
} else {
$sql="SELECT * FROM FUEL_USAGE";
$result = $this->db->query($sql);
}
Related
How to use three conditions with AND operator in while loop in PHP
Here is my code:
$query = "select * from agent_profile where status = 1";
$run = mysqli_query($con, $query);
$pricequery = "select * from airport_parking where airport_name = '$airport' AND '$parking_start_time' >= start_time ";
$resultprice = mysqli_query($con, $pricequery);
$squery = "select * from agent_services where agent_id = '$crnt_id' AND service_type = '$service_type' ";
$srun = mysqli_query($con, $squery);
if ($run && $resultprice && $squery) {
while (($datarow = mysqli_fetch_array($run)) AND ($getdata = mysqli_fetch_array($resultprice)) AND ($sdata = mysqli_fetch_array($srun))) {
}
Try This
$query = "select * from agent_profile where status=1
UNION
select * from airport_parking where airport_name = '$airport' AND '$parking_start_time' >= start_time
UNION
select * from agent_services where agent_id = '$crnt_id' AND service_type='$service_type';";
$run = mysqli_query($con,$query);
if(strlen($run) <> ''){
while (($datarow = mysqli_fetch_array($run)))
{
$column_name = $datarow['column_name'];
}
}
The below works as long as the two fields are selected. If neither are selected it works, however my issue is when only one of the fields is selected, it doesn't work. It throws the unbound parameters issue.
I've tried setting a false value of 0 to both of the variables, however that won't work because then the query would be select from where = 0.
Ideas?
public static function searchProfile($status, $fundamt)
{
$database = DatabaseFactory::getFactory()->getConnection();
$sql = "SELECT profile_id, profile_name, profile_url, finance_fundingtype, finance_equitypercent, finance_loanrate, finance_loanlength, finance_fundingamount, info_tradingstatus, info_elevatorpitch, info_patentable, info_industry, info_industry1, info_industry2, info_industry3, info_industry4, seeker_logo_url FROM profile_seeker WHERE profile_status = '1' ";
if ($status) {
$sql .= "AND info_tradingstatus IN (:status) ";
}
if ($fundamt) {
$sql .= "AND finance_fundingamount <= :fundamt ";
}
$query = $database->prepare($sql);
$query->execute(array(':status' => $status, ':fundamt' => $fundamt));
$profiles = array();
$profiles[$profile->profile_id] = new stdClass();
$profiles[$profile->profile_id]->profile_id = $profile->profile_id;
$profiles[$profile->profile_id]->profile_name = $profile->profile_name;
$profiles[$profile->profile_id]->profile_url = $profile->profile_url;
$profiles[$profile->profile_id]->finance_fundingtype = $profile->finance_fundingtype;
$profiles[$profile->profile_id]->finance_equitypercent = $profile->finance_equitypercent;
$profiles[$profile->profile_id]->finance_loanrate = $profile->finance_loanrate;
$profiles[$profile->profile_id]->finance_loanlength = $profile->finance_loanlength;
$profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount;
$profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus;
$profiles[$profile->profile_id]->info_elevatorpitch = $profile->info_elevatorpitch;
$profiles[$profile->profile_id]->info_patentable = $profile->info_patentable;
$profiles[$profile->profile_id]->info_industry = $profile->info_industry;
$profiles[$profile->profile_id]->info_industry1 = $profile->info_industry1;
$profiles[$profile->profile_id]->info_industry2 = $profile->info_industry2;
$profiles[$profile->profile_id]->info_industry3 = $profile->info_industry3;
$profiles[$profile->profile_id]->info_industry4 = $profile->info_industry4;
$profiles[$profile->profile_id]->seeker_logo_url = $profile->seeker_logo_url;
}
return $profiles;
You could try to check if the variables are set and that they hold a value that is not 0 and this has a string length longer than 0:
if (isset($status) && $status !== 0 && strlen($status) > 0) {
$sql .= "AND info_tradingstatus IN (:status) ";
}
if (isset($fundamt) && $fundamt!== 0 && strlen($fundamt) > 0) {
$sql .= "AND finance_fundingamount <= :fundamt ";
}
You could also try to bind the parameters manually:
$query = $database->prepare($sql);
if (isset($status) && $status !== 0 && strlen($status) > 0) {
$query ->bindParam(':status',$status);
}
if (isset($fundamt) && $status !== 0 && strlen($fundamt) > 0) {
$query ->bindParam(':fundamt',$fundamt);
}
$query->execute();
Your binding error comes because you forgot to use the same if exists statement on your bindings.
Change this...
if ($status) {
$sql .= "AND info_tradingstatus IN (:status) ";
}
if ($fundamt) {
$sql .= "AND finance_fundingamount <= :fundamt ";
}
and
$profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount;
$profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus;
To this...
if (isset($status) && $status != '') {
$sql .= "AND info_tradingstatus IN (:status) ";
}
if (isste($fundamt) && $fundamt != '') {
$sql .= "AND finance_fundingamount <= :fundamt ";
}
and
if (isset($status) && $status != '') {
$profiles[$profile->profile_id]->finance_fundingamount = $profile->finance_fundingamount;
}
if (isste($fundamt) && $fundamt != '') {
$profiles[$profile->profile_id]->info_tradingstatus = $profile->info_tradingstatus;
}
This will stop the binding if one or both of the text boxes are empty.
I have a search function that lets you input into different type boxes of the last, first and middle names. I don't have any problems with the code, but does anyone know how to optimize it?
The code has multiple if statements that finds out what textbox is unempty and that is then used in the WHERE as you can see below:
$where1 = $_POST['firstname'];
$where2 = $_POST['midname'];
$where3 = $_POST['lastname'];
if(!empty($where1) && empty($where2) && empty($where3)){
$result = $connection->query("SELECT * FROM senior WHERE firstname = '$where1'");
} else if(!empty($where1) && !empty($where2) && empty($where3)){
$result = $connection->query("SELECT * FROM senior WHERE firstname = '$where1' AND midname = '$where2'");
} else if(!empty($where1) && !empty($where2) && !empty($where3)){
$result = $connection->query("SELECT * FROM senior WHERE firstname = '$where1' AND midname = '$where2' AND lastname = '$where3' ");
} else if(!empty($where1) && empty($where2) && !empty($where3)){
$result = $connection->query("SELECT * FROM senior WHERE firstname = '$where1' AND lastname = '$where3' ");
} else if(empty($where1) && !empty($where2) && !empty($where3)){
$result = $connection->query("SELECT * FROM senior WHERE lastname = '$where3' AND midname = '$where2' ");
} else if(empty($where1) && !empty($where2) && empty($where3)){
$result = $connection->query("SELECT * FROM senior WHERE midname = '$where2'");
} else if(empty($where1) && empty($where2) && !empty($where3)){
$result = $connection->query("SELECT * FROM senior WHERE lastname = '$where3'");
}
My suggestion for you
$where = '';
if($where1) $where .= ($where ? " AND " : " ")."firstname = '$where1'";
if($where2) $where .= ($where ? " AND " : " ")."midname = '$where2'";
if($where3) $where .= ($where ? " AND " : " ")."lastname = '$where3'";
$query = "SELECT * FROM senior".($where ? " WHERE ".$where : "");
$result = $connection->query($query);
First of all, I encourage you to use Prepared Statements in order to improve security among other things.
Related with the code you write above you can try something like this:
$query = "SELECT * FROM senior";
$firstcondition = true;
if (!empty($where1))
addConditon($query, "firstname = ".$where1);
if (!empty($where2))
addConditon($query, "midname = ".$where2);
if (!empty($where3))
addConditon($query, "lastname = ".$where3);
$result = $connection->query($query);
function addCondition($query, $condition) {
if (!$firstcondition)
$query.= " AND ";
else {
$firstcondition = false;
$query.= " WHERE ";
}
$query.= $condition;
}
Just a suggestion: why not use LIKE, because it is a search query?
$where1 = $_POST['firstname'];
$where2 = $_POST['midname'];
$where3 = $_POST['lastname'];
$result = $connection->query("SELECT * FROM senior WHERE firstname LIKE '%".$where1."%' AND midname LIKE '%".$where2."%' AND lastname LIKE '%".$where3."%' ");
Here is my code.... and my doubts along with the code
See in the function toltalRetailerComm($userId) ... I am fetching the value of
$shoppeId
and
$storeId
and this same variable can be used in the next function retailerDailyComm($userId, $fromDate, $toDate) by using in this manner function retailerDailyComm($shoppeId,$storeId, $fromDate, $toDate)
function toltalRetailerComm($userId) {
$sql = "SELECT shoppe_id FROM atm_super_shoppe WHERE user_id='$userId'";
$shoppe_query = $this->db->query($sql);
$sql = "SELECT shoppe_id FROM atm_store WHERE user_id='$userId'";
$store_query = $this->db->query($sql);
if ($shoppe_query->num_rows() > 0) {
$result = $shoppe_query->row();
$shoppeId = $result->shoppe_id;
$sql = "SELECT COALESCE(sum(commission),0) as commission FROM atm_shoppe_commission WHERE shoppe_id ='$shoppeId'";
//$sql = "SELECT commission FROM atm_shoppe_commission WHERE shoppe_id='$shoppeId ' BETWEEN '03-5-2011' AND '05-5-2011'";
$query = $this->db->query($sql);
print_r($shoppeId);
if ($query->num_rows > 0) {
$result = $query->row();
$commission = $result->commission;
return $commission;
}
} else
if ($store_query->num_rows() > 0) {
$result = $store_query->row();
$storeId = $result->shoppe_id;
$sql = "SELECT COALESCE(sum(commission),0) as commission FROM atm_store_commission WHERE shoppe_id ='$storeId'";
//$sql = "SELECT commission FROM atm_store_commission WHERE shoppe_id='$storeId ' BETWEEN '03-5-2011' AND '05-5-2011'";
$query = $this->db->query($sql);
print_r($storeId);
if ($query->num_rows > 0) {
$result = $query->row();
$commission = $result->commission;
return $commission;
}
}
}
/**********TOTAL RETAILER COMMISSION ENDS*****************************/
/**********TOTAL RETAILER START TO END DATE COMMISSION STARTS*****************/
function retailerDailyComm($userId, $fromDate, $toDate) {
$sql = "SELECT shoppe_id FROM atm_super_shoppe WHERE user_id='$userId'";
$shoppe_query = $this->db->query($sql);
$sql = "SELECT shoppe_id FROM atm_store WHERE user_id='$userId'";
$store_query = $this->db->query($sql);
if ($shoppe_query->num_rows() > 0) {
$result = $shoppe_query->row();
$shoppeId = $result->shoppe_id;
$sql = "SELECT commission as retailDailyCommission FROM atm_shoppe_commission WHERE shoppe_id='$shoppeId ' BETWEEN '$fromDate' AND '$toDate'";
$query = $this->db->query($sql);
if ($query->num_rows > 0) {
$result = $query->row();
$retailDailyCommission = $result->retailDailyCommission;
return $retailDailyCommission;
} else
if ($store_query->num_rows() > 0) {
$result = $store_query->row();
$storeId = $result->shoppe_id;
$sql = "SELECT commission as retailDailyCommission FROM atm_store_commission WHERE shoppe_id='$storeId' BETWEEN '$fromDate' AND '$toDate'";
$query = $this->db->query($sql);
if ($query->num_rows > 0) {
$result = $query->row();
$retailDailyCommission = $result->retailDailyCommission;
return $retailDailyCommission;
}
}
}
}
You're inside a class, change your variables and it should work.
$this->shoppeId
$this->storeId
what's the error in update command ?? it doesn't update the database ?
if($action=="new")
{
$query1="insert into activity_basic_info(activity_id) values($activity_id)";
$result1=mysql_query($query1);
if($result1)
echo "01";
else
echo "00";
}
else
{
$query="update activity_basic_info set";
if(isset($name))
$query = $query." name='$name',";
if($describtion!="" && describtion!=NULL)
$query = $query." describtion='$describtion',";
if($city!=NULL&&$city!="")
$query = $query." city_id=$city,";
if($region!=NULL)
$query = $query." region_id=$region,";
if($street!=NULL)
$query = $query." street_id=$street,";
if($telephone!="")
$query = $query." telephone=$telephone,";
if($email!=NULL&&$email!="")
$query = $query." email='$email',";
if($url!=NULL&&$url!="")
$query = $query." url='$url'";
else
$query = $query."url=''";
$query = $query." where activity_id=$activity_id";
$result=mysql_query($query);
echo $query;
}
?>
You should check the success of $result = mysql_query($query); then if false, check the value of mysql_error() http://php.net/manual/en/function.mysql-error.php