Correct way of implementing if else loop or else if and can i write function inside a loop
function toltalRetailerComm($userId) {
//print_r ($shoppeId);
$sql = "SELECT shoppe_id FROM atm_super_shoppe WHERE user_id='$userId'";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$result = $query->row();
$shoppeId = $result->shoppe_id;
$sql = "SELECT COALESCE(sum(commission),0) as commission FROM atm_shoppe_commission WHERE shoppe_id ='$shoppeId'";
$query = $this->db->query($sql);
print_r($shoppeId);
if ($query->num_rows > 0) {
$result = $query->row();
$commission = $result->commission;
return $commission;
} else {
Blockquote block2
$sql = "SELECT shoppe_id FROM atm_store WHERE user_id='$userId'";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$result = $query->row();
$storeId = $result->shoppe_id;
$sql = "SELECT COALESCE(sum(commission),0) as commission FROM atm_store_commission WHERE shoppe_id ='$storeId'";
$query = $this->db->query($sql);
print_r($storeId);
if ($query->num_rows >开发者_StackOverflow社区; 0) {
$result = $query->row();
$commission = $result->commission;
return $commission;
}
}
}
}
}
I'm new to PHP, can anyone tell me the correct way of using an if/else
or elseif
in the code shown above? Its not able to go to the second block. Also and can I write a function inside an if/else
block?
Are you looking for something like this:
function toltalRetailerComm($userId) {
//print_r ($shoppeId);
$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'";
$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'";
$query = $this->db->query($sql);
print_r($storeId);
if ($query->num_rows > 0) {
$result = $query->row();
$commission = $result->commission;
return $commission;
}
}
}
Each of your if
s checks if there are any results from the query. If there are - the if
section is executed. If not - the else
section is executed (and if there is no else
, nothing is executed).
As to your question about functions - you can't create a function inside an if/else
block, but you can call a function from it. You create a function:
function total ($a, $b){
return $a+$b;
}
and the you can call it from the if
block:
if ($a==$b){
$c = total ($a,$b);
}
EDIT: As to your question about not getting to the second block. Before the second block there is an if
as follows:
$sql = "SELECT COALESCE(sum(commission),0) as commission FROM atm_shoppe_commission WHERE shoppe_id ='$shoppeId'";
$query = $this->db->query($sql);
print_r($shoppeId);
if ($query->num_rows > 0)
The if
checks if any rows were returned from the sql query that is supposed to return the first nonnull expression that follows the condition shoppe_id ='$shoppeId'
. Only if no rows are returned, it gets to the second block, which is the else
of this condition.
精彩评论