PHP: Echoing HTML When Multiple Checkboxes Options Are Checked - php

I've edited this question, because I felt I needed to be clearer in my request.
The code below relates to options checked in a checkbox. It will echo 'both values' when they are both checked, and it will echo 'only valueA', I cannot get it to echo 'only valueB' which instead prints blankly with no echo. Thoughts?
<?php
if (( $a == "General" ) && ( $b == "Specialist" )) {
echo '<h2>both values are printed in HTML</h2>';
}
?>
<?php
if (( $a == "General" ) && ( $b != "Specialist" )){
echo '<h2>only valueA is printed in HTML</h2>';
}
?>
<?php
if (( $a != "General" ) && ( $b == "Specialist" )) {
echo '<h2>only valueB is printed in HTML</h2>';
}
?>

If you want to check whether a value is in array you should use in_array function. Your code should look like this:
<?php
$CheckBox = $form_data['field'][1]
$a = ( "valueA");
$b = ( "valueB");
$isAChecked = in_array($a, $CheckBox[1]);
$isBChecked = in_array($b, $CheckBox[1]);
?>
<?php
if ( $isAChecked && $isBChecked ) {
echo '<h2>both values are printed in HTML</h2>';
} else if ( $isAChecked ) {
echo '<h2>only valueA is printed in HTML</h2>';
} else if ( $isBChecked ) {
echo '<h2>only valueB is printed in HTML</h2>';
} else {
echo '<h2>nothing printed</h2>'
}
?>

Related

Conditional Statement confusion PHP

EDIT : Damn... It was all about Path to the file and somehow I didn't thought about restructuring :| thanks to #Machavity I found the problem.
What is wrong with this code ?
If $foo is set and file exist file_exist() result should be 1.
else if $foo is set but file does not exist file_exist() result should be 2.
else result should be 3.
But I am only getting result 2 for all the three conditions. There gotta be something wrong with the second part of the elseif.
if ( isset ( $foo ) && file_exists ( 'bar.php' ) )
{
echo '1';
}
else if ( isset ( $foo ) && ( ! file_exists ( 'bar.php' ) ) )
{
echo '2';
}
else
{
echo '3';
}
Normally I don't like wrapping but try restructuring like this. This way you have a better idea of what's failing
if(isset($foo)) {
if(file_exists('bar.php')) {
echo '1';
} else {
echo '2';
}
} else {
echo '3';
}

if else statement, not outputting else if?

