C
C
CMETAHAA2016-03-13 00:13:30
PHP
CMETAHAA, 2016-03-13 00:13:30

How to display additional fields from XML?

I sketched a script, the script works fine, but you need to display more fields 2 fields are the following.
There is an XML TV program file for a week, it displays only one line on a specific channel, but you need to display what will be next 2 ~ 3 programs, what will go on.
XML-TV looks like this.

<programme start="20160312041000" stop="20160312042000" channel="2">
    <title lang="ru">Местное время. Вести-Москва</title>
    <desc lang="ru">Произведено: Россия. Самая свежая и объективная информация обо всех гранях жизни столицы и области.</desc>
    <category lang="ru">Новости</category>
    <credits>
      <director>Екатерина Григорова</director>
      <actor>Екатерина Коновалова</actor>
      <actor>Олег Тонконог</actor>
      <actor>Алексей Фролов</actor>
    </credits>
  </programme>
  <programme start="20160312042000" stop="20160312061500" channel="2">
    <title lang="ru">Когда цветет сирень</title>
    <desc lang="ru">Произведено: Россия. Главная героиня живёт в маленькой квартире со своей пожилой матерью и маленьким сыном. Чтобы содержать семью, она вынуждена много работать и времени на личную жизнь у неё практически не остаётся. Подруга, работающая домработницей в богатой семье, чтобы хоть немного развлечь героиню, зовёт её с собой в гости к друзьям своего жениха...</desc>
    <category lang="ru">Фильм</category>
    <credits>
      <director>Сергей Борчуков</director>
      <actor>Людмила Курепова</actor>
      <actor>Елена Захарова</actor>
      <actor>Антон Макарский</actor>
      <actor>Александр Пороховщиков и другие</actor>
    </credits>
    <date>2010</date>
  </programme>

And the script is like this
$G = date ("G") - 1;
$date =  date("Ymd".$G."is");
$channel = 3;

$tv = new SimpleXMLElement ("xmltv.xml", 0, true);

foreach ($tv as $v)
{
    if ($date >= $v->attributes()->start && $date <= $v->attributes()->stop )
    {   
        if ($v->attributes()->channel == $channel)
        { 
      echo "<h2>".$v->title." (".$v->category.")</h2>";
            echo "<h3>".$v->desc."</h3><br />";   
        }
    }
}

program start="2016-03-12-04-10-00"
What kind of numbers I will explain, this is a year-month-day-hour-minute-second

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-03-13
@wielski

$found = false;
foreach ($tv as $i => $v)
{
    if (!$found && ($date >= $v->attributes()->start && $date <= $v->attributes()->stop ))
    {   
        if ($v->attributes()->channel == $channel)
        { 
            echo "<h2>".$v->title." (".$v->category.")</h2>";
            echo "<h3>".$v->desc."</h3><br />";   
            $found = $i;
        }
    } elseif($i - $found <= 2) {
        if ($v->attributes()->channel == $channel)
        { 
             echo "<h2>".$v->title." (".$v->category.")</h2>";
             echo "<h3>".$v->desc."</h3><br />";   
         }
    } else {
         break;
    }
}

But that's shitty code... You'd better completely rewrite the inference logic.
Group by channel first, creating a collection of programs. Then go through the loop, when you find the desired program, print the next two (by index).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question