SimpleXML and php - get a node that contains text that matches another node -
SimpleXML and php - get a node that contains text that matches another node -
i trying display text content of node, if content of node matches node (sibling). here xml:
<memberid> <billinginfo> <details> <line number="0"> <chargedate>20140605</chargedate> <duedate>20140605</duedate> <trackingnumber>pr ded</trackingnumber> <description>account payroll deduction</description> <revcode>sj1550</revcode> <revdesc>account payroll deduction</revdesc> <salesamount>-11.77</salesamount> <servicecharge>0.00</servicecharge> <gst>0.00</gst> <pst>0.00</pst> <othertaxes>0.00</othertaxes> <total>-11.77</total> <chitcode>pr ded</chitcode> <chitdate>20140605</chitdate> </line> <line number="1"> <chargedate>20140605</chargedate> <duedate>20140605</duedate> <trackingnumber>03128862</trackingnumber> <description>mountain mkt sales</description> <revcode>sj1553</revcode> <revdesc>mountain mkt sales</revdesc> <salesamount>8.99</salesamount> <servicecharge>0.00</servicecharge> <gst>0.00</gst> <pst>0.61</pst> <othertaxes>0.00</othertaxes> <total>9.60</total> <chitcode>03128862</chitcode> <chitdate>20140605</chitdate> </line> </details> </billinginfo> <chits> <chit> <code>03128862</code> <body><![cdata[ member#: 0echer name: ******* area: ********* server: jeanneane chit#: 03128862 date: june 5 2014 ------- ------- ------- ------- ------- 1 mingua beef jer 8.99 ------- ------- ------- ------- ------- sub-total 8.99 taxation 0.61 ------- ------- ------- ------- ------- chit total 9.60 fellow member charge -9.60 ]]></body> </chit> </chits> </memberid> i displaying each chitcode , chitdate, because in line[@number] node. want display body node contains text "chit#: xxxxxx" xxxxxx matches line->chitcode. here php, displays body, displays first body line items:
<?php foreach($chitquery $chit) { foreach($linequery $line) { echo "<label class='collapse' for='".$line->chitcode."-".$line->chitdate."'>".$line->description."</label> <input id='".$line->chitcode."-".$line->chitdate."' type='checkbox'> <div>"; // creates hide - show link each description item // start getting matching chit code chit/body if ($chit->code = $line->chitcode) { echo $chit->body."</div>"; } else { echo " ".$line->chitcode."</div>"; } // end if } //end foreach linequery } // end foreach chitquery ?> it's echo $chit->body.""; need help with, want display body contains value of $line->chitcode.
oh yeah, , lines repeating weird, due improper placement of 'foreach' statements! help! in advance!
update: have changed php code , have hide-show divs working, no duplicates, 'array' instead of $chitbody. cannot figure out how find body node contains $line->chitcode in $chitbody = $member->xpath('chits/chit/body[text()="'.$line->chitcode.'"] ');
<?php foreach($linequery $line) { $chitcode = $member->chits->chit->code; $chitbody = $member->xpath('chits/chit/body[text()="'.$line->chitcode.'"] '); echo "<label class='collapse' for='".$line->chitcode."-".$line->chitdate."'>".$line->description."</label> <input id='".$line->chitcode."-".$line->chitdate."' type='checkbox'>"; // creates hide - show link each description item // start getting matching chit code chit/body if ($chitcode = $line->chitcode) { echo "<div>code: ".$chitcode."<br>".$chitbody."</div>"; } else { echo "<div>".$line->chitcode."</div>"; } // end if } //end foreachlinequery ?>
you looking xpath query document.
for illustration iterate on lines - don't understand why way it, here how understand go on lines (text-only output):
$xml = simplexml_load_string($buffer); foreach ($xml->xpath('//details/line') $line) { printf("#%d: %s\n", $line['number'], $line->description); } which gives:
#0: business relationship payroll deduction #1: mount mkt sales i guess see how works in general. getting chit based on $line->chitcode, think more interesting part you. improve illustrates benefits of xpath:
foreach ($xml->xpath('//details/line') $line) { printf("#%d: [%s] %s\n", $line['number'], $line->chitcode, $line->description); $query = sprintf("//chits/chit[code = '%s']/body", $line->chitcode); $result = $xml->xpath($query); if ($result) { $chit = $result[0]; echo $chit; } else { echo ' -- no chit found -- '; } echo "\n"; } this time query contains status number (in case want create save, escape string properly (more info)), creates next 2 xpath queries:
//chits/chit[code = 'pr ded']/body //chits/chit[code = '03128862']/body i 1 time again guess can imagine mean. in case such <body> element found, code above outputs it. , how result looks like:
#0: [pr ded] business relationship payroll deduction -- no chit found -- #1: [03128862] mount mkt sales member#: 0echer name: ******* area: ********* server: jeanneane chit#: 03128862 date: june 5 2014 ------- ------- ------- ------- ------- 1 mingua beef jer 8.99 ------- ------- ------- ------- ------- sub-total 8.99 taxation 0.61 ------- ------- ------- ------- ------- chit total 9.60 fellow member charge -9.60 i hope gives plenty insight started it. xpath() method on simplexmlelement can combine code didn't re-create have illustration smaller (same reason why utilize plain text output here, work as html bloat example).
addition op:
that worked great!!
i adapted code this, , worked perfectly:
foreach ($linequery $line) { $linechitcode = $line->chitcode; $chitbody = sprintf("//chits/chit[code = '%s']/body", $linechitcode); $cbresult = $member->xpath($chitbody); echo "<label class='collapse' for='" . $line->chitcode . "-" . $line->chitdate . "'>" . $line->description . "</label> <input id='" . $line->chitcode . "-" . $line->chitdate . "' type='checkbox'>"; // creates hide - show link each description item // start getting matching chit code chit/body if ($cbresult) { $chit = $cbresult[0]; echo "<div>code: " . $linechitcode . "<br>" . $chit . "</div>"; } else { echo "<div>" . $line->chitcode . "</div>"; } } thank you, saved me 3 days of trying figure out!!
php simplexml xmlnode
Comments
Post a Comment