下載我在CakePHP中framework.I新希望生成CSV file.Here我有結果數組,如:CSV文件沒有CakePHP的
Array
(
[0] => Array
(
[User] => Array
(
[username] => yerica
[email] => [email protected]
[phone] => 993643636
)
[DealPurchase] => Array
(
[deal_title] => Tour de Zaragoza
[original_price] => 30
)
)
[1] => Array
(
[User] => Array
(
[username] => Rama Test
[email] => [email protected]
[phone] => 9652369854
)
[DealPurchase] => Array
(
[deal_title] => Tour de Zaragoza
[original_price] => 30
)
)
)
在這裏,我要生成這個數組的CSV。
在這裏,我必須包括針對CsvHelper.php文件/傭工folder.This文件包含:
<?php
class CsvHelper extends AppHelper
{
var $delimiter = ',';
var $enclosure = '"';
var $filename = 'Export.csv';
var $line = array();
var $buffer;
function CsvHelper()
{
$this->clear();
}
function clear()
{
$this->line = array();
$this->buffer = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
}
function addField($value)
{
$this->line[] = $value;
}
function endRow()
{
$this->addRow($this->line);
$this->line = array();
}
function addRow($row)
{
fputcsv($this->buffer, $row, $this->delimiter, $this->enclosure);
}
function renderHeaders()
{
header('Content-Type: text/csv');
header("Content-type:application/vnd.ms-excel");
header("Content-disposition:attachment;filename=".$this->filename);
}
function setFilename($filename)
{
$this->filename = $filename;
if (strtolower(substr($this->filename, -4)) != '.csv')
{
$this->filename .= '.csv';
}
}
function render($outputHeaders = true, $to_encoding = null, $from_encoding ="auto")
{
if ($outputHeaders)
{
if (is_string($outputHeaders))
{
$this->setFilename($outputHeaders);
}
$this->renderHeaders();
}
rewind($this->buffer);
$output = stream_get_contents($this->buffer);
if ($to_encoding)
{
$output = mb_convert_encoding($output, $to_encoding, $from_encoding);
}
return $this->output($output);
}
}
?>
在MerchantController(控制器)功能是這樣的:
function download_csv($deal_id)
{
$options=array('fields'=>'User.username,User.email,User.phone,DealPurchase.deal_title,DealPurchase.original_price',
'joins' =>
array(
array(
'table' => 'deal_purchases',
'alias' => 'DealPurchase',
'type' => 'inner',
'foreignKey' => false,
'conditions'=> array('DealPurchase.user_id = User.id')
)));
$this->recursive = -1;
$data = $this->User->find('all', $options);
$this->set("users",$data);
$this->layout = null;
$this->autoLayout = false;
Configure::write('debug', '0');
}
這裏,我查看文件代碼是:
<?php
$line= $users[0]['User'];
$this->CSV->addRow(array_keys($line));
foreach ($users as $user)
{
$line = $user['User'];
$this->CSV->addRow($line);
}
$filename='users';
echo $this->CSV->render($filename);
在這裏,我的另一個函數,其中包含鏈接下載CSV文件e和功能看起來像:
function deal_list(){
$this->_checkMerchantSession();
$this->set('title_for_layout', 'Deal List');
$this->layout = "after_login";
//echo $this->Session->read('userData.Merchant.id');
//$conditions = "Deal.merchant_id ='".$this->Session->read('userData.Merchant.id')."' AND Deal.isdeleted ='0' AND Deal.isapproved ='0' ";
$conditions = "Deal.merchant_id ='".$this->Session->read('userData.Merchant.id')."' AND Deal.isdeleted ='0' ";
//$ArDealdetails = $this->Deal->find('all', array('conditions'=>$conditions,'order'=>'Deal.modified DESC'));
$this->paginate = array('conditions' => $conditions,'limit' =>5,'order'=>'Deal.id DESC');
$ArDealdetails = $this->paginate('Deal');
$this->set('ArDealdetails',$ArDealdetails);
$condition1 = "Merchant.id ='".$this->Session->read('userData.Merchant.id')."'";
$merchant_detail = $this->Merchant->find('first',array('conditions'=>$condition1));
$this->set('merchant_detail',$merchant_detail);
}
現在,當我有助手爲CSV,如:
var $helpers = array('Html', 'Form','Javascript','Fck','Js','Paginator','Csv');
然後,它會給出一個error.So我怎樣才能解決這個問題呢?
注意:我有類似的錯誤:錯誤:在此服務器上找不到請求的地址'/ merchant/deal_list'。我已經在deal_list視圖文件中添加了下載csv鏈接。當我從助手中刪除Csv時,顯示此頁面。
錯誤:未定義的屬性:查看:: $ Csv。
錯誤信息是什麼? –
@JasonJoslin在Note中看到我更新的問題。 –
您的商戶控制器中是否有deal_list()操作?它是什麼樣子的? –