I'm having some trouble where my last else statement never runs even though I know the first if will eventually return false, any ideas?
if ($productType == 'HSweat' || 'T Shirt') {
if ($productType == 'HSweat') {
if (!$queryResult) {
}
} elseif ($productType == 'T Shirt') {
if (!$queryResult) {
}
}
} else {
}
if ($productType == 'HSweat' || 'T Shirt') {
Should be
if ($productType == 'HSweat' || $productType == 'T Shirt') {
'T Shirt' as a string will always evaluate to True
I would recommend to use switch :
switch( $productType ){
case 'HSweat':
if (!$queryResult) {
//Do something
}
break;
case 'T Shirt':
if (!$queryResult) {
//Do something
}
break;
default:
//Do something
break;
}
Related
I'm trying to repeat php "if" 20 times with dh_c_01_01. How I can do it?
if ($dh_c_01_01['id'] == $a_c_a_max) {
$dh_sq_01_01 = $a_b_s_05;
} elseif ($dh_c_01_01['id'] == $a_c_a_1_b) {
$dh_sq_01_01 = $a_b_s_06;
} elseif ($dh_c_01_01['id'] == $a_c_a_2_b) {
$dh_sq_01_01 = $a_b_s_07;
} elseif ($dh_c_01_01['id'] == $a_c_a_3_b) {
$dh_sq_01_01 = $a_b_s_08;
} elseif ($dh_c_01_01['id'] == $a_c_a_4_b) {
$dh_sq_01_01 = $a_b_s_09;
} elseif ($dh_c_01_01['id'] == $a_c_a_5_b) {
$dh_sq_01_01 = $a_b_s_10;
} elseif ($dh_c_01_01['id'] == $a_c_a_6_b) {
$dh_sq_01_01 = $a_b_s_11;
} elseif ($dh_c_01_01['id'] == $a_c_a_7_b) {
$dh_sq_01_01 = $a_b_s_12;
} elseif ($dh_c_01_01['id'] == $a_c_a_8_b) {
$dh_sq_01_01 = $a_b_s_13;
} elseif ($dh_c_01_01['id'] == $a_c_a_9_b) {
$dh_sq_01_01 = $a_b_s_14;
}
if ($dh_c_02_01['id'] == $a_c_a_max) {
$dh_sq_02_01 = $a_b_s_05;
} elseif ($dh_c_02_01['id'] == $a_c_a_1_b) {
$dh_sq_02_01 = $a_b_s_06;
} elseif ($dh_c_02_01['id'] == $a_c_a_2_b) {
$dh_sq_02_01 = $a_b_s_07;
} elseif ($dh_c_02_01['id'] == $a_c_a_3_b) {
$dh_sq_02_01 = $a_b_s_08;
} elseif ($dh_c_02_01['id'] == $a_c_a_4_b) {
$dh_sq_02_01 = $a_b_s_09;
} elseif ($dh_c_02_01['id'] == $a_c_a_5_b) {
$dh_sq_02_01 = $a_b_s_10;
} elseif ($dh_c_02_01['id'] == $a_c_a_6_b) {
$dh_sq_02_01 = $a_b_s_11;
} elseif ($dh_c_02_01['id'] == $a_c_a_7_b) {
$dh_sq_02_01 = $a_b_s_12;
} elseif ($dh_c_02_01['id'] == $a_c_a_8_b) {
$dh_sq_02_01 = $a_b_s_13;
} elseif ($dh_c_02_01['id'] == $a_c_a_9_b) {
$dh_sq_02_01 = $a_b_s_14;
}
[$dh_sq_03_01, ... , $dh_sq_20_01]
up to 20 times
I cannot be sure I understand your question correctly, or assume I know what your variables are or what their values are.
Based on your question, this example uses extra variables, for loops and dynamic variables. It also assumes that each of your dynamic variables exists:
<?php
$dh_c_limit = 20; // <-- loop limit
$a_c_a_limit = 9; // <-- loop limit
$a_b_s_addition = 5; // <-- based on your question
$a_c_a_max = 0; // <-- change this to whatever '$a_c_a_max' should be!
for ($dh_i = 1; $dh_i <= $dh_c_limit; $dh_i++) {
if(${"dh_c_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"}['id'] == $a_c_a_max){ // <-- checks against '$a_c_a_max'
${"dh_sq_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"} = ${"a_b_s_".str_pad($a_c_i,2,"0",STR_PAD_LEFT)}; // <-- set the '$dh_sq_' variable to '$a_b_s_' variable
}
else{
for ($a_c_i = 1; $a_c_i <= $a_c_a_limit; $a_c_i++) {
if(${"dh_c_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"}['id'] == ${"a_c_a_".$a_c_i."_b"}){ // <-- checks against '$a_c_a_' variable
${"dh_sq_".str_pad($dh_i,2,"0",STR_PAD_LEFT)."_01"} = ${"a_b_s_".str_pad(($a_c_i+$a_b_s_addition),2,"0",STR_PAD_LEFT)}; // <-- set the '$dh_sq_' variable to '$a_b_s_' variable
break; // <-- break the loop
}
}
}
}
?>
That is a bad habit. Try to use switch case, or use array as container
$array = [
'id1' => 'action1',
'id2' => 'action2',
...
];
$dh_sq_02_01 = $array[$dh_c_02_01['id']];
Just and example. Hope helps
PHP
$name = karlaxis;
if ($name == "") {
return false;
} elseif ($name != karlaxis) {
return false;
} else {
return true;
}
jQuery
$.post("#name-form").attr("action").serializearray(), function(result){
if (result == false) {
console.log("result of the IF CONDITION");
} elseif (result == false) { //the result of ELSEIF in php
console.log("result of the ELSEIF CONDITION");
} else {
console.log("result of the ELSE CONDITION");
}
})
Now, how to get the result of the elseif?
How to code that one?
If it is very important to get the result of different conditions, you can do a simple trick with your code. Rather than returning bool you may return int values like 0,1,2 where 0 is for IF, 1 is for ELSE IF, 2 is for ELSE.
php:
$name = karlaxis;
if($name == ""){
return 0;
}elseif($name != karlaxis){
return 1;
}else{
return 2;
}
jquery:
$.post("#name-form").attr("action").serializearray(), function(result){
IF(result == 0){
console.log("result of the IF CONDITION");
}ELSE IF(result == 1){ //the result of ELSEIF in php
console.log("result of the ELSEIF CONDITION");
}ELSE{
console.log("result of the ELSE CONDITION");
}
})
I use this code in home function it get the usertype and works nice.
function home()
{
$type = $this->session->userdata('type');
if($type == "admin")
{
$this->load->view('index');
}
else if($type == "QA" || "SC" || "CDC")
{
$this->load->view('aindex');
}
}
But when I use same code in jobsheet function it's not working. Code below
function jobsheet()
{
$type = $this->session->userdata('type');
$this->load->model('Ipss_model');
$var1['job'] = $this->Ipss_model->AllJobSheet();
$var2['division'] = $this->Ipss_model->AllDivision();
$var3['tItem'] = $this->Ipss_model->transactionItem();
$data = $var1 + $var2 + $var3;
if($data['job'] != NULL )
{
if($type == "SC" || "CDC")
{
$this->load->view('jobsheet',$data);
}
else if($type == "admin" || "QA" )
{
$this->load->view('jobsheetQA',$data);
}
}
else
{
$this->load->view('jobsheetEmpty',$data);
}
}
Please help me about this. Thanks in advance.
To check weather array is empty or not you can use empty(). Also change your || condition like below.
So you condition would be
if(!empty($data))
{
if($type == "SC" || $type =="CDC")
{
$this->load->view('jobsheet',$data);
}
else if($type == "admin" || $type == "QA" )
{
$this->load->view('jobsheetQA',$data);
}
}
check your array() before using it in if condition
with -:
echo "<pre>";
print_r($data);
die();
I have a Categories name 'Trending' and 'Top' I want to display all posts on both of these Categories pages. I don't want to write custom query I want to edit "global $wp_query;" object and remove "cat" from the query to display all posts.
How it possible?
My Code snap:
global $wp_query;
if (isset($wp_query->query_vars[$monstrotheme->settings->slugs->frontendPosting->addNew])) {
$this->data['specialPage'] = 'frontend-submit';
$this->data['currentContentLayout'] = 'page';
} else if ($wp_query->query_vars['pagename'] == $monstrotheme->settings->slugs->login->login) {
$this->data['currentContentLayout'] = 'frontPage';
} else {
if (is_front_page() || is_404()) {
$this->data['currentContentLayout'] = 'frontPage';
} else if (is_post_type_archive('gallery') || is_tax('gallery-category') || is_tax('gallery-tag')) {
$this->data['currentContentLayout'] = 'galleryArchive';
} else if (is_post_type_archive('video') || is_tax('video-category') || is_tax('video-tag')) {
$this->data['currentContentLayout'] = 'videoArchive';
} else if (is_archive() || is_search() || (is_home() && !is_front_page())) {
$this->data['currentContentLayout'] = 'postArchive';
if (is_category('Trending')) {
// Here I want to alter the query
}
} else if (is_page()) {
$this->data['currentContentLayout'] = 'page';
} else if (is_single()) {
switch (get_post_type()) {
case 'video':
$this->data['currentContentLayout'] = 'video';
break;
case 'gallery':
$this->data['currentContentLayout'] = 'gallery';
break;
default:
$this->data['currentContentLayout'] = 'post';
}
} else {
exit;
}
}
im wanting to check whether a inputted triangle is either a isosceles, equal etc.. the first if statement works, but once i introduce the second one it says unexpected t_else, here is the code...
public function typeOfTriangle()
{
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
}
any ideas?
Maybe this is even better? ( Based on the example from Vlad Preda:
public function getSides()
{
return array($this->sideOneLength, $this->sideTwoLength, $this->sideThreeLength);
}
public function typeOfTriangle()
{
$uniqueSides = count(array_unique($this->getSides()));
switch ($uniqueSides) {
case 1:
return "Equilateral";
break;
case 2:
return "Isosceles";
break;
default:
return "Normal triangle";
break;
}
}
You have a syntax error:
public function typeOfTriangle()
{
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
}else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
}
Need a brace }
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
}
else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
I would suggest a slightly different, more readable approach:
public function getSides()
{
return array($this->sideOneLength, $this->sideTwoLength, $this->sideThreeLength);
}
public function typeOfTriangle()
{
$uniqueSides = count(array_unique($this->getSides()));
if ($uniqueSides == 1) {
return "Equilateral";
} elseif ($uniqueSides == 2) {
return "Isosceles";
} else {
return "Normal triangle";
}
}