<html>
<head>
<title> GRADE DETAILS </title>
</head>
<body>
<?php
$myname=$_GET ['myname'];
$mygrade=$_GET ['mygrade'];
if(($myname=='#' or $myname=='!'))
{
echo "Special character not allowed.";
}
else if($myname==" ")
{
echo "Please enter your name";
}
else
{
switch($mygrade)
{
case "";
echo "you did not enter grade";
break;
case A;
echo "Increment 30% of basic salary";
break;
case B;
echo "Increment 10% of basic salary";
break;
default;
echo"Wrong Grade";
break;
}
}
?>
</body>
</html>
i want to set multiple conditions at my if else statement..i try many way, but still didn't work...i hope someone can help me ..nothing to do with my Switch Case, but the problem only at IF ELSE..PLS HELP.Thanks.
It's basically OK, but you need to replace the semicolons after the "CASE" statements with colons, and second put the text strings (A, B etc.) into quotes:
switch($mygrade) {
case "":
echo "you did not enter grade";
break;
case "A":
echo "Increment 30% of basic salary";
break;
case "B":
echo "Increment 10% of basic salary";
break;
default:
echo "Wrong Grade";
break;
}
You where missing some quotes, A and B are interpreted as constants
<?php
$myname= trim($_GET ['myname']);
$mygrade= trim($_GET ['mygrade']);
if(strstr('#',$myname) or strstr('!',$myname) )
{
echo "Special character not allowed.";
}
else if(empty($myname))
{
echo "Please enter your name";
}
else
{
switch($mygrade)
{
case "":
echo "you did not enter grade";
break;
case 'A':
echo "Increment 30% of basic salary";
break;
case 'B':
echo "Increment 10% of basic salary";
break;
default:
echo"Wrong Grade";
break;
}
}
Is this a plain PHP? I have my answers below. But, I am not recommending that you continue with this. You can search for other class or framework that can help you on the validation. This might be prone on injection as well.
To answer your question: (Basically)
$invalid = array("#", "!");
if (in_array($myname, $invalid)) {
echo 'Special character not allowed.';
}
if (empty($myname)) {
echo "Please enter your name";
}
if (!empty($mygrade)) {
switch($mygrade) {
case "":
echo "you did not enter grade";
break;
case "A":
echo "Increment 30% of basic salary";
break;
case "B":
echo "Increment 10% of basic salary";
break;
default:
echo "Wrong Grade";
break;
}
}
else {
echo 'Grade was empty';
}
Related
echo get_option('bp-username-field'); and echo get_option('bp-email-field'); respectively outputs checked and 0. but with this code both the cases are running. i.e. both hello from username and hello from email are dispayed.
switch("checked")
{
case get_option('bp-username-field'):
echo 'hello from username';
case get_option('bp-email-field'):
echo 'hello from email';
...
}
And if i change switch("0") it only echoes hello from email. Also, with swith(0) both case are running. What is this behaviour?
You have to add a break after the case. If not all cases will be executed. That is normal behavior for switch Statements
switch("checked")
{
case get_option('bp-username-field'):
echo 'hello from username';
break;
case get_option('bp-email-field'):
echo 'hello from email';
...
}
You are probably missing break
switch("checked")
{
case get_option('bp-username-field'):
echo 'hello from username';
break;
case get_option('bp-email-field'):
echo 'hello from email';
break;
...
}
When the first case gets executed, then you need to break the switch. You need to introduce break to break execution of rest of the cases that follows the selected case.
When switch(0) was called, it is the final case (as of here), so it doesn't execute the one before the second case.
switch/case does loose comparison. That's mean that "checked" == 0 is true. What you want to do is:
switch(true)
{
case get_option('bp-username-field') === "checked":
echo 'hello from username';
case get_option('bp-email-field') === "checked":
echo 'hello from email';
...
}
But in a switch statement, the condition is evaluated only once and the result is compared to each case statement. This mean that after the first case is evaluate as true, all the other case will be executed until the end of the switch. What you really want it:
if (get_option('bp-username-field') === "checked") {
echo 'hello from username';
}
if (get_option('bp-email-field') === "checked") {
echo 'hello from email';
}
Because your statement is wrong;
you should compare the variable what ever it is inside switch(variable) to all those cases. for example.
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
I would like to ask if it's possible to have a switch statement inside a php file where it is in mpdf.
this is my switch statement
switch($cts){
case 1:
echo "Not at all";
break;
Case 2:
echo "Ok";
break;
default;
echo "none";
}
but this could not be done in a mpdf.
Help.
You could store the respective messages in a variable and print it out outside the switch.
$msg = '';
switch($cts){
case 1:
$msg = "Not at all";
break;
case 2:
$msg = "Ok";
break;
default: $msg = "none";
}
echo $msg;
function title_of_page() {
// Get the user's location
$user_location = $_SERVER['REQUEST_URI'];
//Find just the filename
$filename = pathinfo($user_location, PATHINFO_FILENAME);
if ($user_location == "") {
switch ($filename) {
case 'intro': echo 'introduction'; break;
case 'about': echo 'about the author'; break;
case 'foreword': echo 'foreword'; break;
case 'pottery': echo 'pottery'; break;
case 'gold_jewelery': echo 'jewelry in bahrain'; break;
case 'metalwork': echo 'metalwork'; break;
case 'weaving': echo 'weaving'; break;
case 'glass': echo 'glass industry'; break;
case 'woodwork': echo 'woodwork'; break;
case 'plant': echo 'Plant-product handicrafts'; break;
case 'calligraphy': echo 'arabic calligraphy'; break;
case 'thanks': echo 'thanks & appreciation'; break;
case 'craft': echo 'craft centres'; break;
case 'videos': echo 'videos'; break;
case 'pano': echo 'panoramic'; break;
// These are the crafts subpages
case 'jasra_handicrafts': echo 'Al Jasra Handicrafts Center'; break;
case 'jasra_training': echo 'Al Jasra Training Center'; break;
case 'capital_mall': echo 'Capital Mall'; break;
case 'craft_industries': echo 'The Craft Industries Development Centre'; break;
default: echo '<img id="logo" src="img/logo.png">'; break;}
} else {
// ARABIC TITLES
switch ($user_location) {
case '/ar/intro.php': echo 'مقدمة'; break;
case '/ar/about.php': echo 'نبذة عن الكاتب'; break;
case '/ar/foreword.php.php': echo 'تقديم'; break;
case '/ar/pottery.php': echo 'الفخار'; break;
case '/ar/gold_jewelery.php': echo 'حلي الذهب والفضة واللآلئ'; break;
case '/ar/metalwork.php': echo 'الأعمال المعدنية'; break;
case '/ar/weaving.php': echo 'النسيج والتطريز وتفصيل الملابس'; break;
case '/ar/glass.php': echo 'صناعة الزجاج'; break;
case '/ar/woodwork.php': echo 'الأشغال الخشبية'; break;
case '/ar/plant.php': echo 'صناعة المنتجات من المواد المأخوذة من النباتات'; break;
case '/ar/calligraphy.php': echo 'الخط العربي'; break;
case '/ar/thanks.php': echo 'شكر وتقدير'; break;
case '/ar/craft.php': echo 'المراكز الحرفية'; break;
case '/ar/panoramic.php': echo 'بانوراما'; break;
// These are the crafts subpages
case '/ar/jasra_handicrafts.php': echo 'مركز الجسرة للحرف اليدوية'; break;
case '/ar/jasra_training.php': echo 'مركز الجسرة للتدريب'; break;
case '/ar/capital_mall.php': echo 'بمجمع العاصمة'; break;
case '/ar/craft_industries.php': echo 'مركز تنمية الصناعات الحرفية'; break;
default: echo '<img id="logo" src="../img/logo.png">'; break;}
}
}
This is currently the code i'm using. I'm building a PhoneGap app for a client of mine and the app is stationery (as in, client doesn't want a CMS). Anyway, what I want to happen is that when the user is viewing the English side of the app, it'll show the English titles however when the user is on the Arabic side of the app it'll show the same titles in Arabic.
Just wondering if i'm going the right way..?
So I have this commands handling:
$message = the entered message.
public function handleCommands($message, $username)
{
//Variables we're going to use
$space = strpos($message, ' '); # The first space.
$command = trim(substr($message, 1, $space)); # The command after the slash
$name = substr($message, $space + 1); # The name after the command.
switch ($command)
{
case 'ban':
$this->ban($name, $username);
break;
case 'prune':
$this->prune($username);
break;
case '':
echo 'Please use a command!';
break;
case 'test':
try
{
$this->test($name);
}
catch (exception $r)
{
echo $r->getMessage();
}
break;
}
}
This basically will check for the command.
$command = the entered word after the slash ( " / " ).
Can you see
case '':
This basically checks if there is no command after the slash.
Question: I want the system to check aswell, if the command exists in the cases.
For example:
user wrote:
/hello
But that command doesn't exists, considering we only have case 'ban', case 'prune', case 'test' and case ''.
there is no case 'hello', so it will throw an error.
Is there a function that does this sort of thing? How can I do this?
I believe what you're looking for is a default: case.
Example:
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
EDIT:
Fixed version of problem that was chatted about: http://privatepaste.com/bd34e7e63b
Use the case default:
switch ($command)
{
case 'ban':
$this->ban($name, $username);
break;
case 'prune':
$this->prune($username);
break;
case '':
echo 'Please use a command!';
break;
case 'test':
try
{
$this->test($name);
}
catch (exception $r)
{
echo $r->getMessage();
}
break;
default:
echo "That command does not exist.";
}
I am creating a report with the data which calls the stored procedure and that procedure returns
various sections (1,2,3,4,5,6) with the data in each section.Now the sections may contain or may
not contain the data.This is how i have wriiten my logic
foreach($this->$dbresults as $row){
$var1 ='';
If($var1!=$row['section']){
switch($row['section']){
case '1':echo "some thing data";
break;
case '2':echo "some thing data";
break;
case '3':echo "some thing data";
break;
case '4':echo "some thing data";
break;
case '5':echo "some thing data";
break;
case '6':echo "some thing data";
break;
}
}
$var1=$row['section']
}
So here My problem if any one of the section is not present then that section case cannot be executed
.I mean How do i execute the section even if the section is not returned from the database
I guess you're already ordering your results by section. If your sections are really 1-n, you could put your switch() code into some runsections function and do this:
$var1=0; $lastsection=16;
foreach($this->dbresults as $row) {
If($var1!=$row['section']){
for($num=$var1+1; $num<$row['section']; $num++) runsections($num);
runsections($row['section']);
}
$var1=$row['section'];
}
for($num=$var1+1;$num<=$lastsection;$num++) runsections($num);
if your sections aren't sequential numbers you could create an array and check if they've all been executed
$sections=array('a'=>0,'b'=>0,'c'=>0,'d'=>0,'e'=>0);
If($var1!=$row['section']){
unset($sections[$row['section']]);
runsection($row['section']);
}
...
}
foreach($sections as $num) {
runsection($num);
}
edit: so the runsections() function would look like this:
function runsections($section) {
switch($section){
case '1':echo "some thing data";
break;
case '2':echo "some thing data";
break;
case '3':echo "some thing data";
break;
case '4':echo "some thing data";
break;
case '5':echo "some thing data";
break;
case '6':echo "some thing data";
break;
}
}
switch($x){
case '1':
echo "some thing 1";
break;
case '2':
echo "some thing 2";
break;
case 'N':
echo "some thing N";
break;
default:
echo "some thing else";
}
After your last case, insert:
default: echo "some error";
The break is optional since it's the last case in the switch statement. Also, The single quotes are also optional if you're looking for numeric options.
case 1: echo "something";
break;
Maybe I am not quite understanding exactly what you want. The following code should work in the following conditions:
You always want to display the 6
sections with either DB data or
non-DB data.
You do not need to display the same
section multiple times.
$sections = range(1, 6);
foreach($sections as $sectionNum) {
$sectionNum = (string) $sectionNum;
$foundSection = false;
foreach($this->dbresults as $row) {
if ($row['section'] == $sectionNum) {
echo "section #$sectionNum has DB data: " . $row['data'];
$foundSection = true;
break;
}
}
if (!$foundSection) {
echo "section #$sectionNum does not have DB data.";
}
}
Here's what I've got:
foreach($this->dbresults as $row){
if(isset($row['section'])){
switch($row['section']){
case '1':echo "some thing data";
break;
case '2':echo "some thing data";
break;
case '3':echo "some thing data";
break;
case '4':echo "some thing data";
break;
case '5':echo "some thing data";
break;
case '6':echo "some thing data";
break;
default:echo "some thing data";
break;
}
} else {
//Do something since no section data was stored
}
}
I added a default case, fixed the small php errors ($this->$dbresults is changed, using isset instead of !='') and added an else to your if to do something if the section isn't found.