请帮我在php中完成这个过程,以便从变量中获取数据。在这种情况下,我创建了一个用于创建xml文件的php文件。这里我有数据,即频道,我希望我的孩子将在该频道中的每个家长输入数据。请看我的代码:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "db_epg";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
$sql = "select distinct(channel_name) FROM epg";
$q = mysql_query($sql) or die(mysql_error());
$xml = "<timetable start='00:00' end='24:00' title='Friday, September 28th, 2011'>";
while($r = mysql_fetch_array($q)){
$CN = $r['channel_name'];
$xml .= "<channel>".$CN."</channel>";
}
$xml .= "</timetable>";
$sxe = new SimpleXMLElement($xml);
$sxe->asXML("epg.xml");
?>
我的代码的结果是:http://s9.postimage.org/rjsodf9mn/result.png
如果我想在每个数据通道中进行循环,那么没有关于儿童的数据,那我该怎么办?
我已尝试在此中部分执行其他查询:
while($r = mysql_fetch_array($q)){
$CN = $r['channel_name'];
$xml .= "<channel>".$CN."</channel>";
$sql2 = "select * FROM epg where channel_name='".$CN."'";
$q2 = mysql_query($sql2) or die(mysql_error());
while($r2 = mysql_fetch_array($q2)){
$Mulai = date('H:i', strtotime($r2['waktu_mulai']));
$Selesai = date('H:i', strtotime($r2['waktu_akhir']));
$Title = $r2['judul'];
$Desk = $r2['sinopsis'];
$xml .= "<event start='".$Mulai."' end='".$Selesai."'>";
$xml .= "<title>".$Title."</title>";
$xml .= "<description>".$Desk."</description>";
$xml .= "</event>";
}
}
但我失败了。有人能帮助我吗?
答案 0 :(得分:2)
尝试修改您的代码,如下所示,请检查语法错误,因为我没有机会尝试它:)
while ($r = mysql_fetch_array($q)) {
// open channel header
$CN = $r['channel_name'];
$xml .= "<channel name='".$CN."'>";
// event details for channel
$sql2 = "select * FROM epg where channel_name='".$CN."'";
$q2 = mysql_query($sql2) or die(mysql_error());
while($r2 = mysql_fetch_array($q2)) {
$Mulai = date('H:i', strtotime($r2['waktu_mulai']));
$Selesai = date('H:i', strtotime($r2['waktu_akhir']));
$Title = $r2['judul'];
$Desk = $r2['sinopsis'];
$xml .= "<event start='".$Mulai."' end='".$Selesai."'>";
$xml .= "<title>".$Title."</title>";
$xml .= "<description>".$Desk."</description>";
$xml .= "</event>";
}
// close channel
$xml .= "</channel>";
}
您可能还会在此处看到代码the code here