xml - Variable seems to keep it's initial value in if statement - PHP, SimpleXML

300

I have the following PHP code which should store the index of a single customer matched with an XPath expression in$customerIndexOfMatch and then uses$customerIndexOfMatch to edit the XML values of the customer located at that index.

My issue is that$customerIndexOfMatch gives the expected value but only outside of theif (isset($_POST['updateCustomer'])) statement and I'm not sure why it's doing this.

<?php
$customerIndexOfMatch = -1;

if (isset($_POST['search'])) {
    $searchTerm = $_POST['searchTerm'];
    $searchBy = $_POST['search-by'];
    if (isset($searchBy)) {
        if ($searchBy == "Last Name") {
            $precedingCount = $customersXml->xpath('//customer/customerInfo/lastName[text()="' . $searchTerm . '"]/preceding::customer');
            $ancestorCount = $customersXml->xpath('//customer/customerInfo/lastName[text()="' . $searchTerm . '"]/ancestor-or-self::customer');
            $customerIndexOfMatch = (count($precedingCount) + count($ancestorCount));
        }
        elseif ($searchBy == "Customer Number") {
            $precedingCount = $customersXml->xpath('//customer[@number="' . $searchTerm . '"]/preceding::customer');
            $ancestorCount = $customersXml->xpath('//customer[@number="' . $searchTerm . '"]/ancestor-or-self::customer');
            $customerIndexOfMatch = (count($precedingCount) + count($ancestorCount));
        }
        elseif ($searchBy == "Meter Number") {
            $precedingCount = $customersXml->xpath('//customer[@meterNumber="' . $searchTerm . '"]/preceding::customer');
            $ancestorCount = $customersXml->xpath('//customer[@meterNumber="' . $searchTerm . '"]/ancestor-or-self::customer');
            $customerIndexOfMatch = (count($precedingCount) + count($ancestorCount));
        }
    }
}

// var_dump($customerIndexOfMatch); // not -1, gives expected value
// die();

$fname = someDataFromAForm
$mname = someDataFromAForm
$lname = someDataFromAForm
$address = someDataFromAForm
$ph1 = someDataFromAForm
$ph2 = someDataFromAForm
$ph3 = someDataFromAForm

// var_dump($customerIndexOfMatch); // not -1, gives expected value
// die();

if (isset($_POST['updateCustomer'])) {
    // edit the details and write to file
    $customersXml = simplexml_load_file('customers.xml', null, true) or die('Error: Cannot load XML from file customers.xml"');

    // The value of $customerIndexOfMatch isn't the expected value, therefore I cannot properly write the edited data to file
    // $customerIndexOfMatch represents the index of the customer that is to be edited. $customerIndexOfMatch isn't 0 based.
    // the value of the index before this if statement is correct but in the if statement it is -1

    // var_dump($customerIndexOfMatch); // -1, the default value for $customerIndexOfMatch
    // die();

    /*
    $customersXml->customer[$customerIndexOfMatch]->customerInfo->firstName = $fname;
    $customersXml->customer[$customerIndexOfMatch]->customerInfo->middleName = $mname;
    $customersXml->customer[$customerIndexOfMatch]->customerInfo->lastName = $lname;
    $customersXml->customer[$customerIndexOfMatch]->customerInfo->address = $address;
    $customersXml->customer[$customerIndexOfMatch]->customerInfo->telephone[0] = $ph1;
    $customersXml->customer[$customerIndexOfMatch]->customerInfo->telephone[1] = $ph2;
    $customersXml->customer[$customerIndexOfMatch]->customerInfo->telephone[2] = $ph3;
    $customersXml->asXML('customers.xml')
    */
}

// the html form where someDataFromAForm comes from

Are there any reasons as to why$customerIndexOfMatch keeps it's initial value when in theif (isset($_POST['updateCustomer'])) block, but before the block it has it's expected value? What options do I have to fix this issue? Still fairly new to PHP, can't see why it's doing this.

Any help is appreciated!

People are also looking for solutions to the problem: php - Find the first appearance of a number in associative array

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.