-2
我想在Zend Framework中將數據庫表列字段下載爲CSV。有沒有任何ZF庫?在Zend Framework中下載數據庫表列字段爲CSV
SO還有其他的問題可以解決這個特定的問題,但是他們都沒有提供足夠的細節。有人可以請詳細描述如何做到這一點?
我想在Zend Framework中將數據庫表列字段下載爲CSV。有沒有任何ZF庫?在Zend Framework中下載數據庫表列字段爲CSV
SO還有其他的問題可以解決這個特定的問題,但是他們都沒有提供足夠的細節。有人可以請詳細描述如何做到這一點?
我假設你有一個標準的DbTable模型用於你的表格。獲取表格信息使用info() method
//the following is pseudocode and not meant to be copied directly, intended to guide
public function indexAction() {
$model = new Application_Model_DbTable_MyTable();
//This returns an array of table info
$info = $model->info();
//prepare the data so each line can be saved
$data = $info['name'] . ',' . $info['primary'];
//then write each line to the database, I usually use a function from
// a utility class or a protected function
//I would normally build an array of the data I want saved and then
//iterate over the array saving as I go.
$save = $this->_writeToFile($filename, $data);
}
//the following function is not pseudocode
protected function _writeToFile($filename, $content) {
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($content) to file ($filename) <br />";
//close file
fclose($handle);
} else {
echo "The file $filename is not writable";
}
}
希望這點指向你在正確的方向。
您應該接受以前問題的一些答案。 – sshow 2012-04-13 09:22:32