0

Having trouble with this little DOM PHP task. When i parse it from unix with the command -> php DOMSearch.php "Brookside"
it prints out

Channel 5
Start : 2001-07-0521:55:00

However, it should print out all channels and start times from the "Brookside" series like below:

Channel 4
Start : 2001-07-05T20:00:00
Start : 2001-07-05T20:30:00
Channel 5
Start : 2001-07-0521:55:00

my script is only going through the document once and only returning one result
can anyone help me how to keep looping through and print channel and start time when series matches what the user has entered.

**** PHP CODE ****

<?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 SOURCE CODE ****

<?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>

Edited by vho123: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by MVied
0

From what I can tell, the code is looping through every time it needs to, you're just overwriting your variables because you re-assign them every time it goes through the loop. You'll notice that the only result returned is the last channel and the last program on that channel. For something like this, I'd probably build an associative array out of it.

I didn't test this, but give it a shot.

<?php
 $result = array();

 $objDOM = new DOMDocument();
 $objDOM->load('TVGuide.xml');
 $channels = $objDOM->getElementsByTagName("Channel");

 $i = 0;
 foreach($channels as $channel) {
  $name = $channel->getElementsByTagName("Name");
  $result[$i]['name'] = $name->item(0)->nodeValue;

  $programs = $channel->getElementsByTagName("Program");
  $j = 0;
  foreach($programs as $progam) {
   $starts = $progam->getElementsByTagName("Start");
   $result[$i]['programs'][$j]['starts'] = $starts->item(0)->nodeValue;
   $series = $progam->getElementsByTagName("Series");
   $result[$i]['programs'][$j]['series'] = $series->item(0)->nodeValue;
   $j++;
  }
  $i++;
 }

 foreach($result as $channel) {
  echo $channel['name']."\n";
  foreach($channel['programs'] as $program) {
   echo "Start: ".$program['starts']."\n";
  }
 }
?>

Edited by MVied: n/a

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.