0
首先,我不想要一個替代的代碼,我只想幫助做這個工作。 使用下面的代碼我想從數據庫表中寫出數據到XML文件,但我只得到「john」和「123」沒有正確的xml標籤,我的第二個foreach()不起作用,但是首先foreach()來顯示錶格的作品。寫出數據庫表到XML
<?php
$dbhandle = sqlite_popen("propertydb", 0666, $err_msg);
if(!$dbhandle) die("Could not open the database");
$query = "CREATE TABLE property(pCode VARCHAR(8), price VARCHAR(6),
iFile VARCHAR(15), visits VARCHAR(3))";
if(!sqlite_query($dbhandle, $query)){ echo "table not created (maybe already
created)";
}
$query = sqlite_query($dbhandle, 'SELECT * FROM property');
$result = sqlite_fetch_all($query, SQLITE_ASSOC);
foreach ($result as $arow) {
echo '<br> Postcode: ' . $arow['pCode'] . ' Price: ' . $arow['price'] .
' Image File: ' . $arow['iFile'] . ' Visits: ' . $arow['visits'];
}
$outfilepointer = fopen("property.xml","w");
$outtext = "<estateagent> \n";
$outtext .= "<agentname>john</agentname> \n";
$outtext .= "<agentphone>123</agentphone> \n";
foreach ($result->property as $oneproperty)
{
$propertyname = $oneproperty->propertyname;
outtext .= '<property><postcode> $arow["pCode"] </postcode>
<price> $arow["price"]</price> <imagefile> $arow["iFile"] </imagefile> <visits>
$arow["visits"]
</visits> </property> \n';
}
$outtex .= "</estateagent>";
fwrite($outfilepointer, $outtext);
fclose($outfilepointer);
sqlite_close($dbhandle);
?>
這是我的輸出應該是什麼......
<estateagent>
<agentname></agentname>
<agentphone></agentphone>
<property>
<postcode></postcode><price></price><imagefile></imagefile><visits></visits>
</property>
<property>...</property>
<property>...</property>
</estateagent>
仍在收到「警告:提供給foreach()的無效參數在...」 – 2012-03-10 14:18:02
編輯我的答案以給出可能的解決方案。 – F21 2012-03-10 22:55:42