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 :)
Related
How do I use php so it tests if a value in a mysql table is equal to a string? I am making a bans list and theres several categories, mutes, warnings and bans. The problem at the moment is they all display in every category so bans, mutes, warnings display in bans, mutes and warnings. They aren't sorted. I tried to fix it by using
<td>
<?php
if($row['punishmentType'] == 'BAN') {
echo "<img src='https://mcapi.ca/avatar/2d/" . $row['name'] . "/25' style='margin-bottom:5px;margin-right:5px;border-radius:2px;' />" . $row['name'];
}
else {
echo 'ERROR'
}
?>
</td>
But it comes up with ERROR. In the mysql table, theres 3 punishment types, BAN, MUTE and WARNING. Can someone tell me whats wrong with the code above? And how I fix it.
First of all you should check if $row['punishmentType'] has content. Then you can use a switch statement to separate your logic according to the type of punishment.
You could do:
<?php
if(!empty($row['punishmentType'])) {
switch($row['punishmentType']) {
case 'BAN': ...
break;
case 'MUTE': ...
break;
case 'WARNING': ...
break;
}
i am trying to show information using IF/ELSEIF commands
i have A through Z along the top of the page and want to show a table with all results starting with each letter
for example i have
<a href='?a'>A</a>
<?php
if($_GET == a)
{
echo "<table><tr><th>[picture]</th><th>information</th></tr>";
}
?>
i want to show a table with all the information, how would i do this using IF/ELSE commands? is there a better way of doing this without going to a different page?
thanks in advance for any help
I think it would be easier/cleaner to use a switch-case instead of if-else for your purpose here.
First off, try changing the links to something like this:
<a href='?l=a'>A</a>
and
<a href='?l=b'>B</a>
Then you should try to access the chosen letter with something like this:
<?php
$sLetter = null;
if (isset($_GET['l'])) {
$sLetter = strtolower($_GET['l']);
}
switch ($sLetter) {
case 'a':
echo "Information related to A";
break;
case 'b':
echo "Information related to B";
break;
// Continue in a similar way for the remaining letters
default:
echo "No information..."; // or perhaps show all A-Z information
break;
}
Note: For testing purposes, this is okay. But Superglobals should always be validated and sanitised to make your application more secure.
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 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.
I'm learning yii framework for an e-commerce project and it was going great so far. I have an addition form for estates and that form is generating using database. The code below, generates the form (/views/ad/_form), and giving names to input fields like detail["ad-title"], detail["ad-image"] etc.
<?php
$connection = Yii::app()->db;
$command = $connection->createCommand("SELECT * FROM eml_ozellikler");
$options = $command->queryAll();
$command = $connection->createCommand("SELECT * FROM eml_kurallar");
$rules = $command->queryAll();
$i = 0;
foreach($options as $option){
echo '<div class="row">';
echo $form->labelEx($model, 'detail["'.$option['name'].'"]');
switch($option['tur']){
case "textfield":
echo $form->textField($model, 'detail["'.$option['name'].'"]');
break;
case "textarea":
echo $form->textArea($model, 'detail["'.$option['name'].'"]', array('rows'=>'5','cols'=>'40'));
break;
case "integer":
echo $form->textField($model, 'detail["'.$option['name'].'"]');
break;
case "selectbox":
CHtml::dropDownList($option['label'], 'detail["'.$option['name'].'"]', $rules[$i]['values']);
break;
case "radio":
break;
case "file":
echo $form->fileField($model, 'detail["'.$option['name'].'"]');
break;
case "image":
break;
}
echo $form->error($model,'detail["'.$option['name'].'"]');
echo '</div>';
$i++;
}
?>
The problem is when giving rules to them. Rules are working just for looking, when i add to rules this
array('detay["ad-title"]', 'required'),
Then, that field is being required and getting a (*). But when i submit the form, then it gives an error saying "Ad.detail["ad-title"]" value isn't defined.
Without rules, i can post and get the posted value correctly using $_POST['Ad']['detail']["ad-title"] etc.
Also; i checked the Yii Framework docs but couldn't find any useful thing except tabular input and Form Builder, and i couldn't implement it to my code. Because i don't want to create variables at my model, i just want to send data using just one variable and rule.
Thanks, çağlar.
I did not find any rule regarding this as you are giving.
array('detay["ad-title"]', 'required'),
if ad-title is your table field name, then you may write.
array('ad-title', 'required'),
that's it ...
otherwise you need to learn rules, below is the one of the link where you can learn.
http://www.yiiframework.com/wiki/56/
Thanks.