2011-12-21 38 views
1

我想在PHP中使用XML文件(編碼正確),從在線數據庫中的數據導出到iphone我想在PHP中使用XML文件(編碼正確),從在線數據庫中的數據導出到iphone

請告訴我它的編碼...因爲我新的PHP ......不知道多少:(

請幫我:(

裏面我是用現在的代碼如下:

<?php 

    // Èdition du dÈbut du fichier XML 
    $xml .= '<?xml version=\"1.0\" encoding=\"UTF-8\"?>'; 
    $sml .= '<channel>'; 
    $xml .= '<title>Infonul</title>'; 
    $xml .= '<link>aaa</link>'; 
    $xml .= '<description>aaa</description>'; 


    // connexion a la base (mettre ‡ jour le mdp) 
    $connect = mysql_connect('...-12','...','...'); 

    /* selection de la base de donnÈe mysql */ 
    mysql_select_db('...'); 

    // selection des 20 derniËres news 
    $res=mysql_query("SELECT u.display_name as author,p.post_date as date,p.comment_count as commentCount, p.post_content as content,p.post_title as title FROM wp_posts p, wp_users u WHERE p.post_status = 'publish' and p.post_type = 'post' and p.post_author = u.id ORDER BY p.id DESC LIMIT 0,20"); 


    // extraction des informations et ajout au contenu 
    while($tab=mysql_fetch_array($res)){ 

    $title=$tab[title]; 
    $author=$tab[author]; 
    $content=$tab[content]; //html stuff 
    $commentCount=$tab[commentCount]; 
    $date=$tab[date]; 

    $xml .= '<item>'; 
    $xml .= '<title>'.$title.'</title>'; 
    $xml .= '<content><![CDATA['.$content.']]></content>'; 
    $xml .= '<date>'.$date.'</date>'; 
    $xml .= '<author>'.$author.'</author>'; 
    $xml .= '<commentCount>'.$commentCount.'</commentCount>'; 
    $xml .= '</item>'; 
    } 

    // Èdition de la fin du fichier XML 
    $xml .= '</channel>'; 
    $xml = utf8_encode($xml); 

    echo $xml; 

    // Ècriture dans le fichier 
    if ($fp = fopen("20news.xml",'w')) 
    { 
    fputs($fp,$xml); 
    fclose($fp); 
    } 

    //mysql_close(); 

?>

但是這個代碼有一些錯誤

回答

0

可能提供XML內容到iPhone的最好辦法是通過一個的plist的方式。

plist文件(filename.plist)實際上是一種XML格式,但以一種特殊的方式。它看起來像這樣:

<plist version="1.0"> 
     <array> 
      <dict> 
       <key>title</key> 
       <string>Angry Cat</string> 
       <key>fileName</key> 
       <string>cat_angry1.caf</string> 
       <key>fileArray</key> 
       <array> 
        <dict> 
         <key>fileName</key> 
         <string>cat_angry1.caf</string> 
        </dict> 
       </array> 
       <key>icon</key> 
       <string>icon1.png</string> 
       <key>buttonIndex</key> 
       <integer>0</integer> 
      </dict> 
     </array> 
    </plist> 

因此,所有你需要做的是輸出在PHP中正確的標題:

header("Content-type: text/xml"); 
echo "<?xml version=\"1.0\"?>"; 

和輸出數據庫;向與上述格式的格式。

從那裏你可以使用NSURLConnection來下載plist並使用類似這裏的教程:http://iphoneincubator.com/blog/data-management/reading-and-writing-plists來閱讀plist格式。 :)代碼之後

更新被張貼

有你的代碼的幾個問題。首先在第5行下來你有$sml .= '<channel>';它應該是$xml...。數組應始終放在引號中。這是錯誤的:$tab[commentCount]它應該是$tab["commentCount"]只是在有任何常量命名相同。

頂部有$xml .= '...。雖然這可能有效,但我從未遇到過。只需在所有代碼行之前在頂部放置$xml = ''即可。然後,在嘗試向其附加字符串之前,初始化該變量。

希望有些作品。如果沒有這些問題,那麼請你發佈文件的OUTPUT。或者你遇到的任何php錯誤/ mysql錯誤,這可能與此有關。

+0

我用PHP代碼生成這個XML文件:( – 2011-12-21 16:39:42

+0

對不起,但是stackoverflow不是讓人們爲你製作腳本的地方,它是爲了幫助,我已經幫你了。去找出有關數據庫連接,從MySQL獲取數據,解析它,輸出等。 – 2011-12-21 16:44:08

+1

其實我已經做腳本..但它有錯誤 – 2011-12-21 16:55:28

相關問題