输出数组从php到xml

时间:2014-03-21 00:18:31

标签: php mysql xml

我的PHP脚本需要一些基本的帮助。我从mysql数据库生成带有数组列表的XML源代码。我想访问每个数组的get-listing.php中的每个PHP链接,以获取程序标题列表,以便在XML源代码中输出它们。

这里的例如:

http://testbox.elementfx.com/get-listing.php

我想访问get-​​listing.php,我想获取cbs链接以获取编程信息列表:

<span id="time1">12:00 PM </span> - <span id="title1">2014 NCAA Basketball Tournament"Dayton vs. Ohio State - LIVE</span><br><br>
<span id="time2">2:30 PM </span> - <span id="title2">2014 NCAA Basketball Tournament"Western Michigan vs. Syracuse - LIVE</span><br><br>
<span id="time3">5:00 PM </span> - <span id="title3">Local Programming</span><br><br>
<span id="time4">6:00 PM </span> - <span id="title4">Local Programming</span><br><br>
<span id="time5">7:00 PM </span> - <span id="title5">2014 NCAA Basketball Tournament"Wofford vs. Michigan - LIVE</span><br><br>
<span id="time6">9:30 PM</span> - <span id="title6">2014 NCAA Basketball Tournament"Arizona State vs. Texas - LIVE</span><br><br>
<span id="time7">12:00 AM </span> - <span id="title7"> 1 Local Programming</span><br><br>
<span id="time8">12:35 AM </span> - <span id="title8">Late Show With David LettermanTV-PG  </span><br><br>
<span id="time9">1:37 AM </span> - <span id=title9>The Late Late Show With Craig FergusonTV-14  </span><br><br>

CBS链接:http://testbox.elementfx.com/get-listing.php?channels=CBS&id=102

<?php

function db_connect()
{
  define('DB_HOST', 'localhost');
  define('DB_USER', 'myusername');
  define('DB_PASSWORD', 'mypassword');
  define('DB_DATABASE', 'mydbname');

  $errmsg_arr = array();
  $errflag = false;
  $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

  if(!$link) 
  {
    die('Failed to connect to server: ' . mysql_error());
  }

  $db = mysql_select_db(DB_DATABASE);
  if(!$db) 
  {
    die("Unable to select database");
  }
}
db_connect();

  function clean($var)
  {
    return mysql_real_escape_string(strip_tags($var));
  } 
  $channels = clean($_GET['channels']);
  $id = clean($_GET['id']);

  if($errflag) 
  {
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    echo implode('<br />',$errmsg_arr);
  }
  else 
  {
    $insert = array();

    if(isset($_GET['channels'])) 
    {
      $insert[] = 'channels = \'' . clean($_GET['channels']) .'\'';
    }
    if(isset($_GET['id'])) 
    {
      $insert[] = 'id = \'' . clean($_GET['id']) . '\'';
    }

    if(!$channels && ! $id) 
    {
      $qrytable1="SELECT id, channels, links, streams FROM tvguide";
      $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());


      while ($row = mysql_fetch_array($result1))
      {
        // fake some example data. the actual data would be retrieved from a database query
        $data[] = array('channel_id'=>$row['channels'],
        'display_name'=>$row['channels'],
        'program_id'=>123,'start'=>'s1','stop'=>'e1',
        'title'=>'program title',
         'sub_title'=>'sub title',
         'description'=>'program description1',
         'category'=>'some category');



        // build the xml    
        $xml = '<?xml version="1.0" encoding="UTF-8" ?>
<tv generator-info-name="www.testbox.elementfx.com/xmltv">';
        $last_channel = null; // used to detect when the channel changes
        foreach($data as $arr)
        {
            if($last_channel != $arr['channel_id'])
            {
                // the channel changed
                if($last_channel != null)
                {
                  // not the first channel, close out the previous channel
                  $xml .= "
</channel> \n";


                }
                // start a new channel
                $xml .= "
<channel id=\"{$arr['channel_id']}\">";
                $xml .= "
   <display-name>{$arr['display_name']}</display-name>";
                $last_channel = $arr['channel_id'];
            }
            // output the program info under each channel
            $xml .= "
   <programme channel=\"{$arr['channel_id']}\" start=\"{$arr['start']}\" stop=\"{$arr['stop']}\">";
            // i don't see a program id in this definition, but it likely needs one
            $xml .= "
       <title lang=\"en\">{$arr['title']}</title>";
            $xml .= "
       <sub-title lang=\"en\">{$arr['sub_title']}</sub-title>";
            $xml .= "
       <desc lang=\"en\">{$arr['description']}</desc>";
            $xml .= "
       <category lang=\"en\">{$arr['category']}</category>";
            $xml .= "
   </programme>";
        }
        if($last_channel != null)
        {
            // close out the previous channel if any
            $xml .= '
</channel>';
        }
     }

   }
   $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);
 }
?>

与get-listing.php中的每个数组相同。

请告诉我如何从每个PHP标记<span id="title1"><span id="title2"> <span id="title3">获取字符串列表,以便为每个xml标记中的每个字符串输出{} {1}}使用我的php源代码的每个程序?

0 个答案:

没有答案