0

I am trying to build an if statement that is dynamically coded based on the values submitted by the users that visit the site. The if statement may have between 1 and 9 conditions to test (depending on user input), and XML values (from an XML document) will be displayed based on the if statement.

The potential if statement conditions are inserted in the $if_statement variable, like this:

$keyword = trim($_GET["Keyword"]);
if (!empty($keyword)) {
$if_statement = ($keyword == $Product->keyword);
}

$shopByStore = $_GET["store"];
if (!empty($shopByStore)) {
$if_statement = ($if_statement && $shopByStore == $Product->store);
}

// plus 7 more GET methods retrieving potential user input for the $if_statement variable.

However nothing is being displayed in the foreach loop below when using the dynamically coded if statement:

$XMLproducts = simplexml_load_file("products.xml");
foreach($XMLproducts->product as $Product) {

if ($if_statement) { // the problem lies here, because results ARE displayed when this if statement is removed
echo $Product->name;
}}

Any advice? Or is there a better way to dynamically code an if statement?

1
  • Use XPath to pull the specific products out of the XML. Commented Apr 6, 2014 at 15:26

2 Answers 2

1

Your $if_statement is evaluated at runtime before there is any actual Product to evaluate. You'll need to change your code to pass the product during the foreach cycle and then evaluate.

Function declaration:

function if_statement($Product, $keyword=null, $store=null) {
    $if_statement=false;
    if($keyword)  $if_statement = ($keyword == $Product->keyword);
    if($store) $if_statement = $if_statement && ($shopByStore == $Product->store);
    return $if_statement;
}

Function evaluation

$keyword = trim($_GET["Keyword"]);
$shopByStore = $_GET["store"];

$XMLproducts = simplexml_load_file("products.xml");
foreach($XMLproducts->product as $Product) {
    if (if_statement($Product,$keyword, $store )) {
        echo $Product->name;
    }
}

By the way. Take a look at PHP's native filter_input. You are evaluating user input without sanitizing.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! My only concern is that the XML file being parsed is huge, and calling this function on every loop may be strenuous on the system. I was hoping to have an if statement created (with the ability to display it) prior to the foreach loop. Seems like this would be less strenuous with regard to processing the code.
In this scenario, once the XML is loaded in memory the effect of whatever function you apply on each of the Product nodes will have little impact in the overall performance.
0
$names = array('Keyword', 'store', ...);
$if_condition = true;
foreach ($names as $name) {
    if (isset($_GET[$name]))
        $if_condition = $if_condition && $_GET[$name] == $Product->$name;
}
if ($if_condition) {
...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.