Welcome, Guest
Username: Password: Remember me

TOPIC: Export to Csv not work

Export to Csv not work 25 Jul 2013 17:28 #10578

  • pinochet
  • pinochet's Avatar
  • Offline
  • Senior Member
  • Posts: 47
  • Karma: 0
I followed to the letter the tutorial to export to csv, it works and I created and downloaded the csv but in WHITE, does not have any data just the name of the columns but everything else blank.

this is my code for your controller:
public function export ()
{
$ model = CkJModel :: getInstance ('Properties', 'M2guateCkModel');
$ model-> setState ('context', 'propiedades.default');
$ items = $ model-> getItems ();
$ fields = array (
'_created_by_name' => 'Created by'
);
$ file = JPATH_ADMIN_M2GUATE. DS. 'tmp'. DS. 'export.csv';
M2guateHelper :: csvExport ($ items, $ fields, $ file, array (
'columns' => true,
'output' => true
));
}

any idea why blank csv
The administrator has disabled public write access.

Export to Csv not work 27 Jul 2013 09:30 #10601

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Try to dump the vars, the arrays...

is $items filled with something ?
are you sure for your context ? Because context is taking the permanent filters values in consideration. (and pagination ....)
Did you tried to create an new context for that ? (in prepareQuery() of the model)

It should not be a big deal... I think this is really easy to debug.
Coding is now a piece of cake
The administrator has disabled public write access.

Export to Csv not work 29 Jul 2013 15:48 #10620

  • pinochet
  • pinochet's Avatar
  • Offline
  • Senior Member
  • Posts: 47
  • Karma: 0
I am sure of the context, I want you like the tutorial export content as shown, with filters and everything.
but does not work, follow to the letter the tutorial, it will be Joomla 3?

so you see the code:

view.html.php:

//export
$task = 'propiedades.export';
$icon = 'export';
$alt = JText::_("JTOOLBAR_EXPORT");
$listSelect = false;
CkJToolBarHelper::custom($task, $icon, $icon, $alt, $listSelect);
parent::display($tpl);
}

helper:

function csvExport($items, $fields, $file, $config = array())
{
$output = (isset($config) && $config);
$columns = (isset($config) && $config);
$delimiter = (isset($config)?$config:',');
$enclosure = (isset($config)?$config:'"');
jimport('joomla.filesystem.file');
if ($output)
{
// send response headers to the browser
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.JFile::getName($file));
$fp = fopen('php://output', 'w');
}
else
$fp = fopen($file, 'w');
if ($columns)
fputcsv($fp, $fields, $delimiter, $enclosure);
foreach($items as $item)
{
$row = array();
foreach($fields as $key => $label)
$row[] = $item->$key;
fputcsv($fp, $row, $delimiter, $enclosure);
}
fclose($fp);
exit();
}


contoler:

//export cvs
public function export()
{
$model = CkJModel::getInstance('Propiedades', 'M2guateCkModel');
$model->setState('context', 'propiedades.default');
$items = $model->getItems();
$fields = array(
'_tipo_de_propiedad_tipo_de_inmueble' => 'tipo de Inmueble',
);
$file = JPATH_ADMIN_M2GUATE .DS. 'tmp' .DS. 'export.csv';
M2guateHelper::csvExport($items, $fields, $file, array(
'columns' => true,
'output' => true
));
}

any idea?
The administrator has disabled public write access.

Export to Csv not work 05 Aug 2013 19:38 #10692

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
I use a completly different way to export, if you still need it.
The administrator has disabled public write access.

Export to Csv not work 06 Aug 2013 20:05 #10696

  • pinochet
  • pinochet's Avatar
  • Offline
  • Senior Member
  • Posts: 47
  • Karma: 0
ofcourse if any help is much appreciated, could give me a light on how to do
The administrator has disabled public write access.

