Using switch on $_GET with if - loops

So I'm trying basically to make it like: ?p=blabla&dep=blabla
switch($_GET['p'])
{
case 'home':
include("template/index.html");
break;
case null:
include("template/index.html");
break;
case 'roster':
include("template/roster.html");
break;
case 'about':
include("template/about.html");
break;
case 'members':
include("members/index.php");
break;
}
if(($_GET['p'] == 'about') && ($_GET['dep'] == 'hospital'))
{
include("template/hospital.html");
}
And it still includes both about.html and hospital.html when I do blablabla?p=about&dep=hospital
How can I fix this?

Just put the if statement inside the switch case.
case 'about':
if ($_GET['dep'] == 'hospital')
include("template/hospital.html");
else
include("template/about.html");
break;

This is exactly what you ask for.
First you have your switch statement. It sees there is 'about' in $_GET['p'] so it will include that script.
Afterwards you have your if and this also evaluates to true, thus it's included.
To change this:
add another if in your 'about' case.
case 'about':
if ($_GET['dep'] == 'hospital') break;
include("template/about.html");
break;

Your switch is rocessed before the line that looks for dep=hospital, so it will include about.html before it even looks for the department.
If you want to display just hospital.html, but only if p=about move the test into the case.
switch($_GET['p'])
{
case 'home':
include("template/index.html");
break;
case null:
include("template/index.html");
break;
case 'roster':
include("template/roster.html");
break;
case 'about':
if(($_GET['dep'] == 'hospital')) {
include("template/hospital.html");
} else {
include("template/about.html");
}
break;
case 'members':
include("members/index.php");
break;
}

Related

Function remove(atOffsets:) complains about wrong type only at runtime

I have this pretty simple code in Playground. It compiles without errors, but when I try to run it, I get an error with remove(atOffsets:) function. Why is that?
enum CandyColor: CaseIterable {
case red, green, yellow, brown
}
enum CandyFilling: CaseIterable {
case chocolade, nuts
}
extension CaseIterable {
static var random: Self {
Self.allCases.randomElement()!
}
}
typealias Candy = (color: CandyColor, filling: CandyFilling)
var candies = (0...20).map{ _ in Candy(color: CandyColor.random, filling: CandyFilling.random)}
var redWithChocolade = [Candy]()
var yellowWithNuts = [Candy]()
var brownAndGreenWithChocolade = [Candy]()
var indiciesToRemove = IndexSet()
for (index, candy) in candies.enumerated() {
switch candy {
case (.red, .chocolade):
redWithChocolade.append(candy)
indiciesToRemove.insert(index)
case (.yellow, .nuts):
yellowWithNuts.append(candy)
indiciesToRemove.insert(index)
case let(c) where [.brown, .green].contains(c.color) && c.filling == .chocolade:
brownAndGreenWithChocolade.append(c)
indiciesToRemove.insert(index)
default:
break
}
}
candies.remove(atOffsets: indiciesToRemove) // Error: candidate expects value of type 'Int' for parameter #1
print("Left in heap: \(candies)")
Why don't you use just filter(_:) instead?
let leftOver = candies.filter { candy in
switch candy {
case (.red, .chocolade):
redWithChocolade.append(candy)
return false
case (.yellow, .nuts):
yellowWithNuts.append(candy)
return false
case (.brown, .chocolade):
fallthrough
case (.green, .chocolade):
brownORGreenWithChocolade.append(candy)
return false
default: return true
}
}
Or in case you really need in-place removal, removeAll(where:):
candies.removeAll { candy in
switch candy {
case (.red, .chocolade):
redWithChocolade.append(candy)
return true
case (.yellow, .nuts):
yellowWithNuts.append(candy)
return true
case (.brown, .chocolade):
fallthrough
case (.green, .chocolade):
brownORGreenWithChocolade.append(candy)
return true
default: return false
}
}
You should add:
import SwiftUI
to your script because it contains remove(atOffsets:) implementation and XCode doesn't import this framework implicitly.

Show and close dynamically created dialog in angularjs

