Which is equivalent for PHP Include at CodeIgniter3? - php

Please explain to me how to do it:
<?php
if(!isset($_GET['s'])) {
$_GET['s'] = ""; }
if ($_GET['s'] == "") { include("Templates/Tutorial/1.tpl"); }
if ($_GET['s'] == "1") { include("Templates/Tutorial/1.tpl"); }
if ($_GET['s'] == "2") { include("Templates/Tutorial/2.tpl"); }
if ($_GET['s'] == "3") { include("Templates/Tutorial/3.tpl"); }
if ($_GET['s'] == "4") { include("Templates/Tutorial/4.tpl"); }
if ($_GET['s'] == "5") { include("Templates/Tutorial/5.tpl"); }
?>
Thank you very much!

I think this will also work
<?php
$inc = 1;
if(isset($_GET['s']) && $_GET['s'] != ""):
$inc = (int)$_GET['s'];
endif;
include("Templates/Tutorial/$inc.tpl");
?>

Related

How to Exit If Statement and Run Another IF Statement?

$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
**I try to Exit() First If Condition, continue to next IF Condition, But Cannot Continue next IF condition totally Exit Overall Please Fix My Problem Thankyou **
No need to use else condition if you want to run second if statement
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
What About the idea of this one.
<?php
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' )
{
if($var2 == 'run'){
echo 'run';
}
elseif($var3 == 'go'){
echo 'go';
}
elseif($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
}
just use only if.. use return
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}else {
return;
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}else {
return;
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}else {
return;
}
}

How to condense multiple IF statements simply in PHP

Here if my code. It works and runs and does everything I want it to do, but its too long. How can I condense it? My PHP level is basic.
Would I use an array? or a function? Can you kindly post the code you would use to make this more efficient?
<?php
$product = $result[CategoryId];
if ($product == "1") {
echo "Tonneau Cover to suit";
} elseif ($product == "2") {
echo "Clip On Tonneau Cover to suit";
} elseif ($product == "3") {
echo "INSERT";
} elseif ($product == "4") {
echo "INSERT";
} elseif ($product == "5") {
echo "INSERT";
} elseif ($product == "6") {
echo "INSERT";
} elseif ($product == "7") {
echo "INSERT";
} elseif ($product == "8") {
echo "INSERT";
} elseif ($product == "9") {
echo "INSERT";
} elseif ($product == "10") {
echo "INSERT";
} elseif ($product == "11") {
echo "INSERT";
} elseif ($product == "12") {
echo "INSERT";
} elseif ($product == "13") {
echo "INSERT";
} elseif ($product == "14") {
echo "INSERT";
} elseif ($product == "15") {
echo "INSERT";
} elseif ($product == "16") {
echo "INSERT";
} elseif ($product == "17") {
echo "INSERT";
} else {
echo ".";
}
?>
yes you can use array in this manner
$product = $result[CategoryId];
$ogh=["Tonneau Cover to suit","Clip On Tonneau Cover to suit","INSERT"];
if(count($ogh)>$product)
echo $ogh[$product];
else
echo "Invalid";
Based on what you have in the example:
<?php
$product = (int) $result['CategoryId'];
if ($product === 1) {
echo "Tonneau Cover to suit";
} elseif ($product === 2) {
echo "Clip On Tonneau Cover to suit";
} elseif ($product >=3 || $product <=17) {
echo "INSERT";
} else {
echo ".";
}
?>

PHP if condition fails inside a foreach loop

