Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
    
Use XPath to pull the specific products out of the XML. – James Apr 6 '14 at 15:26
up vote 1 down vote accepted

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.

share|improve this answer
    
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. – Dean Olsen Apr 6 '14 at 17:25
    
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. – amenadiel Apr 6 '14 at 19:21
$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) {
...
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.