Hi all,

I need your help, I'm working on my php to get the list of contents from my get-listing.php script so I can generating the XML to output the contents into the XML.

I have got a little bit of problem with output the contents. I'm still getting the same contents in the XML output for each channel when I generating the XML.

Here is the XML:

   <tv generator-info-name="www.mysite.com/myXML">
    <channel id="101 ABC FAMILY">
    <display-name>101 ABC FAMILY</display-name>
    <programme channel="101 ABC FAMILY" start="20140418160000" stop="20140418163000">
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
    </programme>
    <programme channel="101 ABC FAMILY" start="20140418163000" stop="20140418170000">
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
    </programme>
    <programme channel="101 ABC FAMILY" start="20140418170000" stop="20140418173000">
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
    </programme>
    </channel>

    <channel id="102 CBS">
    <display-name>102 CBS</display-name>
    <programme channel="102 CBS" start="20140418160000" stop="20140418163000">
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
    </programme>
    <programme channel="102 CBS" start="20140418163000" stop="20140418170000">
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
    </programme>
    <programme channel="102 CBS" start="20140418170000" stop="20140418173000">
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
    </programme>

Here's the ABC Family programme info:

4:00 PM - Boy Meets World - Home

4:30 PM - Boy Meets World - My Best Friend's Girl

5:00 PM - The Middle - The Big Chill

5:30 PM - The Middle - Friends, Lies and Videotape

Here's the CBS programme info:

4:00 PM - The Talk

5:00 PM - Let's Make a Deal

6:00 PM - Local Programming

8:30 PM - CBS Evening News With Scott Pelley

As you can see that on both channel programme info, it have got different time in each array. On mine it will show the same programme time for both channels when I'm generating the XML.

Here is what it supposed to be:

<tv generator-info-name="www.mysite.com/myXML">
<channel id="101 ABC FAMILY">
<display-name>101 ABC FAMILY</display-name>
<programme channel="101 ABC FAMILY" start="20140418160000" stop="20140418163000">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140418163000" stop="20140418170000">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="101 ABC FAMILY" start="20140418170000" stop="20140418173000">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</channel>

<channel id="102 CBS">
<display-name>102 CBS</display-name>
<programme channel="102 CBS" start="20140418160000" stop="20140418170000">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="102 CBS" start="20140418170000" stop="20140418180000">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
<programme channel="102 CBS" start="20140418180000" stop="20140418203000">
<title lang="en"></title>
<sub-title lang="en"></sub-title>
<desc lang="en"></desc>
<category lang="en"></category>
</programme>
</tv>

Here is the PHP:

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;
include ('simple_html_dom.php');

function getUrlFriendlyString($str)
{
  // convert spaces to '-', remove characters that are not alphanumeric
  // or a '-', combine multiple dashes (i.e., '---') into one dash '-'.
  $str = ereg_replace("[-]+", "-", ereg_replace("[^a-z0-9-]", "",
  strtolower( str_replace(" ", "-", $str) ) ) );
  return $str;
}

function getPaddedHour($hour, $meridian) 
{
  $ret = $hour;
  if ($meridian == 'AM' && $ret == 12) 
  { 
    $ret = 0;
  } 
  else if ($meridian == 'PM' && $ret != 12) 
  { 
    $ret += 11;
  }
  if ($ret < 10) 
  {
    $ret = '0'.$ret;
  }
  return $ret;
}
  $xml .= '<?xml version="1.0" encoding="UTF-8" ?>';
  $xml .= '
<tv generator-info-name="www.testbox.elementfx.com/test">';
  $base1 = "http://www.mysite.com/get-listing.php";
  $html = file_get_html($base1);        
  //echo $xml;

