Question

I am making a plugin for JomSocial that will display billing information for a logged-in user, based on an XML file. I have made good headway creating the plugin, I just cant seem to get the syntax right to create php statements so I can populate data in various places on the page. Here is the XML file:

<Inquiry>
<Billing>
<Version>4.5.1</Version>
<startTime><![CDATA[4/15/2014 11:09 PM]]></startTime>
<endTime><![CDATA[4/15/2014 11:12 PM]]></endTime>
<Date>20140415</Date>
<MemberId ID="0ESING">
  <BillingInfo>
    <StatementEndDate>20140430</StatementEndDate>
    <BillingSubAccount>
    </BillingSubAccount>
    <BalanceForward>628.32</BalanceForward>
    <BalanceDue>372</BalanceDue>
    <Payments>-300</Payments>
</MemberId>
<MemberId ID="F00421">
</BillingInfo>
<BillingInfo>
<StatementEndDate>20140430</StatementEndDate>
 <BillingSubAccount>
 </BillingSubAccount>
 <BalanceForward>1158.36</BalanceForward>
 <BalanceDue>93.45</BalanceDue>
 <Payments>-1158.36</Payments>

Here is the PHP so far:

$user =& CFactory::getRequestUser();
$cuser = CFactory::getUser();  
$owner = CFactory::getUser($row->user->id); 
$ptype = $cuser->getProfileType();
$billingid = $owner->getInfo('FIELD_BILLINGID');
$lastname = $owner->getInfo('FIELD_FAMILYNAME');
$uname = $cuser->username;
$memid = $cuser->id;
$name = $cuser->getDisplayName();
$isMine = COwnerHelper::isMine($cuser->id, $user->id);
$config = CFactory::getConfig();

$source = file_get_contents('data/201404.xml');
$xml = new SimpleXMLElement($source);
$balance = $xml->Billing->MemberId->BillingInfo->BalanceDue;
$BalanceForward = $xml->Billing->MemberId->BillingInfo->BalanceForward;
$Payments = $xml->Billing->MemberId->BillingInfo->Payments;
ob_start();

if( $isMine ) {
    if($ptype == '2') { 

    if(strcasecmp($uname, $billingid) == 0) {

Then, in page to call the fields:

<?php echo "<div>Balance Due: $". $balance ." | Balance Forward: $" . $BalanceForward . " | Payment: $" . $Payments . "</div>"; ?>

This pulls in the first record of the XML file. I was trying something like this for hours:

$source = file_get_contents('data/201404.xml');
$xml = new SimpleXMLElement($source);
$balance = $xml->Billing->MemberId[.$uname.]->BillingInfo->BalanceDue;
$BalanceForward = $xml->Billing->MemberId[.$uname.]->BillingInfo->BalanceForward;
$Payments = $xml->Billing->MemberId[.$uname.]->BillingInfo->Payments;

to no avail. I would like to 'pull' the child node from the XML where the MemberId ID= "yadayada" is equal to the $uname. I hope I am being clear, this is my first post on Stackoverflow!

Was it helpful?

Solution

Using the square bracket notation accesses the attribute by it's name, so you are asking for member[0ESING] which isn't right because the attribute is named ID.

You can iterate of the members to find the match like so:

foreach($xml->Billing->MemberId as $member){
    if($member['ID'] == $uname){
        $balance = $member->BillingInfo->BalanceDue;
        $BalanceForward = $member->BillingInfo->BalanceForward;
        $Payments = $member->BillingInfo->Payments;
    }
}

OTHER TIPS

Why are you not using simplexml_load_file() instead? It saves you the hassle in loading the file manually and putting it in a simplexml_element instead. Furthermore, I think your issues arrise because the XML file itself is invalid, it need 1 root element but instead seems to contain zero (or multiple, depends on how one would read the file).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top