2012-09-27 71 views
1

請幫我做在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

代碼是否給你一個錯誤?你是什​​麼意思「失敗」?預期的結果是什麼,你得到了什麼? – Fanis

回答

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