foreach($html->find('p[id=links]') as $element)
{
  $program_list[ $count ] = array();
  $id_split = explode("?", $element->plaintext);
  $id_split = explode("&", $link_split[1]);
  $channels = explode("channels=",$id_split[0]);
  $channels = $channels[1];
  $id = explode("id=",$id_split[1]);
  $id = $id[1];

  //channels
  //$channel_test = $html->find('p[id=channels]', 10)->plaintext;
  //echo $channel_test;

  $program_list[ $count ]['channels'] = $channels;
  $program_list[ $count ]['id'] = $id;

  $channels_split = explode("?", $element->plaintext);
  $channels_split = explode("&", $channels_split[1]);
  $channel_id = explode("channels=",$channels_split[0]);
  $channel_id = $channel_id[1];
  $my_id = explode("id=",$channels_split[1]);
  $my_id = $my_id[1];

  $channel = urlencode($channel_id);
  $id_1 = urlencode($my_id);
  $html_two = file_get_html("http://www.mysite.com/get-listing.php?channels=" . $channel . "&id=" . $my_id);

  //time1
  $time1 = $html_two->find('span[id=time1]',0)->plaintext;
  $title1 = $html_two->find('span[id=title1]',0)->plaintext;

  $hoursMinutes = explode(":", $time1[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];

  $time1 = explode(" ", $time1);
  $hoursMinutes = explode(":", $time1[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];

  if($time1[1] == "PM")
  {
    $time1[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
  } 
  else 
  {
    if($hours=="12")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "00" . $minutes . "00";
    }
    else if($hours=="1")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "01" . $minutes . "00";
    }
    else if($hours=="2")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "02" . $minutes . "00";
    }
    else if($hours=="3")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "03" . $minutes . "00";
    }
    else if($hours=="4")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "04" . $minutes . "00";
    }
    else if($hours=="5")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "05" . $minutes . "00";
    }
    else if($hours=="6")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "06" . $minutes . "00";
    }
    else if($hours=="7")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "07" . $minutes . "00";
    }
    else if($hours=="8")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "08" . $minutes . "00";
    }
    else if($hours=="9")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "09" . $minutes . "00";
    }
    else if($hours=="10")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "10" . $minutes . "00";
    }
    else if($hours=="11")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "11" . $minutes . "00";
    }
    else if($hours=="24")
    {
      $time1[0] = date('Ymd', strtotime(' +1 day')) . "00" . $minutes . "00";
    }
    else
    {
      $time1[0] = date("Ymd") . $hours . $minutes . "00";
    }
  }

  $program_list[$count]['start_time1'] = $time1[0];
  $program_list[$count]['title1'] = $title1;

  //time2
  $time2 = $html_two->find('span[id=time2]',0)->plaintext;
  $title2 = $html_two->find('span[id=title2]',0)->plaintext;

  $time2 = explode(" ", $time2);
  $hoursMinutes = explode(":", $time2[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];

  if($time2[1] == "PM")
  {
    $time2[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
  } 
  else 
  {
    if($hours=="12")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "00" . $minutes . "00";
    }
    else if($hours=="1")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "01" . $minutes . "00";
    }
    else if($hours=="2")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "02" . $minutes . "00";
    }
    else if($hours=="3")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "03" . $minutes . "00";
    }
    else if($hours=="4")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "04" . $minutes . "00";
    }
    else if($hours=="5")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "05" . $minutes . "00";
    }
    else if($hours=="6")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "06" . $minutes . "00";
    }
    else if($hours=="7")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "07" . $minutes . "00";
    }
    else if($hours=="8")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "08" . $minutes . "00";
    }
    else if($hours=="9")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "09" . $minutes . "00";
    }
    else if($hours=="10")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "10" . $minutes . "00";
    }
    else if($hours=="11")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "11" . $minutes . "00";
    }
    else if($hours=="24")
    {
      $time2[0] = date('Ymd', strtotime(' +1 day')) . "00" . $minutes . "00";
    }
    else
    {
      $time2[0] = date("Ymd") . $hours . $minutes . "00";
    }
  }

  $program_list[$count]['end_time1'] = $time2[0];
  $program_list[$count]['start_time2'] = $time2[0];
  $program_list[$count]['title2'] = $title2;

  //time3
  $time3 = $html_two->find('span[id=time3]',0)->plaintext;
  $title3 = $html_two->find('span[id=title3]',0)->plaintext;

  $time3 = explode(" ", $time3);
  $hoursMinutes = explode(":", $time3[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];

  if($time3[1] == "PM")
  {
    $time3[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
  } 
  else 
  {
    if($hours=="12")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "00" . $minutes . "00";
    }
    else if($hours=="1")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "01" . $minutes . "00";
    }
    else if($hours=="2")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "02" . $minutes . "00";
    }
    else if($hours=="3")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "03" . $minutes . "00";
    }
    else if($hours=="4")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "04" . $minutes . "00";
    }
    else if($hours=="5")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "05" . $minutes . "00";
    }
    else if($hours=="6")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "06" . $minutes . "00";
    }
    else if($hours=="7")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "07" . $minutes . "00";
    }
    else if($hours=="8")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "08" . $minutes . "00";
    }
    else if($hours=="9")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "09" . $minutes . "00";
    }
    else if($hours=="10")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "10" . $minutes . "00";
    }
    else if($hours=="11")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "11" . $minutes . "00";
    }
    else if($hours=="24")
    {
      $time3[0] = date('Ymd', strtotime(' +1 day')) . "00" . $minutes . "00";
    }
    else
    {
      $time3[0] = date("Ymd") . $hours . $minutes . "00";
    }
  }

  $program_list[$count]['end_time2'] = $time3[0];
  $program_list[$count]['start_time3'] = $time3[0];
  $program_list[$count]['title3'] = $title3;

  //time4
  $time4 = $html_two->find('span[id=time4]',0)->plaintext;
  $title4 = $html_two->find('span[id=title4]',0)->plaintext;

  $time4 = explode(" ", $time4);
  $hoursMinutes = explode(":", $time4[0]);
  $hours = $hoursMinutes[0];
  $minutes = $hoursMinutes[1];

  if($time4[1] == "PM")
  {
    $time4[0] = date("Ymd") . ((int)($hours) + 12) . $minutes . "00";
  } 
  else 
  {
    if($hours=="12")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "00" . $minutes . "00";
    }
    else if($hours=="1")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "01" . $minutes . "00";
    }
    else if($hours=="2")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "02" . $minutes . "00";
    }
    else if($hours=="3")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "03" . $minutes . "00";
    }
    else if($hours=="4")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "04" . $minutes . "00";
    }
    else if($hours=="5")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "05" . $minutes . "00";
    }
    else if($hours=="6")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "06" . $minutes . "00";
    }
    else if($hours=="7")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "07" . $minutes . "00";
    }
    else if($hours=="8")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "08" . $minutes . "00";
    }
    else if($hours=="9")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "09" . $minutes . "00";
    }
    else if($hours=="10")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "10" . $minutes . "00";
    }
    else if($hours=="11")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "11" . $minutes . "00";
    }
    else if($hours=="24")
    {
      $time4[0] = date('Ymd', strtotime(' +1 day')) . "00" . $minutes . "00";
    }
    else
    {
      $time4[0] = date("Ymd") . $hours . $minutes . "00";
    }
  }

  $program_list[$count]['end_time3'] = $time4[0];
  $program_list[$count]['start_time4'] = $time4[0];
  $program_list[$count]['title4'] = $title4;

  if (strpos($channel, '+') !== false)
  {
    $channel = str_replace('+', ' ', $channel);
  }

  //program 1#
  $xml .= "
  <channel id='" . $my_id. " " . $channel . "'>";
  $xml .= "
    <display-name>" . $my_id. " " . $channel; 
  $xml .= "</display-name>";
  $xml .= "
  <programme channel='" . $my_id. " " . $channel . "' start='" . $program_list[$i]['start_time1'] . "' stop='" . $program_list[$i]['end_time1'] . "'>";
  $xml .= '
    <title lang="en">';
  $xml .= ' </title>';
  $xml .= '
    <sub-title lang="en">';
  $xml .= ' </sub-title>';
  $xml .= '
    <desc lang="en">';
  $xml .= ' </desc>';
  $xml .= '
    <category lang="en">';
  $xml .= ' </category>';
  $xml .= "
  </programme>";


  //program 2#
  $xml .= "
  <programme channel='" . $my_id. " " . $channel . "' start='" . $program_list[$i]['start_time2'] . "' stop='" . $program_list[$i]['end_time2'] . "'>";
  $xml .= '
    <title lang="en">';
  $xml .= ' </title>';
  $xml .= '
    <sub-title lang="en">';
  $xml .= ' </sub-title>';
  $xml .= '
    <desc lang="en">';
  $xml .= ' </desc>';
  $xml .= '
    <category lang="en">';
  $xml .= ' </category>';
  $xml .= '
  </programme>';


  //program 3#
  $xml .= "
  <programme channel='" . $my_id. " " . $channel . "' start='" . $program_list[$i]['start_time3'] . "' stop='" . $program_list[$i]['end_time3'] . "'>";
  $xml .= '
    <title lang="en">';
  $xml .= ' </title>';
  $xml .= '
    <sub-title lang="en">';
  $xml .= ' </sub-title>';
  $xml .= '
    <desc lang="en">';
  $xml .= ' </desc>';
  $xml .= '
    <category lang="en">';
  $xml .= ' </category>';
  $xml .= '
  </programme>';


  //program 4#
  $xml .= "
  <programme channel='" . $my_id. " " . $channel . "' start='" . $program_list[$i]['start_time4'] . "' stop='" . $program_list[$i]['end_time4'] . "'>";
  $xml .= '
    <title lang="en">';
  $xml .= ' </title>';
  $xml .= '
    <sub-title lang="en">';
  $xml .= ' </sub-title>';
  $xml .= '
    <desc lang="en">';
  $xml .= ' </desc>';
  $xml .= '
    <category lang="en">';
  $xml .= ' </category>';
  $xml .= '
  </programme>';
}
  $xml .= '
  </tv>';
  // output the xml to the browser in this example, write $xml to a file here...
  header("Content-Type: text/xml");
  echo $xml;
  $handle = fopen("myChannel.xml", "w"); 
  fwrite ($handle, $xml);