I have a small if else statement, where the if gets output, but not the else currently? I tried double pipe, but no luck.
I have a variable set at the top of my page -
$portfolio_style = ( isset( $uno_theme['portfolio-style-select'] ) ? $uno_theme['portfolio-style-select'] : null );
And then my if else which will not work -
<?php if ( $portfolio_style == 'portfolio_style_one' || 'portfolio_style_two' ) { ?>
<div class="grid">
<?php } elseif ( $portfolio_style == 'portfolio_style_three' ) { ?>
<div class="wfull">
<?php } ?>
I want it where if portfolio style one or two has been selected, show class 'grid', else if style three has been selected show class 'wfull'
Could I also refine this piece of code at all? -
$portfolio_style = ( isset( $uno_theme['portfolio-style-select'] ) ? $uno_theme['portfolio-style-select'] : null );
Many thanks
You cannot omit $portfolio_style variable after || because otherwise the sole string 'portfolio_style_two' will always evaluate as true as stated in the manual describing conversion to boolean:
Your code should be this:
<?php if ( $portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two' ) { ?>
<div class="grid">
<?php } elseif ( $portfolio_style == 'portfolio_style_three' ) { ?>
<div class="wfull">
<?php } ?>
Try to change the if condition:
<?php if ( $portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two' ) { ?>
you need to change this line as
if ( $portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two' )
Try this:
echo ($portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two') ? '<div class="grid">' : (($portfolio_style == 'portfolio_style_three' ) ? '<div class="wfull">' : '');

Main php function used to call other functions with arguments

What Iam trying to create if possible within WordPress or via just php is that i have functions such as below.
function myfunction_displays_something_one() {
echo "string";
echo "string"; }
function myfunction_displays_something_two() {
echo "string";
echo "string";
}
function myfunction_displays_something_three( $arg1, $arg2 ) {
echo "<p>";
echo $arg1 $arg2;
echo "/<p>";
}
I would like the functions above to be called and displayed via
function myfunction_displays( $display ) {
if ( $display == 'something-one' )
return myfunction_displays_something_two();
if ( $display == 'soemthing-two' )
return myfunction_displays_something_two();
}
when called vai the php file would like to call the three functions above like so
<?php myfunction_displays( something-two ); ?>
<?php myfunction_displays( something-three ); ?>
i have been able to make the first two work because they have no arguments but I am unable to call the third function because it has arguments.
Is there a way to create this maybe using Wordpress Filters or just php ?
Online DEMO : https://eval.in/87273
function myfunction_displays( $display ,$arrArg = array()) {
if ( $display == 'something-one' )
return myfunction_displays_something_two();
if ( $display == 'soemthing-two' )
return myfunction_displays_something_two();
if ( $display == 'soemthing-three' )
{
$a1 = isset($arrArg[0]) ? $arrArg[0] : "";
$a2 = isset($arrArg[1]) ? $arrArg[1] : "";
return myfunction_displays_something_three($a1,$a2);
}
}
And call it like this:
<?php myfunction_displays( 'something-two' ); ?>
<?php myfunction_displays( 'something-three',array(0=>'value1',1=>'value2')); ?>
Here is a quick way:
function myfunction_displays( $display, $arg1=NULL, $arg2=NULL ) {
if ( $display == 'something-one' )
return myfunction_displays_something_two();
if ( $display == 'something-two' )
return myfunction_displays_something_two();
if ( $display == 'something-three' )
return myfunction_displays_something_three($arg1, $arg2);
}
The "=NULL" part means you don't have to fill them in every time you call it.
An example:
<?php myfunction_displays( 'something-two' ); ?>
<?php myfunction_displays( 'something-three', 'value1', 'value2' ); ?>

Shorthand to check value in array

Is there a short way of doing this?
if ((isset($a['key']) && ($a['key'] == 'value')) {
echo 'equal';
// more code
}
else {
echo 'not equal';
// more code
}
I need to test lots of values on an array that can or cannot exist. I feel that this method is too verbose.
I could remove the isset() and mute the notices... but then I feel dirty.
Edit:
Answering Jack's question: "Could you give an example how you would test lots of values in an array?"
example:
if (isset($_GET['action']) && $_GET['action'] == 'view') {
//code
}
if (isset($_GET['filter']) && $_GET['filter'] == 'name') {
//code
}
if (isset($_GET['sort']) && $_GET['sort'] == 'up') {
//code
}
if (isset($_GET['tag']) && $_GET['tag'] == 'sometag') {
//code
}
etc...
For anyone still stumbling upon this question...
You could use PHP's coalescing operator:
if (($a['key'] ?? '') === 'value') {
echo 'equal';
// more code
}
else {
echo 'not equal';
// more code
}
See this question: using PHP's null coalescing operator on an array
I don't like to answer my own questions but I feel that the best and cleaner way to do this kind of checkings is to write a "helper funcion" like:
function iskeyval(&$a, $k, $v) {
return isset($a['key']) && ($a['key'] == 'value');
}
and then:
if (iskeyval($a, 'key', 'value')) {
...
}
else {
...
}
I have added comments to explain the code. Here is the code :
//this array maps the function with the get parameters
$functions = array (
"action" => "do_actions" ,
"filter" => "do_filters"
);
foreach ($_GET as $key=>$value) {
//check if this field is corresponding functions or not
if ( array_key_exists($key , $functions) ) {
call_user_func($functions[$key] , $key,$value);
}
}
function do_actions ($key , $value) {
//place your code here to play with this value
echo 'do_actions is called with ' . $key . 'and' . $value . "</br>";
}
function do_filters ($key , $value) {
//place your code here to play with this value
echo 'do_filters is called with ' . $key . ' and ' . $value . "</br>";
}
?>
$list = array(
0 => 'one',
1 => 'two',
2 => 'one',
3 => 'three',
4 => 'one',
);
if( #$list['xxx'] !== 'three')
echo 'Not ';
echo 'Equal';
Suppress the error reporting.

Newbie question: Why is my PHP OR operator not working?

I have a function where I want to display something if the value equals A, C or D.
The problem is if the value is set at B or E, the function is display data in the A.C.D. set in addition to the B or E set.
What am I doing wrong?
$linkp = opensky_featured_image_position();
if ( $linkp == 'featuredimg-1' || 'featuredimg-3' || 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
if ( $linkp == 'featuredimg-2' ) {
echo '<h1>L - '. opensky_featured_image_position() .'</h1>';
}
if ( $linkp == 'featuredimg-5' ) {
echo '<h1>N - '. opensky_featured_image_position() .'</h1>';
}
if ( $linkp == 'featuredimg-1' || 'featuredimg-3' || 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
Should be
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
If all the values of $linkkp are defined you could do something like this
$linkArr = array(
'featuredimg-1' => 'F',
'featuredimg-2' => 'L',
'featuredimg-3' => 'F',
'featuredimg-4' => 'F',
'featuredimg-5' => 'N',
);
$linkp = opensky_featured_image_position();
echo '<h1>'.$linkArr[$linkp].' - '. opensky_featured_image_position() .'</h1>';
It's all about operator precedence. Since == is with higher precedence, your code is rewritten as follows:
if ( ($linkp == 'featuredimg-1') || 'featuredimg-3' || 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
which turns to be
if ( false || true || true ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
Since when you cast a non-empty string to bool, it will be evaluated as true.
You have to rewrite it:
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
Which is equalent to:
if ( ($linkp == 'featuredimg-1') || ($linkp == 'featuredimg-3') || ($linkp == 'featuredimg-4') ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
But when you are dealing with such condition, maybe it's better to use in_array
For me this looks like that you're making too much use of if to describe something very simple: Certain values map to characters, that is essentially static data. No need to use so much verbose code to write that data into your code.
Consider using arrays instead to get the character you're actually looking for based on the number and then just echo it afterwards:
$linkp = opensky_featured_image_position();
sscanf($linkp, 'featuredimg-%d', $number);
$chars = 'FLN';
$map = array(1 => 0, 2 => 1, 3 => 0, 4 => 0, 5 => 2);
echo '<h1>', $chars[$map[$number]], ' - ', $linkp, '</h1>';
Demo
It should be:
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
..
'featuredimg-3' will always be evaluated as true, you have to try $linkp == 'whatever' in each or subcondition.
so :
if ( $linkp == 'featuredimg-1' || $linkp == 'featuredimg-3' || $linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
}
You'll need to specify the == operator for each condition in the or clause, otherwise, the string values such as 'featuredimg-3' will be evaluated on their own, and any non-empty string evaluates to true.
Also, when the choices are mutually exclusive, it is better to use elseif so that PHP can skip the remaining if statements when it matches one.
I've also added a final else, so you can see how you can do something in case none of the values match.
$linkp = opensky_featured_image_position();
if ( $linkp == 'featuredimg-1' ||
$linkp == 'featuredimg-3' ||
$linkp == 'featuredimg-4' ) {
echo '<h1>F - '. opensky_featured_image_position() .'</h1>';
} elseif ( $linkp == 'featuredimg-2' ) {
echo '<h1>L - '. opensky_featured_image_position() .'</h1>';
} elseif ( $linkp == 'featuredimg-5' ) {
echo '<h1>N - '. opensky_featured_image_position() .'</h1>';
} else {
echo '<h1>Invalid or missing image position</h1>';
}
You may also want to take a look at the switch/case structure.
Using arrays as others have suggested does allow for brevity and arguably readability. If you do use arrays, you may still need to check to ensure that you find a value in the array and handle the case if you don't.

Categories