jakre Posted August 14, 2015 Share Posted August 14, 2015 (edited) Hello, I have a problem with my XML reader which I have just done. The code of this reader is the same as in this video because I am beginner and I made it according to it. This is how the PHP file looks like: <?php $xml = new XMLReader(); $xml->open('members.xml'); $members = array(); while($xml->read()) { if($xml->nodeType == XMLREADER::ELEMENT && $xml->localName == 'member') { $members = array(); $members['id'] = $xml->getAttribute('id'); } if($xml->nodeType == XMLREADER::ELEMENT && $xml->localName == 'firstName') { $xml->read(); $members['firstName'] = $xml->value; } if($xml->nodeType == XMLREADER::ELEMENT && $xml->localName == 'lastName') { $xml->read(); $members['lastName'] = $xml->value; $members[] = $member; } } echo displayMembers($members); function displayMembers($members) { $r = ''; // build it if(count($members) > 0) { $r .= '<table>'; foreach($members as $member) { $r .= '<tr>'; $r .= '<td style="background-color:#eee; padding:3px">' . $member['id'] . '</td>'; $r .= '<td style="background-color:#eee; padding:3px">' . $member['teplotaden'] . '</td>'; $r .= '<td style="background-color:#eee; padding:3px">' . $member['teplotanoc'] . '</td>'; $r .= '</tr>'; } $r .= '</table>'; } return $r; } ?> This reader displays the table with first and last names from the XML file. The problem is that it displays all the names in a table and I want to display only the first name (read only the 3 - 7 line), I mean the content from the first attribute in a XML file: <members> <member id="1"> <firstName>Jim</firstName> <lastName>Smith</lastName> ... </member> <member id="2"> <firstName>Hank</firstName> <lastName>Rogers</lastName> ... </member> ... </members> My actual table looks like this: 1 Jim Smith 2 Hank Rogers ... Thanks for help in advance, jakre Edited August 14, 2015 by jakre Quote Link to comment Share on other sites More sharing options...
jakre Posted August 14, 2015 Author Share Posted August 14, 2015 Bump Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted August 14, 2015 Solution Share Posted August 14, 2015 $x = simplexml_load_file('members.xml'); echo $x->member[0]->firstName, $x->member[0]->lastName; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.