How to simplify multiple switch in PHP? - php

is it possible to simplify this code? I am trying to put all cases in a switch but always d break in the first case and i need all echo's in the html. What is possible? Thank you!
$resultservices = mysqli_query($connecDB,"SELECT * FROM clients WHERE id_client = $id_client");
while($rowservice = mysqli_fetch_array($resultservices)){
$php = (int)$rowservice['php'];
$java = (int)$rowservice['java'];
$ruby = (int)$rowservice['ruby'];
$node = (int)$rowservice['node'];
}
// Values can be "1" or "0". Example: php:1, java:1, ruby:0, node:1
switch ($php) {
case 0: break;
case 1: echo "<li>php</li>"; break;
}
switch ($java) {
case 0: break;
case 1: echo "<li>java</li>"; break;
}
switch ($ruby) {
case 0: break;
case 1: echo "<li>ruby</li>"; break;
}
switch ($node) {
case 0: break;
case 1: echo "<li>node</li>"; break;
}

While I'm not sure what you're trying to do, how about:
$resultservices = mysqli_query($connecDB,"SELECT * FROM clients WHERE id_client = $id_client");
while($rowservice = mysqli_fetch_array($resultservices)){
$service[1] = (int)$rowservice['1'];
$service[2] = (int)$rowservice['1'];
$service[3] = (int)$rowservice['0'];
$service[4] = (int)$rowservice['1'];
}
foreach ($service as $k=>$v) {
if ($v) {
echo "<li>service".$k."</li>";
}
}
[edit] I see we've got some new variables.
while($rowservice = mysqli_fetch_array($resultservices)){
$service['php'] = (int)$rowservice['php'];
$service['java'] = (int)$rowservice['java'];
$service['ruby'] = (int)$rowservice['ruby'];
$service['node'] = (int)$rowservice['node'];
}
foreach ($service as $k=>$v) {
if ($v) {
echo "<li>".$k."</li>";
}
}
Although really, all you're doing is outputting the last row of your MySQL, so you could also do
$resultservices = mysqli_query($connecDB,"SELECT * FROM clients WHERE id_client = '".mysqli_real_escape_string($connecDB, $id_client)."' ORDER BY id DESC LIMIT 1");
while($rowservice = mysqli_fetch_array($resultservices)){
if ($rowservice['php']) {
echo "<li>php</li>"
}
if ($rowservice['java']) {
echo "<li>java</li>"
}
if ($rowservice['ruby']) {
echo "<li>ruby</li>"
}
if ($rowservice['node']) {
echo "<li>node</li>"
}
}

Given that 3 of your four service values are going to have the SAME value, you could eliminate 2 of the switches and end up with the same results:
$service01 = (int)$rowservice['1'];
$service02 = (int)$rowservice['1']; // identical to service01
$service03 = (int)$rowservice['0'];
$service04 = (int)$rowservice['1']; // identical to service01
meaning you could have:
switch($service01) {
case 0: break;
case 1: echo "<li>service01, 02, and 04</li>"; break;
}
And then, assuming these values will never be anything but true/false 0/1 values, you could eliminate the switches entirely and go with a conventional if:
if ($service01) {
echo "service 01, 02 and 04";
}

Related

Change order of output based on if condition

I've build my nice page in PHP, but I would like to get a different output order based on the result of a if condition applied at the beginning of the page.
IE:
if(condition is true){
block n1
block n2 }
else{
block n2
block n1 }
Can you kindly advise on what's the best practices in this case?
I think a flag should solve the problem, but I'm struggling to understand how.
You could prepare you blocks and then exactly what you wrote :
$block1 = "12345";
$block2 = "67890";
$inverted = true;
if ($inverted===true) {
echo $block2.$block1;
} else {
echo $block1.$block2;
}
There are several approaches to this. One would be with if and else and if you have many possibilities, then switch case would be advisable.
$check = 1;
if( $check === 1) {
echo `show 1`;
} else {
echo 'It is not 1';
}
// OR
switch($check) {
case 1:
echo 'Show 1';
break;
case 2:
echo 'Show 2';
break;
case 3:
echo 'Show 3';
break;
default:
echo 'It is not 1,2,3';
}
u can use array just like that
$orders = ['a', 'b'];
if (1 === 1) {
$orders = ['b', 'a'];
}
with this you don't need "else" part
foreach ($orders as $order){
echo $order."<br/>";
}

PHP case switch statement with variable set in drop-down

