ayoksus Posted December 12, 2012 Share Posted December 12, 2012 Hi, I'm trying to create a news page base on this xml from other site. <?xml version="1.0" encoding="UTF-8"?> <viewentries toplevelentries="0"> <viewentry position="1" unid="D31B1450A5704018C1257ACF00424EB8" noteid="62BE" siblings="0" date="2012-12-09"> <entrydata columnnumber="0" name="Date"> <text>2012-12-09</text> </entrydata> <entrydata columnnumber="1" name="header"> <text>Title 1</text> </entrydata> <entrydata columnnumber="2" name="intro"> <text>This is the intro tekst 1</text> </entrydata> <entrydata columnnumber="3" name="content"> <text>The content of the newsfeed is here. This is the first content.</text> </entrydata> <entrydata columnnumber="4" name="imagetag"> <text>http://www.feedsite.com/767D50B7A55028E7C1257AC2007C5A5F/$file/200401660-001.jpg</text> </entrydata> </viewentry> <viewentry position="2" unid="C90F4979710F1F88C1257ACE005EDC4A" noteid="62BA" siblings="0" date="2012-12-08"> <entrydata columnnumber="0" name="Date"> <text>2012-12-08</text> </entrydata> <entrydata columnnumber="1" name="header"> <text>Title 2</text> </entrydata> <entrydata columnnumber="2" name="intro"> <text>This is the intro tekst 2</text> </entrydata> <entrydata columnnumber="3" name="content"> <text>The content of the newsfeed is here. This is the second content.</text> </entrydata> <entrydata columnnumber="4" name="imagetag"> <text>http://www.feedsite.com/9FB08DD8D84A6ABDC1257ACA003A49E2/$file/iPad.jpg</text> </entrydata> </viewentry> </viewentries> I use DOMDocument to retrieve the data's from this XML. First I display the titles, when it's clicked it displays the chosen news. However, I don't know the most effective way so that I don't need to do to many foreach loops which slows the pages. <?php $objDOM2 = new DOMDocument(); $objDOM2->load("https://www.feedsite.com/news?openagent&cnt=10&tkl=1"); $viewentry = $objDOM2->getElementsByTagName("viewentry"); $i = 0; foreach( $viewentry as $value ){ $unid = $viewentry->item($i)->getAttribute("unid"); $date = $value->getElementsByTagName("entrydata"); $date = trim($date->item(0)->nodeValue); $title = $value->getElementsByTagName("entrydata"); $title = trim($title->item(1)->nodeValue); $head = $value->getElementsByTagName("entrydata"); $head = trim($kopje->item(2)->nodeValue); $text = $value->getElementsByTagName("entrydata"); $text = nl2br(trim($text->item(3)->nodeValue)); $img = $value->getElementsByTagName("entrydata"); $img = trim($img->item(4)->nodeValue); if($_GET['id'] === $unid){ ?> <div class="popup"> <h2><?php echo $title ?></h2> <?php if($img != 'http://www.fiscanet.nl'){ echo "<img src='".$img."' class='newsImg' />"; } echo "<div class='intro'><strong>".$date; echo "</strong> - ".$head."</div>"; echo "<div class='content'>".$text."</div>"; break; ?> </div> <?php } $i++; } ?> So I made a loop for each items and match those the $_GET['id'] with the unique id ($unid) to display the chosen news item. However, the page is become so slow to be opened, because of the loops. Especially when there are a lot of news items. And if I make an "other news" list, then it become even slower. I need a faster query for this case. Is there any other way to make it faster? Quote Link to comment https://forums.phpfreaks.com/topic/271897-select-an-xml-element-from-attribute-name/ Share on other sites More sharing options...
Barand Posted December 12, 2012 Share Posted December 12, 2012 Testing if it's the id you want before getting all the child data may help. Quote Link to comment https://forums.phpfreaks.com/topic/271897-select-an-xml-element-from-attribute-name/#findComment-1398912 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.