?>

Does anyone know how I can get the list of contents for each channel when I'm generating the XML without output the same contents on both channels tags in the XML?

I think I have missing with something especially the integer.

Member Avatar for diafol

I can't get my head around 500+ lines of code, and it seems few other people can judging by the lack of response thus far. You have a lot of needless code here. I'll see if I can reduce it and perhpas it'll be more digestable :)

Member Avatar for diafol

Sorry, I got this far and had to stop. $count?? The getlisting.php may contain some paragraphs with urls, but this isn't made clear. You seem to re-assign your variables with different datatypes, which can be difficult to follow.

ini_set('max_execution_time', 300);

$errmsg_arr = array();
$errflag = false;

include ('simple_html_dom.php');

function getUrlFriendlyString($str)
{
  // convert spaces to '-', remove characters that are not alphanumeric
  // or a '-', combine multiple dashes (i.e., '---') into one dash '-'.
  $str = ereg_replace("[-]+", "-", ereg_replace("[^a-z0-9-]", "",
  strtolower( str_replace(" ", "-", $str) ) ) );
  return $str;
}

function getPaddedHour($hour, $meridian) 
{
    $ret = intval($hour);
    $ret += ($meridian == 'AM') ? 0 : 11;
    return str_pad($ret,2,'0',STR_PAD_LEFT);
}

