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;
}
Related
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.
There are several options for one item. I.e. Item1 --> Option1: black, white, pink Option2: Small, Medium, Large --> each option is either available(1) or not available(0).
So if someone chooses Item1 with color black and size small the status should be returned based on availability.
My code is the following:
foreach ($item['option'] as $option) {
if ($option['availability']=0) { echo 'Not available'; } else { echo 'Available'; } }
So far so good. My problem is that I only want to print the status message only once, because the item with the configuration is either available or unavailable. With the above code I get twice the echo 'Not available' if both options in the example above are unavailable. I only would like to print :Not available' only once even if several options are unavailable. How can that be achieved?
I hope my question/problem is clear. Thank you for your help!
Use '==' because you are comparing the values and you can use break to stop the loop.
foreach ($item['option'] as $option) {
if ($option['availability']==0) {
echo 'Not available';
break;
} else {
echo 'Available';
break;
}
}
You can break; the loop or just exit it, exit;.
(This had to be a answer as i am not able to put it as a comment*
Pretty sure this is a quick and easy question but I have a form that on action POST goes to a confirmation page. I need a message to display on the confirmation page if the user selects county1 but if user selects county2, county3, or county4. However, when I setup the statement it's not working. Probably a syntax error or two on my part. Any help would be greatly appreciated.
A messy idea of what I think should work:
<?php $county=$_POST['County'];
if ($county="Polk") {
echo "Important message about your county"; }
else {
echo " "; // Or nothing at all
}
?>
But
<?php echo $_POST['County'] ?>
displays the name of the county so I know the submission is carrying through. Thoughts on why my above code wouldn't be working? If you could flag syntax errors or code placement that'd be much appreciated! Thank you!
Inside the if condition you should use two equal operators instead of one . try this code
<?php
$county = isset($_POST['County'])?$_POST['County']:"";
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
?>
Use double equal in your if statement for comparison
See the answer and read the comment to understand why you have to change it
if ($county="Polk") {
Single equal is assignment operator, it assign value
Change this line to this
if ($county=="Polk") {
So your whole code should look like this
$county=$_POST['County'];
if ($county == "Polk") {
echo "Important message about your county";
}
else {
echo " "; // Or nothing at all
}
Use double equal sign, double equal is comparison operator,
Here you are checking if the $county is equal to Polk or not,
Means you are comparing value
I am using a with statement for values in a table cell. Prior to inserting this statement all was fine. All I am trying to do is to change the color label on the text depending on the string value, but now the Cell is blank which tells me that none of the cases are true. I suspect that if there was an error instead then the cells after the statement would also be blank, but they are complete.
If I had to guess, I would say there is an error in the string declaration, but I am not sure.
<?php
$result = mysql_query("SELECT * FROM logs ORDER BY logid DESC");
while($data = mysql_fetch_array($result))
{
echo '<tr>
<td>'.$data["logid"].'</td>
<td class="center">'.$data["date"].'</td>
<td class="center">';
$log_type='.$data["log_type"].';
switch ($log_type) {
case "Check in":
echo '<span class="label label-success">'.$data["log_type"].'</span>';
break;
case "Log":
echo '<span class="label">'.$data["log_type"].'</span>';
break;
case "Sensor Event":
echo '<span class="label label-important">'.$data["log_type"].'</span>';
break;
}
echo '</td>
<td class="center">'.$data["log_entry"].'</td>
<td class="center">'.$data["customer_id"].'</td>
</tr>';
}
?>
The full code block simply reads data from a mysql table and spits it out in a nice table format into a website. By using different CSS classes, I can change the label color.
Question is why is this Case only a blank cell?
UPDATE: for those of you reading this and looking for the specific answer I have changed the line:
$log_type='.$data["log_type"].';
to
$log_type=$data["log_type"];
and that fixed it.
It's a pretty obvious mistake. Variables under single quotes will not be parsed which is what you have done. With your current code, $log_type will have the value .$data["log_type"]. (tested)
So, your code of:
$log_type= '.$data["log_type"].';
should be:
$log_type= $data["log_type"];
If you want single quotes around the value, you should do:
$log_type = "'".$data["log_type"]."'";
$data holds your result set array.
In this line :
$log_type='.$data["log_type"].';
you are accesing the result set in incorrcet manner, to access the attributes of the result set, you just have to enclose them in quotes against the object ( $data in this case ) holding the array.Below is the correct way to solve it :
$log_type=$data["log_type"];
to concat or enclose your solution in ' :
"'".$log_type."'" OR "'".$data["log_type"]."'"
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 :)