使用phonegap我創建一個可以導出csv文件的應用程序。我通過使用文件編寫器(http://docs.phonegap.com/en/3.3.0/cordova_file_file.md.html#FileWriter)來做到這一點。寫一個CSV文件:顯示 Â 字符
問題是,當我打開使用OpenOffice.org它顯示此字符導出.csv文件:一個程度符號(°)之前。 下面是詳細信息:
HTML
<select>
<option value="°C"> °C </option>
<select>
價值被保存在數據庫中:
ID | temperature | temperature_unit
--- ------------- -----------------
1 | 36 | °C
PHP
while($row = $STH->fetch())
$row['temperature_unit'] = html_entity_decode($row['temperature_unit']);
$row_set[] = $row;
}
echo json_encode($row_set);
使用Javascript(縮短)
//data = json_encoded $row_set
var datatoexport = JSON.parse(data)
var content = "Temperature \n";
for(var x in datatoexport) {
content += datatoexport[x].temperature + " " + datatoexport[x].temperature_unit;
}
//write content to file
writer.write(content);
writer.onwriteend = function(evt) {
alert("File exported");
};
結果(CSV文件中的openoffice.org應用程序中打開)
Temperature
-----------
36 �Â�°C
編輯:AJAX和PHP代碼來保存數據到數據庫(縮短)
AJAX
assetinfo = {temp:"35",temp_unit:"°C"}
$.post(serverURL + 'API/save.php',
{
temperature: assetinfo.temp,
temp_unit: assetinfo.temp_unit
},
function(data) {
alert("saved");
}
});
PHP
$temp = $_REQUEST['temperature'];
$temp_unit = $_REQUEST['temp_unit'];
$STH = $DBH->prepare("INSERT INTO bearing_table (temperature, temperature_unit) VALUES (:temp, :temp_unit)");
$STH->bindParam(':temp', $temp);
$STH->bindParam(':temp_unit', $temp_unit);
$STH->execute();
這是一個編碼問題,在你編寫UTF-8(°)爲'LATIN1'的地方。數據庫中已經有錯誤的值。你的數據庫列編碼是否設置爲UTF-8? – mudasobwa 2014-10-08 06:56:39
@mudasobwa表的排序規則目前設置爲「latin1_swedish_ci」,我應該如何設置它? – clintgh 2014-10-08 07:02:59