Does anyone know how to fix this error, please? PHP Warning: Undefined array key "gid" in /index.php
[index.php]
$gid = ['1245620'];
$mid = ['0111161];
$sid = ['0108778];
switch($_GET['gid']) {
case '1':
echo "1 selected!;
break;
case '2':
echo "2 selected!;
break;
default:
header("Location: http://$_SERVER['HTTP_HOST'];");
}
switch($_GET['mid']) {
case '1':
echo "1 selected!;
break;
case '2':
echo "2 selected!;
break;
default:
header("Location: http://$_SERVER['HTTP_HOST'];");
}
switch($_GET['sid']) {
case '1':
echo "1 selected!;
break;
case '2':
echo "2 selected!;
break;
default:
header("Location: http://$_SERVER['HTTP_HOST'];");
}
Should return on browser index.php?gid=1, index.php?mid=1, index.php?sid=1 but it returns me to default with an error in the error_log file (stated above.
Could it be due to me using three switches in one file (index.php)?
Any help please, it's giving me such an headache?
Undefined array key "gid"
This means there were not GET variables passed with ‘gif’ as the key.
Try debugging var_dump($_GET) to make sure any arguments are being passed. If not, check your Nginx/Apache config.
You will also wish to wrap the switches with an if statement to check isset($_GET[‘gid’]) is set before checking in the switch.
Related
What is the correct way to put an else clause in a switch statement? For example, instead of:
switch (input) {
case (1):
printf("yes!");
break
case (0):
printf("no!");
break;
default:
printf("invalid!");
}
To do something along the lines of (if possible):
switch (input) {
case (1):
printf("yes!");
case (0):
printf("no!");
else:
printf("invalid!");
}
I'm simply asking if there is a short-cut to not have to add a break after every case statement to act as-if it were the else part of an if statement.
The correct way is using default. Also ,you don't have to use break for non-default cases, only if you don't want execution to fall into the next case.
when using
getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK
to check what mode the app is currently in,
int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're in day time
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're at night!
case Configuration.UI_MODE_NIGHT_UNDEFINED:
// We don't know what mode we're in, assume notnight
}
if called this with AppCompatDelegate.MODE_NIGHT_YES earlier
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
is the return of currentNightMode to be Configuration.UI_MODE_NIGHT_YES?
what it would return when the AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM was set before
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
and the device has changed from light to dark them (or from dark to light)?
context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
tells current what mode the app will be in.
when
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
if change the system theme in settings (in Android Q),
the configuration.uiMode will reflect the change.
same with the
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
or
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
note: the configuration.uiMode change will trigger a config change and may cause recreate the activity.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
themeSystem.setVisibility(View.VISIBLE);
} else {
themeSystem.setVisibility(View.GONE);
}
switch (sharePref.getTheme()) {
case THEME_LIGHT:
themeLight.setChecked(true);
break;
case THEME_DARK:
themeDark.setChecked(true);
break;
case THEME_SYSTEM:
themeSystem.setChecked(true);
break;
default:
switch (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) {
case Configuration.UI_MODE_NIGHT_NO:
themeLight.setChecked(true);
break;
case Configuration.UI_MODE_NIGHT_YES:
themeDark.setChecked(true);
break;
case Configuration.UI_MODE_NIGHT_UNDEFINED:
themeLight.setChecked(true);
break;
}
}
themeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.themeLight:
setTheme(AppCompatDelegate.MODE_NIGHT_NO, THEME_LIGHT);
break;
case R.id.themeDark:
setTheme(AppCompatDelegate.MODE_NIGHT_YES, THEME_DARK);
break;
case R.id.themeSystem:
setTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, THEME_SYSTEM);
break;
}
}
});
I am fetching few values from my backend and assigning then to my scope variables in angular controller. for one of the variables I get the values between 1 till 7 and depending on the which number it is I want to do something like following:
.success(function(response){
$scope.initial_data=response;
angular.forEach($scope.initial_data, function(item){
$scope.exercise_id=item.exercise_type;
alert($scope.exercise_id) // This gives me either 1 or any number till 7
switch($scope.exercise_id){
case '1':
alert("1");
break;
case '2':
alert("2");
break;
default:
alert("Default");
}
However, this piece of code for switch statement always alerts gives me Default. What am I doing wrong?
i'm struggling to create hotkeys using angularjs $event. i need to simplify the codes for uppercase and lowercase. however i viewed from the console.log($event) they have different keyCode.
p is 112
P is 80
HTML:
<body ng-app="pointofsale" ng-controller="PointofSaleCtrl" ng-keypress="hotKeyEvent($event)">
Javascript:
$scope.hotKeyEvent = function(data){
console.log(data);
switch(data.keyCode){
case 112: // p
break;
case 80: // P
break;
default:
console.log('No key associated')
}
}
What am i missing here? i don't want to create two conditions for the same character but different case.
Appreciate any help, Thanks!
You could simplify your switch by grouping the case statements
like this:
switch(data.keyCode){
case 112: // p
case 80: // P
break;
default:
console.log('No key associated')
}
It's known as falling through - essentially, anything matched will continue to run until a break is hit.
Makes maintenance quite a bit easier too.
I'm dealing with a rather large enumeration, which can conceptually be divided as representing four different categories. Currently I'm using this enumeration in a switch case statement in order to map values into four separate arrays (in line with the four different categories).
What I'm curious about is if it is possible to change a locally defined variable after arbitrary cases in the switch case statement. This would allow for the ability to break the switch statement into these four different sections, and value assignments that would occur for each case -- if equivalent -- can occur at these sections.
A simplified example what I'm going for is as follows:
Setup
enum incidental_indexes {
arr1_0, arr1_2, arr2_0, arr1_1, arr2_1
} indexes;
struct foobar{
int arr1[3];
int arr2[2];
}
enum indexes unknown_index = ???; // In my code there are two separate indexes being mapped
// from one another, so for the sake of example imagine that
// this index is unknown
enum indexes curr_index = arr1_1; //Value here does not matter
struct foobar my_struc;
int * curr_arr;
int mapped_index;
Brute force approach
switch(unknown_index){
case(arr1_0):
curr_arr = my_struc.arr_1; //First category array
curr_index = arr1_0;
break;
case(arr1_1):
curr_arr = my_struc.arr_1; //First category array, again
curr_index = arr1_1;
break;
case(arr1_2):
curr_arr = my_struc.arr_1; //First category array, again, again
curr_index = arr1_2;
break;
case(arr2_0):
curr_index = arr2_0;
curr_arr = my_struc.arr_2; //Second category array
break;
case(arr2_1):
curr_index = arr2_1;
curr_arr = my_struc.arr_2; //....
break;
}
Ideal Approach
switch(unknown_index){
default: //Notice no break.
curr_arr = my_struc.arr_1; //First category array
case(arr1_0):
curr_index = arr1_0;
break;
case(arr1_1):
curr_index = arr1_1;
break;
case(arr1_2):
curr_index = arr1_2;
break;
default: //Something like a second default, however disallowed
curr_arr = my_struc.arr_2; //Second category array
case(arr2_0):
curr_index = arr2_0;
break;
case(arr2_1):
curr_index = arr2_1;
break;
}
The functional benefits are obviously nill, however I'm curious if this functionality even exists in C, or if there is perhaps a better method for executing this.
Thanks!
Switch statements only perform a single branch, so you can't jump around inside of the switch like that. What you can do however is group certain cases together without a break in between:
switch(curr_index){
case arr1_0:
case arr1_1:
case arr1_2:
curr_arr = my_struc.arr_1;
break;
case arr2_0:
case arr2_1:
curr_arr = my_struc.arr_2;
break;
}
EDIT:
For the index assignment part, you could do a second switch like this:
switch(unknown_index){
case arr1_0:
curr_index = arr1_0;
break;
case arr1_1:
curr_index = arr1_1;
break;
case arr1_2:
curr_index = arr1_2;
break;
case arr2_0:
curr_index = arr2_0;
break;
case arr2_1:
curr_index = arr2_1;
break;
}
But since you're always assigning whatever the value of unknown_index is, the above is the same as this:
curr_index = unknown_index;
One, no.
Two, just use ifs and elses. As the saying goes, when you have a hammer, everything looks like a nail. switch is a really weird "hammer" to try applying to everything.
Three, um, I guess you could use goto everywhere, but we decided this was a bad idea and creates horribly messes of code in the 80s or something.
Within a switch you have access to all the locally defined variables. I'm not quite sure I understand your question... it seems like what you're trying to do is best accomplished by 2 switches:
switch(unknown_index){
case(arr1_0):
case(arr1_1):
case(arr1_2):
curr_arr = my_struc.arr_1; //First category array, again, again
break;
case(arr2_0):
case(arr2_1):
curr_arr = my_struc.arr_2; //....
break;
}
switch(unknown_index){
case(arr1_0):
curr_index = arr1_0;
break;
case(arr1_1):
curr_index = arr1_1;
break;
case(arr1_2):
curr_index = arr1_2;
break;
case(arr2_0):
curr_index = arr2_0;
break;
case(arr2_1):
curr_index = arr2_1;
break;
}