How to condense multiple IF statements simply in PHP - 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 ".";
}
?>

Related

Which is equivalent for PHP Include at CodeIgniter3?

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");
?>

PHP 'foreach' array concatenate echo

The following will echo "12334".
However, I would like for the "12334" to be placed in a variable say $wordNumValue.
I know it might be a simple thing, but to me it is not. Any help is much appreciated.
<?php
$msg ="hello";
$arrEn = str_split($msg);
foreach ($arrEn as &$value) {
if ($value == 'h') {
echo "1";
} elseif ($value == 'e') {
echo "2";
} elseif ($value == 'l') {
echo "3";
} elseif ($value == 'o') {
echo "4";
} else {
echo 'NULL';
}
}
?>
<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = "";
foreach ($arrEn as &$value) {
if ($value == 'h') {
echo "1";
$wordNumValue .= "1";
} elseif ($value == 'e') {
echo "2";
$wordNumValue .= "2";
} elseif ($value == 'l') {
echo "3";
$wordNumValue .= "3";
} elseif ($value == 'o') {
echo "4";
$wordNumValue .= "4";
} else {
echo 'NULL';
}
}
?>
Try this:
<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = '';
foreach ($arrEn as &$value) {
if ($value == 'h') {
echo "1";
$wordNumValue .= "1";
} elseif ($value == 'e') {
echo "2";
$wordNumValue .= "2";
} elseif ($value == 'l') {
echo "3";
$wordNumValue .= "3";
} elseif ($value == 'o') {
echo "4";
$wordNumValue .= "4";
} else {
echo 'NULL';
}
}
But as you have many elseifs I, would do this instead:
<?php
$msg ="hello";
$arrEn = str_split($msg);
$wordNumValue = '';
foreach ($arrEn as &$value) {
switch ($value) {
case "h":
$wordNumValue .= "1";
break;
case "e":
$wordNumValue .= "2";
break;
case "l":
$wordNumValue .= "3";
break;
case "o":
$wordNumValue .= "4";
break;
default:
echo 'NULL';
}
}
echo $wordNumValue;
And finally I echoed $wordNumValue instead of echoing single numbers several times.
You can add each string onto a variable using the .= assignment operator;
<?php
$msg ="hello";
$arrEn = str_split($msg);
foreach ($arrEn as &$value) {
if ($value == 'h') {
$wordNumValue .= "1";
} elseif ($value == 'e') {
$wordNumValue .= "2";
} elseif ($value == 'l') {
$wordNumValue .= "3";
} elseif ($value == 'o') {
$wordNumValue .= "4";
}
}
?>
You don't need the final else. See this, PHP: Assignment Operators, and this, PHP: String Operators.
Hope this helps.
You don't need to change your code , before foreach add ob_start();
and after foreach add $wordNumValue = ob_get_clean();

A line of code is showing as well as the webpage

<?php
session_start();
include_once "incfiles/connectdb.php";
include_once "incfiles/func.php";
$page="gamepoints.php";
logincheck();
$username=$_SESSION['username'];
$fetch=mysql_fetch_object(mysql_query("SELECT * FROM accounts WHERE username='$username'"));
if ($_GET['item']){
$item=strip_tags($_GET['item']);
if ($item == "LHP"){ $cost="50"; $amount="10000"; }
elseif ($item == "FMJ"){ $cost="70"; $amount="15000"; }
elseif ($item == "rankpoints"){ $cost="150"; $amount="10000"; }
elseif ($item == "awp"){ $cost="300"; $amount="1"; }
elseif ($item == "credits"){ $cost="50"; $amount="10"; }
elseif ($item == "money"){ $cost="300"; $amount="5000000"; }
From if if ($item == "LHP") to the end is what shows up on my webpage along with the page and I can't seem to figure out why
Not sure if this is all of your code - if it is, looks like you might be missing the last }.
<?php
session_start();
include_once "incfiles/connectdb.php";
include_once "incfiles/func.php";
$page="gamepoints.php";
logincheck();
$username=$_SESSION['username'];
$fetch=mysql_fetch_object(mysql_query("SELECT * FROM accounts WHERE username='$username'"));
if ($_GET['item']){
$item=strip_tags($_GET['item']);
if ($item == "LHP"){ $cost="50"; $amount="10000"; }
elseif ($item == "FMJ"){ $cost="70"; $amount="15000"; }
elseif ($item == "rankpoints"){ $cost="150"; $amount="10000"; }
elseif ($item == "awp"){ $cost="300"; $amount="1"; }
elseif ($item == "credits"){ $cost="50"; $amount="10"; }
elseif ($item == "money"){ $cost="300"; $amount="5000000"; }
}

I have used this code for populating Checkbox.. But it not Works

foreach($add_details_arr as $key=>$val1){
if($val1 == '1'){
$ck1= "checked=>'checked'";
}else{
$ck1= '';
} if($val1 == '2'){
$ck2= "checked=>'checked'";
}else{
$ck2= '';
}
}
in my view
<?php echo CHtml::activecheckBox($model,'setting[0]',array('value'=>'1','uncheckValue'=>null, 'class'=>'radio',checked=>$ck1)); ?>
<?php echo CHtml::activecheckBox($model,'setting[1]',array('value'=>'2','uncheckValue'=>null, 'class'=>'radio',checked=>$ck2)); ?>
Try this -
foreach($add_details_arr as $key=>$val1){
if($val1 == '1'){
$ck1 = 'checked';
}else{
$ck1= '';
} if($val1 == '2'){
$ck2= 'checked';
}else{
$ck2= '';
}
echo CHtml::activecheckBox($model,'setting[0]',array('value'=>'1','uncheckValue'=>null, 'class'=>'radio',checked=>$ck1));
echo CHtml::activecheckBox($model,'setting[1]',array('value'=>'2','uncheckValue'=>null, 'class'=>'radio',checked=>$ck2));
}

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';
}

Categories