php - simplexml_load_string foreach doesnt display anything

204

My PHP code looks like below, it shows blank i want to Display all the records with hyperlinks like

<a href="test.php?id=$Id">$Name</a>

where $Id, $Name is from XML feed

  $path = 'www.abc.com/test.xml';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $path);
  curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  $returned = curl_exec($ch);
  curl_close($ch);
  // $xml === False on failure
  $xml = simplexml_load_string($returned);

foreach( $xml->Name as $Name){
   print (string)$Name;
}

MY XML looks like below

<ABCXml version="1.1.0">
  <Manufacturers>
   <Item>
    <Id>219</Id>
    <Name>Matrix Corp</Name>
   </Item>
   <Item>
    <Id>2040</Id>
    <Name>Microcomputer</Name>
   </Item>
 </Manufacturers>
</ABCXml>
137

Answer

Solution:

Your xml object starts with Manufacturers which contains multiple Items which contain the Names and IDs. You need to path out to them.

foreach($xml->Manufacturers->Item as $item) {
    printf("<a href='test.php?id=%s>%s</a>", $item->Id, $item->Name);
}

People are also looking for solutions to the problem: javascript - HTML Special Characters and PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.