<?php
$current_store_type = get_field("valj_typ");
$last_type = array_slice($current_store_type, -1, 1, true);
$type_title = "";
switch($last_type){
case "Butik":
$type_title = "Alla butiker";
break;
case "Cafe":
$type_title = "Alla caféer";
break;
case "Restaurang":
$type_title = "Alla restauranger";
break;
case "Service":
$type_title = "Alla Serviceställen";
break;
}
?>
I want to slice out the last item in the $current_store_type array. But it only works if the field is having one variable, as soon as I get it into an array it does not work.
Update: not working either
$current_store_type = get_field("valj_typ");
$last_type = end($current_store_type);
$type_title = "";
switch($last_type){
case "Butik":
$type_title = "Alla butiker";
break;
case "Cafe":
$type_title = "Alla caféer";
break;
case "Restaurang":
$type_title = "Alla restauranger";
break;
case "Service":
$type_title = "Alla Serviceställen";
break;
}
I think this is what you want to achive. It doesn't use array_slice but will function properly. If you insist on using array_slice check out #Random's comment in your question.
$last_type = end($current_store_type);
This code did the trick for me:
$last_type = end($current_store_type);
Related
Ok guys, i´m trying to switch inside a case but it does not work it allways falls back to the default. What am i missing?
When i try /index.php?siteSettings it displays the default, same thing when i try index.php?siteSettings&mode=adminSettings
switch($_SERVER['QUERY_STRING']){
case 'logout':
include("logout.php");
break;
case 'siteSettings':
switch($_GET['mode']){
case 'generalSettings':
$mainPageTitle = "General settings";
$caseFile = "".$admin_templates_path."/generalSettings.php";
break;
case 'adminSettings':
$mainPageTitle = "Admin settings";
$caseFile = "".$admin_templates_path."/adminSettings.php";
break;
case 'userSettings':
$mainPageTitle = "User settings";
$caseFile = "".$admin_templates_path."/userSettings.php";
break;
case 'advertising':
$mainPageTitle = "Advertising";
$caseFile = "".$admin_templates_path."/advertising.php";
break;
default:
$mainPageTitle = "General settings";
$caseFile = "".$admin_templates_path."/generalSettings.php";
break;
}
break;
default:
$mainPageTitle = "General settings";
$caseFile = "".$admin_templates_path."/generalSettings.php";
break;
}
After #aynber made me aware of my mistake this is what i did and it does work now.
Maybe someone will need it.
Thnx guys.
$switchPage = explode("~",str_replace(["?","&","="], "~",$_SERVER['QUERY_STRING']));
switch($switchPage[0]){
.....
Here is my code:
$order_newest = $order_votes = $order_featured = $order_frequent = '';
switch ($_GET['o']) {
case 'newest':
$order_newest = 'order_active';
break;
case 'votes':
$order_votes = 'order_active';
break;
case 'featured':
$order_featured = 'order_active';
break;
case 'frequent':
$order_frequent = 'order_active';
break;
default:
$order_newest = 'order_active';
break;
}
It throws following error when o argument isn't exist in the URL:
Notice: Undefined index: o in C:\xampp\htdocs... on line 71
I can fix the problem by adding this condition:
$order_newest = $order_votes = $order_featured = $order_frequent = '';
if ( isset($_GET['o']) ) {
switch ($_GET['o']) {
.
.
.
}
} else {
$order_newest = 'order_active';
}
But I guess this isn't the right way. Because in this case I have to add lots of conditions contain isset() function.
Anyway, is there any better approach to handle that?
Use filter_input(). As example:
$order = filter_input(INPUT_GET, 'o');
switch ($order) {
// ...
In PHP, is it possible to get a reference to the expression used in the switch statement?
For example:
switch ($_POST['id']) {
case 0:
$id = <switch expression>
break;
case 1:
$id = <switch expression>
break;
default
$id = null;
}
so if $_POST['id'] = 1, then $id = 1
Then I can test for if (! $id) {}
Obviously you are probably thinking why not just use $id = $_POST['id'] but in the real example it looks like this
switch (strtolower($load->post('payment_method')))
{
case 'paypal':
$payment_method = <switch/case expression>;
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_method = <switch/case expression>;
$payment_type = 'ecurrency';
break;
default:
//$payment_method = null; // taken from card number
$payment_type = 'card';
}
I dont want $payment_method assigned.
HAD A EUREKA MOMENT WHILST WRITING THIS
Well, that works for what I was trying to achieve anyway.
switch (($payment_method = strtolower($load->post('payment_method'))))
{
case 'paypal':
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_type = 'ecurrency';
break;
default:
unset($payment_method); // taken from card number
$payment_type = 'card';
}
There are no way
use for example such way
$cases = array(0, 1, 3 ,5);
$defaultVal = 1;
$id = in_array($_POST['id'], $cases) ? $_POST['id']: $defaultVal;
AFAIK there is no such feature in PHP.
But you can do what you are looking for as easy as this:
switch (strtolower($load->post('payment_method')))
{
case 'paypal':
$payment_method = 'paypal';
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_method = 'bitcoin';
$payment_type = 'ecurrency';
break;
default:
$payment_method = null; // taken from card type
$payment_type = 'card';
}
Actually I just realize that this probably is possible using a simple workaround:
switch ($switch_value = strtolower($load->post('payment_method')))
{
case 'paypal':
$payment_method = $switch_value;
$payment_type = 'ewallet';
break;
case 'bitcoin':
$payment_method = $switch_value;
$payment_type = 'ecurrency';
break;
default:
$payment_method = null; // taken from card type
$payment_type = 'card';
}
;-)
How can I correct/simplify this and put it in an array?
A link is passing: somelink.php?w=a (or b,c,d)
I want the page (somelink.php) to determine if "w" is set, and if set and the var matches, include the specified page.
<?php
if(isset($_GET['w'])&&($GET['w'] == "a")){include("1.htm");}
if(isset($_GET['w'])&&($GET['w'] == "b")){include("2.htm");}
if(isset($_GET['w'])&&($GET['w'] == "c")){include("3.htm");}
if(isset($_GET['w'])&&($GET['w'] == "d")){include("4.htm");}
else{include("1.htm");}
?>
try using:
$w = $_GET['w'];
if(isset($w)) {
switch(strtolower($w)) {
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
default:
include("not-found.htm");
break;
}
}
Use a switch statement:
if(isset($_GET['w']))
{
switch($_GET['w'])
{
case 'a': include("1.html"); break;
case 'b': include("2.html"); break;
case 'c': include("3.html"); break;
case 'd': include("4.html"); break;
default: include("1.html"); break;
}
} else {
include("1.html");
}
how about a simple array
$x=array('a'=>'1.html','b'=>'2.html');
then
include $x[$GET['w']];
Like this:
if(isset($_GET['w'])){
switch($_GET['w']){
case "a":
include("1.htm");
break;
case "b":
include("2.htm");
break;
case "c":
include("3.htm");
break;
case "d":
include("4.htm");
break;
}
}
But I wouldn't do it that way. I'd make it so that the name of the page corresponds to the value being retrieved from the $_GET variable. That way you could do something like this.
if(!empty($_GET['w'])){
include($_GET['w'] . ".htm");
}
Of course, you'd want a little filtering of the $_GET var too to make sure it doesn't get something you don't want there. Maybe like this.
$acceptable_values = array("some","acceptable","values");
if(!empty($_GET['w']) && in_array($_GET['w'],$acceptable_values) ){
include($_GET['w'] . ".htm");
}
As I'm sure you are aware, passing variables directly into include statements or database queries is a TERRIBLE idea. See here for why in this case.
http://websec.wordpress.com/2010/02/22/exploiting-php-file-inclusion-overview/
You could do a few things, lets take a look at some of them.
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
switch($webpage)
{
case 'b':
include '2.html';
break;
case 'c':
include '3.html';
break;
case 'd':
include '4.html';
break;
default:
include '1.html';
break;
}
Or we could use arrays
<?php
$webpage = '';
if(isset($_GET['w']))
$webpage = strtolower($_GET['w']);
$included_pages = array(
'a' => '1.htm',
'b' => '2.htm',
'c' => '3.htm',
'd' => '4.htm',
);
// Check inside our array
if(array_key_exists($webpage, $includes))
{
include $included_pages[$webpage];
}
else
{
// Couldn't find the site
include '1.htm';
}
I have a code to check through several fields to execute certain code as below:
switch($tag1 || $tag2 || $tag3 || $tag4 ||$tag5){
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
break;
case "chillicrab":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chilli_crab/$img_name";
break;
case "chickenrice":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chicken_rice/$img_name";
break;
case "chendol":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chendol/$img_name";
break;
}
But it does not work.
Anyone can help?
Switch support one value only. Only IF condition can have OR and AND condition.
$tags = get the tag value.
switch($tags){
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
break;
case "chillicrab":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chilli_crab/$img_name";
break;
case "chickenrice":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chicken_rice/$img_name";
break;
case "chendol":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/chendol/$img_name";
break;
}
The following should do the trick for you:
<?php
$tag1='satay';
$tag2='test2';
$tag3='digitalmarketing';
function isItThere($myTag)
{
switch($myTag)
{
case "satay":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/satay/$img_name";
echo $imgput;
break;
case "digitalmarketing":
$imgput = "/home/uploads/sandbox/jovine/Food/tags/interactive_marketing/$img_name";
echo $imgput;
break;
// etc etc
}
}
for($i=1;$i<4;$i++)
{
isItThere(${'tag'.$i});
}
?>
I have basically set up a small function that contains the switch statement and written a simple loop to test the variables.
As I said in my comment, you can't use more than one variable in the switch statement, but this will provide you a nice clean workaround to do the same thing without the need to write many long statements.