0

hi all
here is my xml file note.xml file

<?xml version="1.0" encoding="ISO-8859-1"?>
<agents>
    <agent>
    <id>1</id>
    <image> img/primary-nav-logo.png</image>
    <name>Tommy Jenkin</name>
    <company>CJenkins Insurance</company>
    <street>Insurance150 S State Stree</street>
    <city>Linkend</city>
    <phone>(773) 561-4331</phone>
    </agent>
    <agent>
    <id>2</id>
    <image> img/primary-nav-logo.png</image>
    <name>Tommy Jenkin</name>
    <company>CJenkins Insurance</company>
    <street>Insurance150 S State Stree</street>
    <city>Linkend</city>
    <phone>(773) 561-4331</phone>
    </agent>
</agents>

and i have to print xml record of id 1 and i have write code in php like this

<?php
    $xml = simplexml_load_file("note.xml") or die("Error: Cannot create object");
    foreach($xml->xpath('//agent') as $item) { 
    $row = simplexml_load_string($item->asXML());
    $v = $row->xpath('//id[. ="1"]');
    if($v[0]){ 
        print $item->id; 
        print $item->image; 
        print $item->name; 
        print $item->company; 
        print $item->street; 
        print $item->city;
        print $item->phone; 
    }
    else{
        echo 'No records';
    }
?>

please suggest me where

2
Contributors
1
Reply
25
Views
3 Years
Discussion Span
Last Post by pixelsoul
0

You're missing a closing bracket for your foreach loop.

I would do something like this probably

<?php

if (file_exists('note.xml')) {

    $xml = simplexml_load_file("note.xml");
    $i = 0;

    foreach($xml->agent as $agent) {

        if($agent->id == 1){

            print $agent->id;
            print $agent->image;
            print $agent->name;
            print $agent->company;
            print $agent->street;
            print $agent->city;
            print $agent->phone;

            $i++;
        }

    }        
    if($i == 0){
        echo "No records found";
    }

}else{      
    exit('Failed to open file');      
}

?>

Edited by pixelsoul

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.