HI can someone tell me where I'm going wrong with this code. For some reason the variable $BlogType; isn't going through the Switch statement. This variable is set using a HTML drop-down and I have tested that its value changes accordingly with the user choice using the echo statement at the beginning of the code.
But It doesn't seem to be going past the first 'break;' in the statement, as the echo statement in the Switch code below isn't printed:
echo "<br /> value in case " . $BlogType;
echo "<br /> Blog " . $BlogType; // OK, value changes with dropdown choice
function getParameter($param, $defaultValue) {
if (array_key_exists($param, $_GET)) {
$value=$_GET[$param];
return isSet($value)?$value:$defaultValue;
}
return $defaultValue;
}
$BlogType = getParameter("blog_type", "defaultValue");
$byDate = getParameter("by_date", "defaultValue");
$loopArr = array();
switch ($BlogType) {
case 0:
$loopArr = $all_posts;
break;
case 1:
$loopArr = $music_matters->posts;
echo "<br /> value in case " . $BlogType;
break;
case 2:
$loopArr = $staff_student_news->posts;
break;
case 3:
$loopArr = $kbs_news_events->posts;
break;
default:
$loopArr = array();
}
?>
<?php foreach ($loopArr as $post): {
{ // code here

PHP Switch using it with Functions

I have this switch and I do certain things based on the selection of the action with the switch. Based on my testing, function thats in the switch is not even taking place when the page runs.
I am interested in being able to running the sortby action for now. when I go to the page, switch puts me in the first case but does not run the function.Why? How do I fix it?
switch ($_GET['action']) {
case 'sortby':
sort_by($_GET['sortby']);
break;
case 'add':
resident_add($_GET['residentID']);
include('inc/modify/add.php');
break;
case 'edit':
resident_edit($_GET['residentID']);
include('inc/modify/edit.php');
break;
case 'delete':
resident_delete($_GET['residentID']);
include('inc/modify/delete.php');
break;
case 'search':
echo "";
break;
default:
resident_default($_GET['sortby']);
}
function sort_by($sortby) {
if ($sortby == "last_name") {
$sort_db_field = "Last Name";
$sort_order = "ASC";
} elseif ($sortby == "lot") {
$sort_db_field = "Lot";
$sort_order = "ASC";
} elseif ($sortby == "date_added") {
$sort_db_field = "No";
$sort_order = "DESC";
} else {
include('inc/error.php?error_code=100');
}
return $sort_db_field;
return $sort_order;
}
$data = mysqli_query($dbcon, "SELECT * FROM `residents` ORDER BY `residents`.`".$sort_db_field."` ".$sort_order."") or die(mysqli_error());
You can't have 2 return statements in a function, the second one will never be executed.
Declare two variable before your switch:
$sort_db_field = "";
$sort_order = "";
switch ($_GET['action']) {
/*snip*/
}
Then in the function drop the returns. This function will now set the values in the two variables you declared.

Using conditional values from an array in an if...statement

I have an array of conditions :
$arrConditions = array ('>=2', '==1', '<=10');
...which I want to be able to use in an if...statement.
IE.
if (5 $arrConditions[0])
{
...do something
}
...which would be the same as :
if (5 >= 2)
{
...do something
}
Any help?
Thanks
Such a requirement is a sure sign of a bad design.
Most likely you can do that another, more usual way.
Nevertheless, never use eval for such things.
At least store each operator in pairs - an operator and operand.
$arrConditions = array (
array('>=',2),
array('==',1),
array('<=',10),
);
and then use switch:
list ($operator,$operand) = $arrConditions[0];
switch($operator) {
case '==':
$result = ($input == $operand);
break;
case '>=':
$result = ($input >= $operand);
break;
// and so on
}
But again - most likely you can solve it another, much easier way.
What about this ?
<?php
$arrConditions = array('==2', '==9', '==5', '==1', '==10', '==6', '==7');
$count = 0;
$myval = 0;
foreach ($arrConditions as $cond) {
$str = "if(5 $cond) { return $count;}";
$evalval = eval($str);
if (!empty($evalval)) {
$myval = $count;
}
$count++;
}
switch ($myval) {
case 0: echo '==2 satisfied';
break;
case 1: echo '==9 satisfied';
break;
case 2: echo '==5 satisfied';
break;
case 3: echo '==1 satisfied';
break;
case 4: echo '==10 satisfied';
break;
default : echo 'No condition satisfied';
}
?>

Use of switch and case in php and codeigniter

I want use of switch and case in php and codeigniter library, i try it as following code, But I not receive output. what do i do?
Demo: http://codepad.viper-7.com/Wq0Noj
function indicators() {
$CI = &get_instance();
$Year = '1355';
$Month = '03';
switch ($Year) {
case 1354:
$key=array('0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6');
$output = $key[$Month-1];
break;
case 1355:
$key=array('0.6','0.7','0.2','0.4','0.7','0.1','0.7','0.2','0.5','0.9','0.4','0.8');
$output = $key[$Month-1];
break;
echo $output; // The output should be: 0.7
}
}
I think your echo needs to be outside of the switch as well... checking to verify.
Yep, the echo needs to be outside. The type should actually be coerced when comparing.
<?php
$s = '5';
switch ($s) {
case 5:
echo "Foo\n";
break;
default:
echo "Bar\n";
break;
}
echo $s;
OUTPUT
Foo
5
And for your example:
<?php
function indicators() {
$Year = '1355';
$Month = '03';
switch ($Year) {
case 1354:
$key=array('0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6','0.6');
$output = $key[$Month-1];
break;
case 1355:
$key=array('0.6','0.7','0.2','0.4','0.7','0.1','0.7','0.2','0.5','0.9','0.4','0.8');
$output = $key[$Month-1];
break;
}
echo $output; // The output should be: 0.7
}
indicators();
OUTPUT
0.2
Which is correct according to the code. '03' - 1 == 2. $key[2] == '0.2'
As pointed out in the comment below by #vstm, the docs state that the "switch/case does loose comparision."

Categories