XML Data Search PHP

Viewed 44

I want to get data by where Id="722",SectionId="1" and ClassNo="1".

When I search for Id and SectionId I get data but I do not know how to include ClassNo.

This is my PHP Code

$students = $xml->xpath("//Student[@Id=" . $studentid . "][@SectionId=" . $sectionid . "]");

This is my XML

    <XML>
<Students>
<Student Id="722" SectionId="1">
            <AdmissionNo>1363</AdmissionNo>
            <Name>AADITRI K S</Name>
            <ClassNo>1</ClassNo>
            <Course>Std-VIII</Course>
            <Division>VIII B</Division>
            <GName>Satheesh K S</GName>
            <GMobileno>70707077070</GMobileno>
            <GPhone>8080808</GPhone>
            <HGroup/>
            <Address>Lorem Ipsum</Address>
            <LoginId>1363</LoginId>
            <LoginPwd>15382</LoginPwd>
            <DOB>24/Dec/2006</DOB>
            <BloodGroup/>
            <FatherName>Satheesh K S</FatherName>
            <FatherMobileno/>
            <MotherName>Sini Satheesh</MotherName>
            <MotherMobileno/>
            <Disability/>
            <DisabilityOrderNo/>
            <Height> </Height>
            <Weight> </Weight>
            <VisionL/>
            <VisionR/>
            <Teeth/>
            <OralHygiene/>
            <Ailment/>
            <Terminated>N</Terminated>
            <bus>BUS 4</bus>
            </Student>
            </Students>
            </XML>
2 Answers

You can search through the XML listitems with the indexnumber of the Student in the Students array ( [0] or [1] [2] etc.). Then use an arrow ( -> ) and the name of the key you need:

<?php

$xml = simplexml_load_file('test.xml');
$students = $xml->xpath("//Student[@Id=" . "722" . "][@SectionId=" . "1" . "]");
echo($students[0] -> ClassNo);
echo($students[0] -> Course);
echo($students[0] -> DOB);

?>

I'm not sure exactly what data you are looking for, but if, for example you are looking to get that student's mother's name, try something like this:

$students = $xml->xpath("//Student[@Id=$studentid][@SectionId=$sectionid][./ClassNo[$classno]]/MotherName");
echo $students[0];

Output should be

Sini Satheesh
Related