2

I tried to make a PHP file that could search for specific fish in an XML document. This is what I wrote:

$FISH_NAME = $_POST["fish_name"];
$LOCATION = $_POST["location"];
$STATUS = $_POST["status"];
$EDIBLE = $_POST["edible"];
$SCS_NAME = $_POST["scs_name"];
$COLOR = $_POST["color"];
$FEATURE = $_POST["feature"];


$varsPassed = array(
    $FISH_NAME => "NAME", 
    $LOCATION => "@LOCATION", 
    $STATUS => "STATUS", 
    $EDIBLE => "EDIBLE", 
    $SCS_NAME => "SCS-NAME", 
    $COLOR => "COLORS/COLOR", 
    $FEATURE => "FEATURES/FEATURE"
);

$conditional = "";

function addToCond($varPassed, $tagName) {
    if (!empty($varPassed)) {
        if(!empty($conditional)) {
            $conditional .= " and {$tagName} = '{$varPassed}'";
        }
        else {
            $conditional = "{$tagName} = '{$varPassed}'";
        }
    }
}

foreach($varsPassed as $current_feature => $current_tag_name) {
    addToCond($current_feature, $current_tag_Name);
    echo "{$current_feature}: {$current_tag_name}\n";
}

$query = "/FISHDICT/FISH[@LOCATION = 'REEF']";
echo $query;

$dom = new DOMDocument();
$dom->load('server/fish.xml');
$xpath = new DOMXpath($dom);
$value = $xpath->evaluate($query);
foreach($value as $item) {
    echo "found:  {$item->nodeValue}\n\n";
}

But it doesn't work. I get this echoed on the webpage:

Angelfish: NAME : FEATURES/FEATURE /FISHDICT/FISH[@LOCATION = 'REEF']

Does anybody know why it doesn't loop through each post variable?

XML:

    <FISH location="REEF">
    <NAME>Angelfish</NAME>
    <COLORS>
        <COLOR> Blue </COLOR>
        <COLOR> Green </COLOR>
    </COLORS>
    <FEATURES> 
        <FEATURE> Round head </FEATURE>
        <FEATURE> Crown on head </FEATURE>
        <FEATURE> Tall tail fin </FEATURE>
    </FEATURES>
    <TAGS> 
        <TAG> REEF-FISH </TAG>
        <TAG> MULTICOLORED </TAG>
    </TAGS>
    <STATUS> ENDANGERED </STATUS>
    <DESCRIPTION> This fish is found in reefs and is common throughout Bermuda...</DESCRIPTION>
    <FISHINGLEGAL> FALSE </FISHINGLEGAL> 
    <DIET> 
        <DIETITEM> Algae </DIETITEM>
        <DIETITEM> Sea Plants </DIETITEM>
    </DIET>
    <EDIBLE> FALSE </EDIBLE> 
    <SCS-NAME> Holacanthus ciliaris </SCS-NAME>
</FISH>
1
  • Your script works for me when I input static values. Are you sure your POST names are correct? Also not sure if your expecting to use $conditional in the future but current code it won't have anything in. Commented Apr 5, 2015 at 22:02

1 Answer 1

2

From your output the $varsPassed array clearly doesn't contain what you expect. To debug I would do print_r($_POST); at the top of the file to see if the post values are what you expect. Then possibly a print_r($varsPassed); after you create that array to make sure it is populated as expected.

By the way, your function addToCond() is attempting to append values to a variable not inside the scope of the function, $conditional. You should change the function to either return a value which gets appended to $conditional in the calling code, or pass by reference $conditional into the function.

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

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.