I have scenarios which are created dynamically by clicking Add Scenario button. Initially 1 scenario will be there and then user can add 4 scenarios. Maximum 5 scenarios can be added. Each scenario has a calculator icon and by clicking it, it will show a calculator popup.
If user clicks calculator icon on a particular scenario, it should show only particular scenario's calculator popup; and if user clicks close, it should close the popup.
I'm passing scenarios.Id in showCalculatorPopup(scenarios.Id) and CloseCalculatorPopup(scenarios.Id) and with scenarios.Id i'm trying to open and close particular scenario's popup.
My issue is if I click a calculator icon in scenario 1, popup is being opened in all scenarios. I tried using eval(), window[] to no avail.
Updated the code.
Instead of Switch Case i'm using
vm.showCalculatorPopup = function (ScenarioId) {
vm['calculatorPopup' + ScenarioId] = true;
}
Added break;. Even after adding break it's not working properly. When i open and close calculator for 1st time in scenario 1 then its working fine.
But when i add scenario 2 and click calculator icon, its shows the popup and when i click close in scenario 1 popup it close the scenario 2 popup.
Can't use index because button is inside the Licenceplates and its inside the Sections and its inside the Scenarios.
<div data-ng-app="ang" data-ng-controller="InputController as input">
<div data-ng-repeat="scenarios in input.model.Scenarios"> <!-- Scenario Id = 1, 2, 3, 4, 5 -->
<div data-ng-repeat="sections in scenarios.Sections">
<div data-ng-repeat="licensePlates in sections.LicensePlates">
<button data-ng-click="input.showCalculatorPopup(scenarios.Id)">Calculate</button>
<div data-ng-show="input.calculatorPopup{{scenarios.Id}}">
Dialog Box
<button data-ng-click="input.CloseCalculatorPopup(scenarios.Id)"></button>
</div>
</div>
</div>
</div>
</div>
Latest Angular Controller
vm.showCalculatorPopup = function (ScenarioId) {
vm['calculatorPopup' + ScenarioId] = true;
}
vm.showCalculatorPopup = function (ScenarioId) {
vm['calculatorPopup' + ScenarioId] = false;
}
Previous Angular Controller
vm.showCalculatorPopup = function (ScenarioId) {
vm.eval('calculatorPopup' + ScenarioId) = true;
// or
vm.window['calculatorPopup' + ScenarioId] = true;
vm.calculatorPopup1 = true; // working
// or
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = true;
break;
case 2:
vm.calculatorPopup2 = true;
break;
case 3:
vm.calculatorPopup3 = true;
break;
case 4:
vm.calculatorPopup4 = true;
break;
case 5:
vm.calculatorPopup5 = true;
break;
}
}
vm.CloseCalculatorPopup = function (ScenarioId) {
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = false;
break;
//case 3 ,4
case 5:
vm.calculatorPopup2 = false;
break;
}
}
Your switch cases are never breaking, so you are executing every later case.
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = true;
break;
case 2:
vm.calculatorPopup2 = true;
break;
case 3:
vm.calculatorPopup3 = true;
break;
case 4:
vm.calculatorPopup4 = true;
break;
case 5:
vm.calculatorPopup5 = true;
break;
}
You have the same problem with the other swtich. More info on this.
you could pass $index instead of scenarios.Id. finally you need to add a break statement in your switch statements
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = true;
break;
case 2:
vm.calculatorPopup2 = true;
break;
case 3:
vm.calculatorPopup3 = true;
break;
case 4:
vm.calculatorPopup4 = true;
break;
case 5:
vm.calculatorPopup5 = true;
break;
}
}
vm.CloseCalculatorPopup = function (ScenarioId) {
switch (ScenarioId) {
case 1:
vm.calculatorPopup1 = false;
break;
case 2:
vm.calculatorPopup2 = false;
break;
}
}
break stops the evaluation of your switch statement.

AngularJS: When applying custom filter to data, i get an empty output