<?php
$key='APS';
$value='A|B|';
if ($key == 'APS'){
$aps = $key;
if (!empty($value)){
if(preg_match("/\|/",$value)){
$elephant = explode('|',$value);
foreach ($elephant as $elekey=>$elevalue){
if($elevalue = 'A'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'B'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'C'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'D'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'E'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
}
}
else{
echo $singlevalue = $value;
}
}
else {
echo $value='NIL';
}
}
?>
The above code may be lengthy but it's a very simple example, where if you execute you can see the if conditions will be failing to escape the loops.
Why in PHP if condition fails inside a foreach loop?
The Problem : You are using assignment operator inside if statements
Replace = with == or === (strict check)
if($elevalue = 'B'){
to
if($elevalue == 'B'){ [or] if($elevalue === 'B'){

PHP if/else statement issue

I'm having some issues with this statement,
<?php
$cert_mess = $vehicle['make'];
if ($vehicle["make"] == "Honda" && $vehicle["Certified"] == "0") {
$cert_mess = "DFWCertAutos";
}
elseif ($vehicle["make"] == "Cadillac" && $vehicle["Certified"] == "0") {
$cert_mess = "DFWCertAutos";
}
elseif (!in_array($vehicle['make'], array('Cadillac','Honda') )) {
$cert_mess = "DFWCertAutos";
}
?>
<div style="font-size:10px; padding:10px; padding-top: 0px;">*This car is <?php
echo $cert_mess ?> certified.</div>
Any suggestions? currently it just displays $cert_mess as 'make' and ignores the if / else if statements.
A simpler code can be the following:
$cert_mess = $vehicle['make'];
if (($vehicle["make"] == "Honda" && $vehicle["Certified"] == "0")
|| ($vehicle["make"] == "Cadillac" && $vehicle["Certified"] == "0")
|| !in_array($vehicle['make'], array('Cadillac','Honda'))
) {
$cert_mess = "DFWCertAutos";
}
Simpler still:
$cert_mess = $vehicle['make'];
if (!in_array($vehicle['make'], array('Cadillac', 'Honda')) || $vehicle['certified'] == '0')
{
$cert_mess = 'DFWCertAutos';
}

How to go to 'else if' loop after 'if' condition is sucessfully entered

Is there any way to go to the else if body after the if condition is sucessfully entered?
Is there any way to go to the else if body after the if condition is sucessfully entered?
if ($axisX == $axisXOne) { /* Main IF Statement */
if($axisY =='0' && $axisYOne == '1') { $second = '2';}
else if($axisY =='0' && $axisYOne == '2') { $second = '1';}
else if($axisY =='1' && $axisYOne == '0') { $second = '2';}
else if($axisY =='1' && $axisYOne == '2') { $second = '0';}
else if($axisY =='2' && $axisYOne == '0') { $second = '1';}
else if($axisY =='2' && $axisYOne == '1') { $second = '0';}
if (($_POST['button'.$axisX.$second] == null) && ($curVal != $axisX.$second)){ /* Inner IF Statement */
echo $axisX.$second;
}
}
else if ($axisY == $axisYOne) { /* Main ELSE IF statement */
if($axisX =='0' && $axisXOne == '1') { $second = '2';}
else if($axisX =='0' && $axisXOne == '2') { $second = '1';}
else if($axisX =='1' && $axisXOne == '0') { $second = '2';}
else if($axisX =='1' && $axisXOne == '2') { $second = '0';}
else if($axisX =='2' && $axisXOne == '0') { $second = '1';}
else if($axisX =='2' && $axisXOne == '1') { $second = '0';}
if($_POST['button'.$second.$axisY] == null) {/* Inner IF Statement */
echo $second.$axisY;
}
}
else if ($xAxis == $yAxis && $xAxisOne == $yAxisOne) { /* other Main ELSE IF Statement*/
if($comVal[0] == '00' && $comVal[1] == '11') { $diagon = '22';}
else if($comVal[0] == '00' && $comVal[1] == '22') { $diagon = '11';}
else if($comVal[0] == '11' && $comVal[1] == '00') { $diagon = '22';}
else if($comVal[0] == '11' && $comVal[1] == '22') { $diagon = '00';}
else if($comVal[0] == '22' && $comVal[1] == '00') { $diagon = '11';}
else if($comVal[0] == '22' && $comVal[1] == '11') { $diagon = '00';}
if($_POST['button'.$diagon] == null) {
echo $diagon;
}
}
If Main IF Statement evaluates true and Inner IF Statement evalautes false then go to Main ELSE IF ladder. If Main IF Statement evaluates true and Inner IF Statement evalautes true then stop loop . If Main IF Statement evaluates false, directly go to Main ELSE IF Statement
If Main ELSE IF Statement evaluates true and Inner IF Statement evalautes false then go to Main ELSE IF ladder. If Main ElSE IF Statement evaluates true and Inner IF Statement evalautes true ,then stop loop .If Main ELSE IF Statement evaluates false, directly go to other Main ELSE IF Statements
Try this:
$success = true;
if($a == $Xaxis && $b == $yaxis) {
$zAxis = $Xaxis + $yaxis;
$success = $Xaxis != $zAxis;
}
if( ($b == $Xaxis && $a == $yaxis) || !$success) {
// do stuff here
}
Or something along those lines.

Categories