Jump to content

Recommended Posts

Hello, guys i got some issue in my script for exporting data to CSV (exel for view)

 

Its export all data in one collumn, but i need to export each data in separeta column like.

 

 

      a          B

a1  ID        ID2

 

But i got there this:

 

        a              B

a1  ID""ID2

 

 

Here is the script:

 

 

<?php
class WDealAdminExport extends UWidgetWorklet
{	
public function accessRules()
{
	return array(
		array('allow', 'roles' => array('company')),
		array('deny', 'users'=>array('*'))
	);
}

public function taskConfig()
{
	$list = wm()->get('deal.admin.coupon');
	$_GET[$list->modelClassName] = $_POST[$list->modelClassName];
	$list->init();
	$dataProvider = $list->dataProvider();
	$dataProvider->pagination = false;

	$csv = array(
		array(
			$this->t('ID'),
			$this->t('Order ID'),
			$this->t('Deal ID'),
			$this->t('User'),
			$this->t('Status'),
		)
	);

	foreach($dataProvider->data as $data)
	{
		$row = array(
			"#".$data->couponId(),
			$data->orderId,
			$data->dealId,
			$data->user->email." [".$data->user->getName(true)."]",
			$data->status==1
				? ($data->deal->isExpired() ? $this->t('Expired') : $this->t('Available'))
				: $this->t('Used')
		);

		if($_POST['charset'] != 'utf-8')
		{
			$row[3] = iconv('utf-8', $_POST['charset'], $row[3]);
			$row[4] = iconv('utf-8', $_POST['charset'], $row[4]);
		}

		$csv[] = $row;
	}

	$contents = $this->generateCSV($csv);
	$this->send($contents);
}

public function taskGenerateCSV($src)
{
	$csv = '';
	foreach($src as $row)
	{
		foreach($row as $item)
			$csv.= $this->escape($item).$this->param('delimiter');
		$csv = rtrim($csv,$this->param('delimiter'))."\r\n";
	}
	return $csv;
}

public function taskEscape($value)
{
	return '"'.str_replace('"','""',$value).'"';
}

public function taskSend($contents)
{
	$content_type = "application/octet-stream";
	$filename = 'export.csv';

	// disable browser caching
	header('Cache-control: private');
	header('Pragma: private');
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

	header('Content-Type: '.$content_type);
	header('Content-Transfer-Encoding: binary');
	header('Content-Length: '.strlen($contents));
	header('Content-Disposition: attachment;filename="'.$filename.'"');

	echo $contents;
	app()->end();
}
}

Link to comment
https://forums.phpfreaks.com/topic/259194-need-help-for-exporting-scriprt-to-csv/
Share on other sites

Script exports data from database, to exel...

 

 

But thing is , that it exports all data in one cell

 

I need somehow export this info from database to saparete cells 1 cell for id , 1 cell for order id etc


$csv = array(
		array(
			$this->t('ID'),
			$this->t('Order ID'),
			$this->t('Deal ID'),
			$this->t('User'),
			$this->t('Status'),
		)
	);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.