Search xml file and fill HTML table with result in php -
hello every body have xml file showing in html table using simplexml library. want search file records using elements values. here xml file content.
<?xml version="1.0" encoding="iso-8859-1"?> <studentslist> <student> <student_id email="test@yahoo.com">18700</student_id> <firstname>jhon</firstname> <lastname>smith</lastname> <address>dragon vally china</address> </student> <student> <student_id email="leesin@gmail.com">18701</student_id> <firstname>lee</firstname> <lastname>sin</lastname> <address>league of legend uk</address> </student> </stuedntslist>
i using php right able find specified student searched value of firstname or lastname. have no clue how reset current html table records , fill result of searched value. using following php code searching.
$firstname = "lee"; $searched_stu_fname = $xml->xpath("//studentslist/student/firstname[contains(text(),'$firstname')]/parent::*"); if(count($searched_stu_fname) > 0) { print_r ($searched_stu_fname); echo "found student"; }
print statement give following result:
array ( [0] => simplexmlelement object ( [student_id] => 18701 [firstname] => lee [lastname] => sin [address] => league of legend uk ) )
now want reload html table record right having students in xml file showing there.
i need ideas or guidance suggestions in advance , sorry bad english. in php loading file using these line of codes
$file = file_get_contents('student.xml'); $xml = simplexml_load_string($file);
in html showing in table
<table> <?php foreach($xml->student $student) :?> <tr> <td><?php echo $student->student_id.'('.$student->student_id['email'].')'; ?></td> <td><?php echo $student->firstname; ?></td> <td><?php echo $student->lastname; ?></td> <td><?php echo $student->address; ?></td> </tr> <?php endforeach; ?> </table>
as far understand, when print students it's list of objects 1 property student. when want print 1 student filter it's array of student objects.
that's why loop this:
<table> <?php foreach($searched_stu_fname $student) :?> <tr> <td><?php echo $student->student_id.'('.$student->student_id['email'].')'; ?></td> <td><?php echo $student->firstname; ?></td> <td><?php echo $student->lastname; ?></td> <td><?php echo $student->address; ?></td> </tr> <?php endforeach; ?> </table>
Comments
Post a Comment