$xml = '< ' .'?xml version="1.0" encoding="UTF-8" ?' .'>';
$xml .= '<tv generator-info-name="www.testbox.elementfx.com/test">';
$base1 = "http://www.mysite.com/get-listing.php";

$html = file_get_html($base1);        

foreach($html->find('p[id=links]') as $element)
{
  //$count is not encountered elsewhere - so should throw an error  
  $program_list[$count] = array();

  $id_split = explode("?", $element->plaintext);
  //You've just OVERWRITTEN the $id_split array with the following explode
  $id_split = explode("&", $link_split[1]);

  //Is $channels now a single value or an array? Confusing - assuming single
  $channels = explode("channels=",$id_split[0]);
  $channels = $channels[1];

  //Is $id a single value or an array? Assuming single
  $id = explode("id=",$id_split[1]);
  $id = $id[1];

There's a lot of conditionals too, so you could get rid of a lot of it with this:

function returnDateTime($hours, $minutes)
{
    $days = 1;
    if($hours == 24)$days = 2;
    if($hours == 12 || $hours = 24) $hours = 0;
    if($hours > 12 && $hours < 24) $days = 0;

    return date('Ymd', strtotime("+$days day")) . str_pad($hours,2,'0',STR_PAD_LEFT) . $minutes . "00";
}

$time1[0] = returnDateTime($hours,$minutes);

However, this is not quite right as you only allow AM to enter this branch. You've created a getPaddedHour() function, but haven't used it.

Without wanting to sound too critical, this is a mess and it will take some work to clean it up. I'd suggest that you try to cut all those needless conditionals and create reusable functions (as exeplified).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.