PHP case switch statement with variable set in drop-down - php

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

Related

Assign result of function within switch to variable

here's some pseudo-code (it's not written correctly, the point of my ? is the variable, not the switch):
switch ($action) {
case "1":
//this is a function
case "2":
//this is a function
//etc.
}
How should this be written:
$variable = result of function in case 1.
Your switch statement is wrong . It requires a break keyword between every case
$action = 1;
$result = "Success";
switch ($action) {
case 1:
$variable = $result;
echo $variable;//prints Success
//this is a function
break; // like this
case 2:
//this is a function
break;//
//etc.
}
Just run the function(s) (you can pass args in too) as part of the code within the case / break blocks like this :
$action = 1;
switch ($action) {
case 1:
$variable = someFunctionOne();
break;
case 2:
$variable = someOtherFunctionTwo();
break;
//etc.
}
how to variable the result of php switch.
ex:
<?php
//variables of cases
$var_1 = 1;
$var_2 = 2;
$var_3 = 3;
$var_0 = 0;
//end variables of cases
//action variable
$action = 10;
//end action variable
//start switch
switch ($action) {
case "1":
echo "$var_1;";
break;
case "2":
echo "$var_2;";
break;
case "3":
echo "$var_3;";
break;
default:
echo "$var_0;";
}
//receives the value of the switch.
$switch_result = get_result_case;
//in this my example I need to enter the value of the case in a variable.
?>
in this my example I need to enter the value of the case in a variable.

How to simplify multiple switch in 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";
}

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

PHP Switch statement error

<?php
include 'db.php';
$mail=$_SESSION['session_u_e_mail'];
if(!isset($_GET['edit']))
{
$_GET['edit']=0;
}
switch($_GET['edit'])
{
case 1: echo "Value";
break;
case 2: echo "Value";
break;
default: echo "Value";
break;
}
I m facing problem that the values in every case is echoed twice.
I've just grabbed the
if(!isset($_GET['edit']))
{
$_GET['edit']=0;
}
switch($_GET['edit'])
{
case 1: echo "Value";
break;
case 2: echo "Value";
break;
default: echo "Value";
break;
}
this portion and pasted onto codepad and it works fine.
Here is the working version: http://codepad.org/P8bhL5vl
It outputs just "Value".
There is nothing in this code that would even output your default value. No matter the input, this outputs Value. Look in db.php or the files included by it for debugging echoes.

Question about a php switch

Given the array below, can I do this for the switch(use the variable $cost inside the switch)?
Or does the switch($need a different variable here)
$cost=array(chocoru =>1, oeenergy =>0.35, lemondew =>0.55, chcmyst =>0.25, drsalty =>0.75);
Notice the $cost variable inside the switch is the same as the $cost associative array.
switch($cost)
{
case 1:
echo "You chose lemondew <br />";
echo "the price is".$cost["lemondew"]'<br>';
}
No, you have to iterate over the array with a loop.
foreach($cost as $product => $price)
{
echo "You chose " . $product . "<br />";
echo "the price is" . $price . '<br>';
}
You have a variable called $cost, which is an associative array mapping product names to their price. You can use the product name as array index, there is no need for any other variable or a switch statement:
echo "You chose $product <br />n";
echo "the price is " . $cost[$product] . "<br />n";
It won't work. Variable in switch needs must be equal to any of case values for associated code to be executed.
A switch statement is a lot like a set of ifs. For example:
switch ($var) {
case 1:
$other_var = 6;
break;
case 2:
$other_var = 3;
break;
case 3:
$other_var = 2;
break;
default:
$other_var = 0;
break;
}
is logically equivalent to:
if ($var == 1) {
$other_var = 6;
}
else if ($var == 2) {
$other_var = 3;
}
else if ($var == 3) {
$other_var = 2;
}
else {
$other_var = 0;
}
You want to use a different construct based on what you're trying to do.
No, you must use precise variable, that is one giving exactly one result in evaluation, it might be $cost['chocoru']. But in this case it makes no sense, so best way is, I guess, switching it by name, like:
echo "You chose $name <br />";
echo "the price is".$cost[$name].'<br>';

Categories