I have used the following SWITCH statement to ECHO one of three CASE's. However when I do this the output to the web page shows the CASE label then the actual echo-ed statement.
$sc_stk_poa is a boolean field of either 0 or -1.
$sc_stk_prc_stanard is a price field in the MySQL database.
echo $x = $sc_stk_poa;
switch($x) {
case 0: echo "£{$sc_stk_prc_standard}";
break;
case -1: echo "POA";
break;
default: echo "";
}
The output I get in the webpage is something like: -1POA or 0£59.14 depending which case is selected to output.
Write less convoluted things :
<?php
switch ($sc_stk_poa)
{
case 0:
echo "£{$sc_stk_prc_standard}";
break;
case -1:
echo "POA";
break;
default:
echo "";
}
There was no need for variable $x, was it?
It's not displaying the label. You have an echo here: <?php echo $x=$sc_stk_poa;. This is the one outputting the value of x, which is used in your cases.
Related
I have some dynamic values to place in variable, but for testing purpose I am taking dummy values, The problem is that I want to set value to the variable inside switch case by checking different conditions and call that variable outside switch case to print out put but I am not getting any value while echo .
So how to print the value by calling that variable?
<?php
$favcolor = "red";
$color_val;
switch ($favcolor) {
case "red":
return $color_val="Your favorite color is red!";
// echo "Your favorite color is red!";
break;
case "blue":
return $color_val="Your favorite color is blue!";
break;
case "green":
return $color_val="Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
echo $color_val;
You need to remove the "return" keyword from your switch. It doesn't do what I think you are intending for it to do there.
I have a php code that is meant to redirect people to a new page if they get the correct username and password. I have used a switch statement which has the header location function. The problem is that the header is executing for both cases and also the default keyword. I would only like the header to be excuted for one of the correct username and passwords
<?php
switch($_POST["username"] + "|" + $_POST["password"]) {
case"un1"|"pw1":
header("Location:http://MyFavWebsite.php");
break;
case"un2"|"pw2":
echo "hello";
break;
default:
echo "Go Away";
}
?>
I would like to know if this could be fixed i will appreciate any help to solve this problem.
case"un1"|"pw1":
Because that is not the right format for a string and it breaks your switch structure
case "un1|pw1":
Is.
And Oh someone might ask. That | is a bitwise OR operator and its result for your first case is TRUE that's why you always get that redirect.
header has no issue but your switch syntax is wrong, use both parameters as one condition
i.e
<?php
switch($_POST["username"].$_POST["password"]) {
case"un1pw1":
header("Location:http://MyFavWebsite.php");
break;
case"un2pw2":
echo "hello";
break;
default:
echo "Go Away";
}
//e.g
$un='un1';
$pass='pw1';
switch($un.$pass) {
case"un1pw1":
header("Location:http://MyFavWebsite.php");
break;
case"un2pw2":
echo "hello";
break;
default:
echo "Go Away";
}
?>
Try to change case"un1"|"pw1": to case "un1|pw1"
i got the code below to find the url of the site and this works. But I want a switch statement to search this url in a list and echo something else in for every url.
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
As far as I understand the question, this looks like what you are looking for:
switch ($actual_link) {
case 'http://domain/something1/':
echo 'Something 1';
break;
case 'http://domain/something2/':
echo 'SomeSthing 2';
break;
default:
echo 'default';
}
I have this function for show type name :
function is_typename($type){
switch ($type){
case 1:
echo "Bring an extra $500";
break;
case 2:
echo "Bring an open mind";
break;
case 3:
echo "Bring 15 bottles of SPF 50 Sunscreen";
break;
case 4:
echo "Bring lots of money";
break;
case 5:
echo "Bring a swimsuit";
break;
}
}
No for show link I have :
echo ''.$title.'';
in action i see :
Bring an extra $500titeltest
Problem: $type and typename show in outer href and <a></a>. how do fix this?
instead of echo use return,
example
case 1:
return "Bring an extra $500";
break;
I am creating my first PHP script that tests the user on cranial nerve association; specifically the name of the cranial nerve is displayed to the user and they are supposed to click on the correct number corresponding to the name of the cranial nerve.
I will post my code first and my question after:
<?php
$generated_nerve_number = mt_rand(1,12);
switch($generated_nerve_number) {
case '1':
echo "Olfactory";
break;
case '2':
echo "Optic";
break;
case '3':
echo "Oculomotor";
break;
case '4':
echo "Trochlear";
break;
case '5':
echo "Trigeminal";
break;
case '6':
echo "Abducens";
break;
case '7':
echo "Facial";
break;
case '8':
echo "Vestibulocochlear";
break;
case '9':
echo "Glossopharyngeal";
break;
case '10':
echo "Vagus";
break;
case '11':
echo "Accessory";
break;
case '12':
echo "Hypoglossal";
break;
}
?>
<html>
<head>
<title>Cranial Nerves Test</title>
</head>
<body>
<p>Select the cranial nerve number below associated with the name of the cranial nerve given above:</p>
<form action="cranial.php" method="POST">
<?php
echo "Cranial Number: ";
for($i = 1; $i <= 12; $i++) {
echo "<input type=\"submit\" name=\"nerve_$i\" class=\"nerve_number\" value=\"$i\">";
}
?>
<?php
$submit = (isset($_POST['nerve_' . $i])) ? $_POST['nerve_' . $i] : NULL;
if($submit) {
$selected_nerve_number = $_POST['nerve_' . $i];
if($generated_nerve_number == $selected_nerve_number) {
echo "That is correct!";
} else {
echo "Sorry that is incorrect.";
}
}
?>
</form>
</body></html>
A quick overview of my thought process: I am randomly generating a number between 1-12 and using that number in a switch statement to echo the name of the cranial nerve. I used a for loop to generate submit buttons with names that contain the number of the cranial nerve corresponding to its displayed value. Lastly, my plan on checking whether the answer is correct or not is to use an if statement comparing the randomly generated number to the number selected and, if this is true, output a message saying that they were correct.
This is where the problem comes in: when I click any of the buttons, whether its the correct answer or not, the page just refreshes without giving any feedback on whether the answer was right or wrong. Can someone please point out the flaw?
Additionally, if there is a more optimal way of doing something in this script please let me know.
Thanks in advance.
There is a slight problem with this approach. Your script is actually having two steps to consider: first, sending a page to the client with a randomly chosen cranial nerve, then comparing a choice with what was randomly specified.
If you wish your script to do this, you must add some logic so that it will know it have to react differently.
if (isset($_POST))
{/*compare the user choice with what you had sent.
you will have to do add an input to your form containing the random value AND the chosen value, or you eon't be able to compare them*/}
currently, you're trying to do all this at once, eg, you're comparing the return value of your user before you even receive it in your post! Understand that once sent to the client (browser) your page is not linked anymore to the php. So here:
if($generated_nerve_number == $selected_nerve_number) {
you're actually comparing the random number you just generated, with the answer you will have from your user in the FUTURE, since he has not even seen it on screen yet :)