xml - PHP - creating a dynamically coded if statement -
i trying build if statement dynamically coded based on values submitted users visit site. if statement may have between 1 , 9 conditions test (depending on user input), , xml values (from xml document) displayed based on if statement.
the potential if statement conditions inserted in $if_statement variable, 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 methods retrieving potential user input $if_statement variable.
however nothing being displayed in foreach loop below when using dynamically coded if statement:
$xmlproducts = simplexml_load_file("products.xml"); foreach($xmlproducts->product $product) { if ($if_statement) { // problem lies here, because results displayed when if statement removed echo $product->name; }}
any advice? or there better way dynamically code if statement?
your $if_statement evaluated @ runtime before there actual product evaluate. you'll need change code pass product during foreach cycle , 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 $product) { if (if_statement($product,$keyword, $store )) { echo $product->name; } }
by way. take @ php's native filter_input. evaluating user input without sanitizing.
Comments
Post a Comment