I'm trying to apply a filter to some data that I'm pulling out of Firebase.
My HTML look like this:
<span ng-bind-html="game.rating | ratings"></span>
Note: If I remove the "| ratings" it does work, and output the original text from firebase.
But once I apply this filter, I don't get any output, it's just empty. :(
I'm totally new to both Angular, javascript and firebase. So any advice is very appreciated
'use strict';
MyApp.filter('ratings', function() {
return function(rating) {
switch(rating) {
case 1:
return "1star";
case 2:
return "2stars";
case 3:
return "3stars";
case 4:
return "4stars";
}
}
})
Have any of you seen this before?
Maybe the output is not an int? Parse it. And use a default, its good practice.
MyApp.filter('ratings', function() {
return function(rating) {
switch(parseInt(rating)) {
case 1:
return "1star";
case 2:
return "2stars";
case 3:
return "3stars";
case 4:
return "4stars";
default:
return "Not set";
}
}
})

Extjs - newbie, Switch case function not working

In this following code I am getting the desired output
fn: function(btn) {
switch(btn){
case 'yes':
Ext.Msg.prompt('Milton', 'Where is it?');
break;
case 'no':
Ext.Msg.alert('Milton',
'Im going to burn the building down!');
break;
case 'cancel':
Ext.Msg.wait('Saving tables to disk...','File Copy');
break;
}
}
This Works just fine. Now i am trying to do a function call in the switch 'yes' but i don't get any output in the screen.
This is the code i am using.
case 'yes':
Ext.Msg.prompt('Milton', 'Where is it?', function(btn,txt)
{
if (txt.toLowerCase() == 'the office') {
Ext.get('my_id').dom.innerHTML = 'Dull Work';
}else{
Ext.get('my_id').dom.innerHTML = txt;
}
Ext.DomHelper.applyStyles('my_id',{
background: 'transparent
url(images/stapler.png) 50% 50% no-repeat'
});
});
break;
Using this code inside the swich case 'yes' , i am getting a blank screen. Even the dialog box has dissapeared. Please help.
It is a very basic javascript error. A string cannot extend over the end of the line. This will work.
case 'yes':
Ext.Msg.prompt('Milton', 'Where is it?', function(btn,txt){
if (txt.toLowerCase() == 'the office') {
Ext.get('my_id').dom.innerHTML = 'Dull Work';
}else{
Ext.get('my_id').dom.innerHTML = txt;
}
Ext.DomHelper.applyStyles('my_id',{
background: 'transparent url(images/stapler.png) 50% 50% no-repeat'
});
});
break;
In the console I got the error SyntaxError: Unexpected token ILLEGAL

C nested switches: outer switch's case inside inner switch

I'm adding coroutine support to an interpreter I'm writing and I'd like to do something like the following:
typedef enum {
bar_stuff,
bar_other
} Bar;
typedef enum {
foo_error=-1,
foo_none=0,
foo_again
} Foo_state;
Foo_state do_foo(Bar bar,Foo_state foo)
{
switch(foo)
{
case foo_none: //start
switch(bar)
{
case bar_stuff:
//do stuff
return foo_none;
case bar_other:
//do other stuff
return foo_again;
case foo_again: //!! this doesn't work
/* edit: this is supposed to be a case of
* switch(foo), not switch(bar)
*/
//do more other stuff
return foo_none;
default:
//stuff
return foo_none;
}
default:
//fail
return foo_error;
}
}
Obviously this dosn't work (I get duplicate case value, alternative is probably undefined behavior/segfault). I could write switch(bar) as an if/else if/else chain, but I was hoping there was a better way.
I'm using gcc if that makes a difference.
Edit:
The following would work, but would be a PITA to maintain:
Foo_state do_foo2(Bar bar,Foo_state foo)
{
switch(foo)
{
case foo_none: goto case_foo_none;
case foo_again: goto case_foo_again;
default:
//fail
return foo_error;
}
case_foo_none: //start
switch(bar)
{
case bar_stuff:
//do stuff
return foo_none;
case bar_other:
//do other stuff
return foo_again;
case_foo_again:
//do more other stuff
return foo_none;
default:
//stuff
return foo_none;
}
}
Edit 2:
Well, this doesn't seem to be yielding the aforementioned 'better way', so I'd like know if anyone foresees a problem with writing it like this:
Foo_state do_foo3(Bar bar,Foo_state foo)
{
switch(foo)
{
case foo_none: //start
if(bar == bar_stuff)
{
printf("do stuff\n");
return foo_none;
}
else if(bar == bar_other)
{
printf("do other stuff\n");
return foo_again;
case foo_again: //continue
printf("do more other stuff\n");
return foo_none;
}
else
{
printf("stuff\n");
return foo_none;
}
default:
//fail
return foo_error;
}
}
The problem I see with this is missing a bar_* value (since there are several functions like this, and some of the enums have dozens of values), but I suppose a test script for that should work...
You can also just put { } inside each case: statement
without them the whole case stack is evaluated as a single unit, so no variables can be defined within one case:
But by putting
case blah:
{
// do stuff
}
break;
You can put anythign you want inside the case statement.
Hm ... use an equivalent of a Karnaugh Map to simplify the logic and do it all with
if (cond1 && cond2) {
doX();
return;
}
if (cond3 && cond4) {
doY();
return;
}
// Sometimes you can take shortcuts
if (cond5) {
doZ();
} else {
doW();
}
return;
This code is readable. Nested things are best avoided if possible.
Checking the simplest conditions first will make the function simpler.
IN YOUR CASE START WITH:
Foo_state do_foo2(Bar bar,Foo_state foo) {
if (foo != foo_none && foo != foo_again) {
return foo_error;
}
...
if (foo == foo_none) {
...
}
// Implicit Else
...
A simple fix would be to change the values in the bar_ enum so that they are unique with respect to the foo_ enum. That doesn't address the fact, however, that your code is confusing. Why would you look for a foo_ value inside of the bar_ switch statement? Syntactically, it is valid (as long as the values are unique) but its poor coding.
Ok, it seems that your code does this:
bar \ foo foo_none foo_again other
bar_stuff doStuff, return foo_none do more other stuff, return foo_none return foo_error
bar_other do other stuff, return foo_again do more other stuff, return foo_none return foo_error
other stuff, return foo_none do more other stuff, return foo_none return foo_error
This is what I meant by a Karnaugh Map. Now, here is the simplest implementation:
Foo_state do_foo2(Bar bar,Foo_state foo) {
if (foo == foo_again) {
// do more stuff
return foo_none;
}
if (foo != foo_none) { // other
return foo_error;
}
// foo_none
if (bar == bar_stuff) {
// do stuff
return foo_none;
}
if (bar == bar_other) {
// do other stuff
return foo_again;
}
// At this point bar = other
// stuff
return foo_none;
}
I believe this does the same thing as your code, but does not use switches and gotos. You can fill out a table with the result, and you can also put both implementations through a unit tests to make sure they do the same thing for all inputs.

Resources