变量似乎在if语句中保持它的初始值 - PHP,SimpleXML

时间:2017-10-23 06:16:20

标签: php xml variables simplexml

我有以下PHP代码,它应该存储与$customerIndexOfMatch中的XPath表达式匹配的单个客户的索引,然后使用$customerIndexOfMatch编辑位于该索引处的客户的XML值。

我的问题是$customerIndexOfMatch给出了预期值,但仅在if (isset($_POST['updateCustomer']))语句之外,我不确定为什么会这样做。

<?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

为什么$customerIndexOfMatchif (isset($_POST['updateCustomer']))块中保持初始值时会有什么原因,但在块之前它具有预期值?我有什么选择来解决这个问题?对PHP来说还是一个新手,看不出为什么会这样做。

感谢任何帮助!

0 个答案:

没有答案