friedice Posted May 18, 2010 Share Posted May 18, 2010 um im having trouble with practising with DOm in xml in command prompt when u type-> php DOMSearch.php "Brookside" its prints out Channel 5 Start : 2001-07-0521:55:00 but it should print all start times and channel with series brookside my script is only goin through the document once and only returnin one result can anyone tell me or lead me on how to keep loopin through and print all when series matches wat the user entered cheers <?php $objDOM = new DOMDocument(); $objDOM->load('TVGuide.xml'); $series = $argv[1]; $channel = $objDOM->getElementsByTagName("Channel"); $chan = $channel->item(0)->nodeValue; foreach( $channel as $value ) { $names = $value->getElementsByTagName("Name"); $name = $names->item(0)->nodeValue; $program = $objDOM->getElementsByTagName("Program"); foreach( $program as $prog ) { $starts = $prog->getElementsByTagName("Start"); $start = $starts->item(0)->nodeValue; $serie = $prog->getElementsByTagName("Series"); $ser = $serie->item(0)->nodeValue; } } if($series == $ser) { echo "$name\n"; echo "Start : $start\n"; } else { echo "Series '$series' not found.\n"; } ?> <?xml version="1.0" encoding="ISO-8859-1"?> <TVGuide start="2001-07-05" end="2001-07-05"> <Channel> <Name>BBC1</Name> <Program> <Start>2001-07-05T19:00:00</Start> <Duration>PT30M</Duration> <Series>QuestionOfSport</Series> <Title></Title> </Program> <Program rating="5" flag="favorite"> <Start>2001-07-05T19:30:00</Start> <Duration>PT30M</Duration> <Series>EastEnders</Series> <Title></Title> <Description> Mark's health scare forces him to reconsider his future with Lisa, while Jamie is torn between Sonia and Zoe. </Description> <CastList> <CastMember> <Character><Name>Zoe Slater</Name></Character> <Actor><Name>Michelle Ryan</Name></Actor> </CastMember> <CastMember> <Character><Name>Jamie Mitchell</Name></Character> <Actor><Name>Jack Ryder</Name></Actor> </CastMember> <CastMember> <Character><Name>Sonia Jackson</Name></Character> <Actor><Name>Natalie Cassidy</Name></Actor> </CastMember> </CastList> <Writers> <Writer><Name>Nick Saltrese</Name></Writer> <Writer><Name>Julie Wassmer</Name></Writer> </Writers> <Director><Name>Stewart Edwards</Name></Director> <Producer><Name>Emma Turner</Name></Producer> </Program> <Program type="documentary"> <Start>2001-07-05T20:00:00</Start> <Duration>PT45M</Duration> <Series></Series> <Title>Get Real with Casualty</Title> </Program> <Program> <Start>2001-07-05T20:45:00</Start> <Duration>PT45M</Duration> <Series>Lottery</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T21:00:00</Start> <Duration>PT1H</Duration> <Series>Panorama</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T22:00:00</Start> <Duration>PT45M</Duration> <Series>Lottery</Series> <Title></Title> </Program> </Channel> <Channel> <Name>BBC2</Name> <Program> <Start>2001-07-05T19:00:00</Start> <Duration>PT1H</Duration> <Series>Snooker</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T20:00:00</Start> <Duration>PT1H</Duration> <Series>HomeFront</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T21:00:00</Start> <Duration>PT50M</Duration> <Series>WildAfrica</Series> <Title></Title> </Program> <Program flag="interesting"> <Start>2001-07-05T21:50:00</Start> <Duration>PT40M</Duration> <Series>Taboo</Series> <Title>Nakedness</Title> </Program> <Program> <Start>2001-07-05T22:30:00</Start> <Duration>PT45M</Duration> <Series>Lottery</Series> <Title></Title> </Program> </Channel> <Channel> <Name>ITV</Name> <Program> <Start>2001-07-05T19:00:00</Start> <Duration>PT30M</Duration> <Series>Emmerdale</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T19:30:00</Start> <Duration>PT30M</Duration> <Series>CoronationStreet</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T20:00:00</Start> <Duration>PT1H</Duration> <Series>Millionaire</Series> <Title></Title> </Program> <Program type="drama"> <Start>2001-07-05T21:00:00</Start> <Duration>PT2H</Duration> <Series></Series> <Title>Hot Money</Title> </Program> </Channel> <Channel> <Name>Channel 4</Name> <Program> <Start>2001-07-05T19:00:00</Start> <Duration>PT55M</Duration> <Series>Channel4News</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T19:55:00</Start> <Duration>PT5M</Duration> <Series>SlotArt</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T20:00:00</Start> <Duration>PT30M</Duration> <Series>Brookside</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T20:30:00</Start> <Duration>PT30M</Duration> <Series>Brookside</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T21:00:00</Start> <Duration>PT1H</Duration> <Series>Swallow</Series> <Title></Title> </Program> <Program flag="favorite"> <Start>2001-07-05T22:00:00</Start> <Duration>PT1H</Duration> <Series>AllyMcBeal</Series> <Title></Title> </Program> </Channel> <Channel> <Name>Channel 5</Name> <Program> <Start>2001-07-05T19:00:00</Start> <Duration>PT30M</Duration> <Series>MovieChartShow</Series> <Title></Title> </Program> <Program> <Start>2001-07-05T19:30:00</Start> <Duration>PT30M</Duration> <Series>5News</Series> <Title></Title> </Program> <Program type="entertainment"> <Start>2001-07-05T20:00:00</Start> <Duration>PT1H</Duration> <Series></Series> <Title>The World's Worst Drivers Caught On Tape</Title> </Program> <Program type="film"> <Start>2001-07-05T21:00:00</Start> <Duration>PT1H55M</Duration> <Series></Series> <Title>Black and White</Title> </Program> <Program> <Start>2001-07-05T21:55:00</Start> <Duration>PT30M</Duration> <Series>Brookside</Series> <Title></Title> </Program> </Channel> </TVGuide> Link to comment https://forums.phpfreaks.com/topic/202129-dom-php-help-needed/ Share on other sites More sharing options...
friedice Posted May 19, 2010 Author Share Posted May 19, 2010 anyone can help me? Link to comment https://forums.phpfreaks.com/topic/202129-dom-php-help-needed/#findComment-1060360 Share on other sites More sharing options...
friedice Posted May 19, 2010 Author Share Posted May 19, 2010 help pls Link to comment https://forums.phpfreaks.com/topic/202129-dom-php-help-needed/#findComment-1060481 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 Try if (empty($argv[1])) { echo 'Please provide a search term.'; exit(0); } $dom = new DomDocument(); if (!$dom->load('TVGuide.xml')) { echo 'Failed to load TVGuide.xml'; exit(0); } foreach ($dom->getElementsByTagName('Series') as $serie) { if ($argv[1] == $serie->nodeValue) { foreach ($serie->parentNode->childNodes as $childNode) { echo $childNode->nodeName, ': ', $childNode->nodeValue, PHP_EOL; } echo PHP_EOL; } } Should create something like: Start: 2001-07-05T20:00:00 Duration: PT30M Series: Brookside Title: Start: 2001-07-05T20:30:00 Duration: PT30M Series: Brookside Title: Link to comment https://forums.phpfreaks.com/topic/202129-dom-php-help-needed/#findComment-1060495 Share on other sites More sharing options...
friedice Posted May 19, 2010 Author Share Posted May 19, 2010 thx for dat help is there a ways to print specific element nodes? the output im lookin for would be Channel 4 Start : 2001-07-05T20:00:00 Start : 2001-07-05T20:30:00 Channel 5 Start : 2001-07-05T21:55:00 if u entered Brookside as the series name Link to comment https://forums.phpfreaks.com/topic/202129-dom-php-help-needed/#findComment-1060533 Share on other sites More sharing options...
TheJoey Posted May 19, 2010 Share Posted May 19, 2010 looks like you need a foreach loop for the echo of your results Link to comment https://forums.phpfreaks.com/topic/202129-dom-php-help-needed/#findComment-1060549 Share on other sites More sharing options...
btxlongwish Posted May 19, 2010 Share Posted May 19, 2010 need help Link to comment https://forums.phpfreaks.com/topic/202129-dom-php-help-needed/#findComment-1060605 Share on other sites More sharing options...
friedice Posted May 20, 2010 Author Share Posted May 20, 2010 ya dats wat i was tryin to do but it keep echoing one result =.= Link to comment https://forums.phpfreaks.com/topic/202129-dom-php-help-needed/#findComment-1060929 Share on other sites More sharing options...
friedice Posted May 20, 2010 Author Share Posted May 20, 2010 anyone ? i gettin so annoyed with it atm Link to comment https://forums.phpfreaks.com/topic/202129-dom-php-help-needed/#findComment-1061002 Share on other sites More sharing options...
friedice Posted May 20, 2010 Author Share Posted May 20, 2010 dw i got in printin nearly to wat i want it to print its not exactly but good enough thx for ur help Link to comment https://forums.phpfreaks.com/topic/202129-dom-php-help-needed/#findComment-1061099 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.