Heres what I want to do:
I have a variable named $user, where the information is being POSTED from a form. If the form data is empty I want to do one thing, if the form data is just a comma or symbol I want to do another thing, and if the form data is correct I want to do a third thing. This is how I have it set up, will this work? If not, what can I do to make it work?
switch($user){
case 1:
if(empty($user)){
echo "<h2>"."<strong>"."You entered NOTHING, PLEASE Re-Enter"."</strong>"."</h2>";
}
case 2:
if($user == ",") {
echo "<h2>"."<strong>"."You did not enter any valid names/abbreviations to search, PLEASE Re-Enter"."</strong>"."</h2>";
}
default:
print "==========================================="."<br />";
tester($company, $user);
print "==========================================="."<br />";
print "========The following were NOT found====="."<br />";
notFound($company, $user);
print "==========================================="."<br />";
}
Right now it is automatically going to default. It runs, but I want integrate the two qualifiers, how would I go about doing that?
You need to put break at the end of each case in a switch statement or it falls through to the next case. I also think you may be confused about the way switch works. If you're switching on $user, case matches against the value of $user--so you don't even need if-statements.
switch($user){
case "":
echo "<h2>"."<strong>"."You entered NOTHING, PLEASE Re-Enter"."</strong>"."</h2>";
break;
case ",":
echo "<h2>"."<strong>"."You did not enter any valid names/abbreviations to search, PLEASE Re-Enter"."</strong>"."</h2>";
break;
default:
print "==========================================="."<br />";
tester($company, $user);
print "==========================================="."<br />";
print "========The following were NOT found====="."<br />";
notFound($company, $user);
print "==========================================="."<br />";
}
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"
So I'm busy on a verification system, when the player of the game puts a sentence in his/her status, in this case: HT-N5I4-S3ZI-GU6A and my script checks if it's the same one as the code in my db. If it is, its proven to be their account. Because I've experienced that people make accounts under fake names.
When I echo the code in my db, it echoes:
HT-N5I4-S3ZI-GU6A
And the one in my test players mission echoes:
HT-N5I4-S3ZI-GU6A
It is exactly the same code, but when I wanna compare them like this:
if($_SESSION['user']['email_activation'] == $functions->getMotto($_SESSION['user']['username']))
{
echo "Works";
}
else
{
echo "Doesnt work";
}
It aways echoes 'doesn\'t work'. Did I use the wrong equalization operator? Or any other mistakes?
Thanks
Try This Code
if($_SESSION['user']['email_activation'] === $functions->getMotto($_SESSION['user']['username']))
{
echo "Works";
}
else
{
echo "Doesnt work";
}
Thanks.
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