Solved

Parse array then run a if function on result

Posted on 2016-08-25
5
52 Views
Last Modified: 2016-09-04
I have an array of items (cheese) in a checkbox format like so

array(
'type' => 'checkbox',
"holder" => "div",
"class" => "",
"heading" => __("Choose your cheese topping", 'rbm_menu_item'),
'param_name' => 'cheesebox',
'value' => array( 'Cheddar'=>'Cheddar', 'Gouda'=>' Gouda', 'Bleu'=>' Bleu' ),
"description" => __("<br /><hr class='gduo'>", 'rbm_menu_item')
	),

Open in new window


      the values of the array are displayed on a page (in a hidden div) using the heredoc declaration with an if to check if the checkbox has been used - if the return is empty the div does not show - if one of the checkboxes is "checked" then it does.

///Cheese selections
if (!empty($cheesebox)) {
          $output .= <<< OUTPUT
            <br />
            <div class="sides">Comes with: <p>{$cheesebox}</p></div>
            <br />
OUTPUT4;
        }

Open in new window


What I need to do is pull any one of the values in the array and if its in the $cheesebox the do something.

Ive tried using an ifelse like this

if ( ('$cheesebox') == "Cheddar" ){
               echo "Your topping is Cheddar";
            }
              	elseif ( ('$cheesebox') == "Gouda" ){
            {
               echo "Your topping is Gouda";
            }
              	elseif ( ('$cheesebox') == "Bleu" ){
            {
               echo "Your topping is Bleu";
            } 

Open in new window

however this does not work - I'm sure I have it wrong somewhere along the line or does the heredoc function only allow for one?

 if so is there a way to accomplish this?
0
Comment
Question by:Daryl Isaacs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41770963
In the second code snippet ("cheese selections") the HEREDOC tags do not match.  There may be other things in play as well, but that jumps out at me.

Here's some background information on how we handle checkboxes.
https://www.experts-exchange.com/articles/5450/Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html

Also, it looks as if you may be using some kind of CMS or framework.  If so, please tell us what it is, thanks.
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41770983
if ( ('$cheesebox') == "Cheddar" ){

Open in new window


In PHP there are three types of strings
- Single quoted - string gets dumped as is - no parsing of string - single quotes escaped with a \ double quotes allowed as is.
- Double quoted - strings are parsed and variables (optionally enclosed in {})  are replaced with their values. Double quotes escaped with a \ single quotes allowed as is
- HEREDOC - tag enclosed string - allows mixing of single and double quotes without escaping and does variable substitution.

In your example ('$cheesebox') is interpreted as a string - not its value - take the single quotes from around the variable.

You are not showing us where $cheesebox is being set - so we cannot see if your use of the if statement is correct or not.

Alternative to the if is the switch statement

switch ($cheesebox) {
    case 'Cheddar' : 
// Handle Cheddar
        break;
    case 'Gouda' :
// Handle Gouda
    break;
    case 'Bleu':
// Handle Bleu
    break;
    default: 
// Handle default here (when none of the above match)
     break;
}

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41770989
There are many ways of handling HTML form inputs.  Here are some examples.  Try a few combinations to see how it works.  Note that if you make a "default value" be zero, (one of the "falsy" values in PHP) you can test with empty().  It will be true if the input is zero, NULL, FALSE, omitted, etc.
https://iconoun.com/demo/temp_daryl.php
<?php // demo/temp_daryl.php
/**
 * https://www.experts-exchange.com/questions/28965722/Parse-array-then-run-a-if-function-on-result.html
 */
error_reporting(E_ALL);

$cheeses = [ 'Cheddar', 'Gouda', 'Bleu' ];

// PREPARE A SELECT CONTROL FOR CHOOSING A CHEESE
$select = NULL;
$select .= '<select name="select">' . PHP_EOL;
$select .= '<option selected value="0">Choose a cheese</option>' . PHP_EOL;
foreach ($cheeses as $c)
{
    $select .= '<option value="' . $c . '">' . $c . '</option>' . PHP_EOL;
}
$select .= '</select>' . PHP_EOL;

// PREPARE A CHECKBOX FOR CHOOSING ONE OR MORE CHEESES (MAY BE UNDEFINED IN THE REQUEST IF EMPTY)
$checkbox = NULL;
foreach ($cheeses as $c)
{
    $checkbox .= '<input type="checkbox" name="check[]" value="' . $c . '" />' . $c . '<br>' . PHP_EOL;
}

// PREPARE A RADIO PUSHBUTTON FOR CHOOSING A CHEESE
$radio = NULL;
$radio .= '<input name="pushbutton" value="0" type="hidden" />' . PHP_EOL;
foreach ($cheeses as $c)
{
    $radio .= '<input type="radio" name="pushbutton" value="' . $c . '" />' . $c . '<br>' . PHP_EOL;
}


// IF THE FORM HAS BEEN SUBMITTED SHOW THE REQUEST VARIABLES
if (!empty($_POST))
{
    echo PHP_EOL . "SELECT:<br>" . PHP_EOL;
    print_r($_POST['select']);
    echo '<br><br>' . PHP_EOL;

    echo PHP_EOL . "CHECKBOX:<br>" . PHP_EOL;
    print_r($_POST['check']);
    echo '<br><br>' . PHP_EOL;

    echo PHP_EOL . "RADIO:<br>" . PHP_EOL;
    print_r($_POST['pushbutton']);
    echo '<br><br>' . PHP_EOL;
}


// CREATE THE FORM
$form = <<<EOD
<form method="post">
$select
<br><br>
$checkbox
<br><br>
$radio
<br><br>
<input type="submit" />
</form>
EOD;

echo $form;

Open in new window

0
 

Author Comment

by:Daryl Isaacs
ID: 41772406
Both of these examples work - they display the echo associated to the value name - this is based on what both Ray and Julian wrote.... I'm leaning toward the first as I can then add other divs around that particular echo.

if ($cheesebox == "Cheddar") {
echo "Your topping is Cheddar";
} else if ($cheesebox == "Gouda") {
echo "Your topping is Gouda";
}  else if ($cheesebox == "Bleu") {
echo "Your topping is Bleu";
}


if (in_array($cheesebox, ['Cheddar', 'Gouda', 'Bleu'])) {
echo "Your side order is {$cheesebox}";
}

The single and double quotes from what I understood was (in my mind) looking for a literal value in '$cheesebox' then comparing it to  whatever that was in == ""

I guess I got that round wrong

I am still on the same thing now - even though the echo works - its outside of the heredoc OUTPUT4 and throws the echo to the top of the page. Can this work inside the OUTPUT4?

I've never worked with the heredoc class before, so if this seems rudimentary, please be lenient with me...
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41772502
Here are some good learning resources for those of us new to PHP.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

Here is how quote marks work in PHP.
https://www.experts-exchange.com/articles/12241/Quotation-Marks-in-PHP.html

The "OUTPUT4" thing is a problem, as was noted above.  PHP Heredoc is described here.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

In particular, the starting and ending tags must match for heredoc to work.

Let's try this one again: It looks as if you may be using some kind of CMS or framework.  If so, please tell us what it is, thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question