Export to Csv not work 06 Aug 2013 23:04 #10698

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
this comes in the the_view_name.php
<script>
jQuery(document).ready(function() {
    jQuery('#convert').click(function() {
		var href = 'http://yourdomain.tld/libraries/vecrea/export.php';      // Replace with you own domain name
		var datas = jQuery('#grid').tableToCSV();
		datas = datas.replace(/\s+/g, '');
		datas = datas.replace(/,/g, "\t");
		datas = datas.replace(/\\r/g, "\r\n");
		jQuery.download('http://vhus.recycar.pro/libraries/vecrea/export.php', {data : datas} );
	});
});
jQuery.download = function(url, data, method){
	if( url && data ){ 
		data = typeof data == 'string' ? data : jQuery.param(data);
		var inputs = '';
		jQuery.each(data.split('&'), function(){ 
			var pair = this.split('=');
			inputs+='<input type="hidden" name="'+ pair[0] +'" value="'+ pair[1] +'" />'; 
		});
		jQuery('<form action="'+ url +'" method="'+ (method||'post') +'">'+inputs+'</form>')
		.appendTo('body').submit().remove();
	};
};
jQuery.fn.tableToCSV = function() {
	 var csv = '';
	 var headers = [];
         jQuery("#grid").find('th').each(function() {
		   var header = '';
				var $th = jQuery(this);
				var text = $th.text();
				var header = '"' + text + '"';
	  
				headers.push(header);
			});
	   csv += headers.join(',');
	   csv += '\\r\n';
	   	 jQuery("#grid").find('tr').not('tr:first-child').each(function() 
	     jQuery(this).find('td').each(function() {
		    var row = jQuery(this).text();
			if(!jQuery(this).is('td:last-child')) {
			    row += ',';
			} else {
				row += '\\r\n';
			}
			csv += row;
		 });
	  });  
	return csv;
};
</script>

Don't forget to add a button, div, whatever with id #convert somewhere. I add it in the toolbar, but choose what you want.
It calls a export.php. Here's the code
<?
if(empty($_POST['data'])){echo "rien";exit;}
header('Content-type: application/vnd.ms-excel;');
header('Content-Disposition: attachment; filename="table.csv"');
$datas = $_POST['data'];
$csv = urldecode($datas);
echo $csv;
exit();
?>

The whole thing is pretty ugly, but it works fine for me : what it does is it takes what the user sees in the table on screen and converts it in csv.
It's not the best solution, and the code is pretty bad, but it works
The administrator has disabled public write access.

Export to Csv not work 07 Aug 2013 16:19 #10708

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
@VeCrea : I cannot validate this.
$datas = $_POST['data'];
$csv = urldecode($datas);
Ouch....


You are parsing your HTML, then sending to PHP to create the file.
I believe that it works, but really wierd.

@pinochet
My advice is to do it completly in PHP. No javascript.
Your items can be extracted using the same filtering and/or pagination than the current view. Or can also be totally different filtering.

Also for huge lists, the best is to read DB /write File step by step.
In other terms, if you load the whole table it can be too much in memory.

Let see when I have a little bit of time I will integrate in Cook because this optimization story is not easy.
You need to use native php functions, and not object oriented programation. (POO)

Well. Are your exports huges lists ?
Coding is now a piece of cake
Last Edit: 07 Aug 2013 16:21 by admin.
The administrator has disabled public write access.

Export to Csv not work 07 Aug 2013 18:36 #10716

  • pinochet
  • pinochet's Avatar
  • Offline
  • Senior Member
  • Posts: 47
  • Karma: 0
I have not been able to export, export lists that might become big cook tutorial also works in your example but does not work with J3, there is something to do to make it compatible? or as to export all items, regardless of the filter?

@ Vecrea, your solution is great, I'll try, but not if you work for long lists. Thanks for taking the time to share your solution
The administrator has disabled public write access.

Export to Csv not work 07 Aug 2013 19:42 #10717

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
@admin : i know it's not pretty, i said it in my post. But it works, and at the moment, without something built in cook, well that's all i have and the people using it don't complain, even if i know it should not be done this way.

@pinochet : how long would your list be ? It has worked for over 1000 lines here, but i don't know how long your list is.
The administrator has disabled public write access.
Time to create page: 0.140 seconds

Get Started