Hi all,

I need some of your help, I'm working on my PHP script to create the XML document with encoding utf8 so I can generate the XML file to allow me to save the XML file in my web host.


I want to make the xml output to something like this:

Code:
<?xml version="1.0" encoding="UTF-8" ?>
<tv generator-info-name="www.mysite.com/xmltv">
<channel id="">
   <display-name>Information from database</display-name>
   <programme channel="Information from database" start="" stop="">
       <title lang="en"></title>
       <sub-title lang="en">
       </sub-title>
       <desc lang="en"></desc>
       <category lang="en"></category>
   </programme>
</channel>


Here's what my XML output looks like:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<tv generator-info-name="www.mysite.com/xmltv"><channel><display-name>Information from database</display-name><programme/><desc/></channel></tv>

Here's the current code:

Code:
<?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 FROM tvguide WHERE channels='$channels' && id='$id'";
      $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
      echo '<?xml version="1.0" encoding="UTF-8" ?>
<tv generator-info-name="www.mysite.com/xmltv">
<channel id="">
   <display-name></display-name>
   <programme channel="" start="" stop="">
      <title lang="en"></title>
      <sub-title lang="en"></sub-title>
      <desc lang="en"></desc>
      <category lang="en"></category>
   </programme>
</channel>
</tv>';
	
      while ($row = mysql_fetch_array($result1))
      {
        
      }
      mysql_close();
    }
    else 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)) 
      {
        
      }
      mysql_close();
    }
  }
  // create a dom document with encoding utf8
  $domtree = new DOMDocument('1.0', 'UTF-8');

  // create a root element of the xml tree
  $tv = $domtree->createElement('tv');

    //create attributes for element
    $generator_info_name = $domtree->createAttribute('generator-info-name');
    $generator_info_name->value = 'mysite.com/xmltv';
    //append attribute
    $tv->appendChild($generator_info_name);
    // append element to the doc
    $tv = $domtree->appendChild($tv);

    //add a channel as a child of the root
    $channel = $domtree->createElement('channel');
    $channel_id = $domtree->createAttribute('id');
    $channel_id->value = '""';
    $channel = $tv->appendChild($channel);

        //append children to channel
        $channel->appendChild($domtree->createElement('display-name','Information from database'));
        $channel->appendChild($domtree->createElement("programme"));
        $channel->appendChild($domtree->createElement('desc'));

    //finally, save the file
    echo $domtree->saveXML();
    $domtree->save('myChannel.xml');
?>


Do you know how I can make the same XML output as the first code?

And how I can output for each data from mysql database to put it in each channel tag and I want to add the tags under the channel tag including the display-name, programme-channel, title, sub-title, desc and category tags when I output for each data from mysql?

Any advise would be much appreciated.

Thanks in advance