Redapple Posted March 6, 2007 Share Posted March 6, 2007 Hello everybody. I got a big problem with a xml loop, This are my files content: news.xml: <?xml version="1.0" encoding="ISO-8859-1" ?> <News> <Result id='2'> <NewsDate>06 March 2007 at 20:51:55</NewsDate> <NewsPoster>Ricardo</NewsPoster> <NewsName>Google launches book search for China</NewsName> <TopicImage>google.gif</TopicImage> <NewsContent><![CDATA[Google has been working on the Chinese book search service since the middle of last year. On March 2, Google launched the latest addition to Google.cn: a trial Web site for testing a book search service in China that allows users to search and read the full text of books. A posting on Google's Chinese blog said the company has so far scanned 1 million books from around the world into its index, but relatively few Chinese books are available: "[i]In China, this mission has just started, we still have a long way to go[/i." The book-search service also provides links to online book sellers that sell the book – users are of course encouraged to use Google's revamped and recently re-launched Chinese map service to then find the address and location of nearby bookstores. In the future, the search engine may also be able to tell users if a bookstore has a particular book in stock.]]></NewsContent> </Result> <Result id='1'> <NewsDate>06 March 2007 at 12:23:03</NewsDate> <NewsPoster>Ricardo</NewsPoster> <NewsName>DOT bans Vista, Office 2007, IE 7 upgrades, at least for now</NewsName> <TopicImage>ms_stop.gif</TopicImage> <NewsContent><![CDATA[The U.S. Department of Transportation has quietly put the kibosh on Windows Vista, Office 2007 and Internet Explorer 7 (IE7), banning upgrades to those Microsoft Corp. products -- at least for now. Donna Seymour, CIO of the DOT's Maritime Administration (MARAD), said a July move of the agency's Washington headquarters is to blame for the reluctance to deploy Microsoft's new software. "It has less to do with technical concerns about Microsoft and more to do with the fact that with our July move, our plates are totally full and we can't take another thing on right now," she said in an interview today after a speech at the Computerworld Premier 100 IT Leaders conference in Palm Desert, Calif.<br><br>MARAD has already begun testing Vista and IE 7, according to Seymour. That testing, however, may take time because MARAD relies extensively on old, custom applications that will require long evaluation on Vista. She has not yet set an upgrade timetable, but if or when she does, Seymour said, she can add Vista and Office without spending additional money; MARAD has a Software Assurance contract with Microsoft. Even if her department doesn't widely deploy Vista and Office 2007 until early 2009, "we would be middle of the pack among private corporations and somewhat ahead of most government agencies," Seymour claimed. ]]></NewsContent> </Result> </News> news.php: $xmlDoc = new DOMDocument(); $xmlDoc->load("includes/information/news.xml"); $x = $xmlDoc->documentElement; foreach ($x->childNodes AS $item) { echo " <table width=\"100%\" class=\"news-table\"> <tr> <td class=\"news-header\"> <div style=\"float:left;\">$newshead</div> </td> </tr> <tr> <td class=\"news-content\"> <img class=\"news-image\" align=\"right\" src=\"includes/site_layout/images/topics/$topicimage\"> <div style=\"float:left;\">$newscontent</div> </td> </tr> <tr> <td> <div style=\"float:left;\">$newsposter - $newsdate</div> </td> </tr> </table> "; } Now, I know that foreach ($x->childNodes AS $item) will display every row with $item->nodeValue but i want the content as in my news.php displayed above here? How can i do that? Link to comment https://forums.phpfreaks.com/topic/41504-solved-php-xml-loop/ Share on other sites More sharing options...
Redapple Posted March 6, 2007 Author Share Posted March 6, 2007 I think its not foreach ($x->childNodes AS $item) or $x = $xmlDoc->documentElement; at all Link to comment https://forums.phpfreaks.com/topic/41504-solved-php-xml-loop/#findComment-201069 Share on other sites More sharing options...
Barand Posted March 6, 2007 Share Posted March 6, 2007 try (php5) <?php $x = simplexml_load_file('news1.xml'); foreach ($x->Result as $item) { echo "<div style=\"float:left; width:25%; margin:5px\">$item->NewsName</div>"; echo "<div style=\"float:left; width:40%; margin:5px\">$item->NewsContent</div>"; echo "<div style=\"float:left; width:25%; margin:5px\">$item->NewsPoster<br>$item->NewsDate</div>"; echo "<br style='clear: both'>"; } ?> Link to comment https://forums.phpfreaks.com/topic/41504-solved-php-xml-loop/#findComment-201105 Share on other sites More sharing options...
Redapple Posted March 6, 2007 Author Share Posted March 6, 2007 It worked like a champ! Do you olso know how to do a limit on how mutch time's a foreachloop may grab something, like 2 times? Link to comment https://forums.phpfreaks.com/topic/41504-solved-php-xml-loop/#findComment-201151 Share on other sites More sharing options...
Barand Posted March 6, 2007 Share Posted March 6, 2007 <?php $a = array (1,2,3,4,5); $limit = 0; foreach ($a as $val) { echo $val, '<br>'; if (++$limit == 2) break; } ?> Link to comment https://forums.phpfreaks.com/topic/41504-solved-php-xml-loop/#findComment-201158 Share on other sites More sharing options...
Redapple Posted March 6, 2007 Author Share Posted March 6, 2007 I get it, Many thanks, 5 Stars for you! Link to comment https://forums.phpfreaks.com/topic/41504-solved-php-xml-loop/#findComment-201163 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.