Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

Split string in 2 and send data to controller

I have been converting data to csv files simply by making the href target the controller responsible for it and when the user clicks on the link, the csv file downloads automatically.

However, in this particular case I actually need to send some data along where I didn't for the others. So, when just fetching some data I simply did the below and that points directly to the controller responsible for creating the file.

<a href="<?php echo URLROOT; ?>/Widgets/exportUsers" class="m-widget4__icon" download>
	<i class="la la-download"></i>
</a>

Open in new window



Here is the jQuery and Ajax for the date range picker. I have to split the dates so I have a start date and end date to pass through.

		$( '.salesclick' ).on('click', function(e) {
			e.preventDefault();
			var wholeDate = $( '#daterange' ).val();
			var index = wholeDate.indexOf(" ");  
			var start_date = wholeDate.substr(0, index); 
			var end_date = wholeDate.substr(index + 2); 
			$.ajax({
				url: url + '/Widgets/exportSales',
				type: 'POST',
				data: {start_date: start_date, end_date: end_date}
			})
			.done(function (data) {
				alert(data);
			})
		});

Open in new window


That goes to my controller:

      
	$start_date = $_POST['start_date'];
		$end_date = $_POST['end_date'];
		
		$results = $this->DashboardModel->listSales($start_date, $end_date);
		$filename = 'Sales_' . date('dmY') . '.csv'; 
			
			header("Content-Description: File Transfer");
			header("Content-Type: application/csv");
			header("Content-Disposition: attachment; filename=$filename");
			header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

			
			$handle = fopen('php://output', 'w');
		
			$header = array("Order Date"); 
			fputcsv($handle, $header);		
				foreach ($results as $result):
					fputcsv($handle, $result);
				endforeach;

			fclose($handle);
	}

Open in new window


And here is the model:

		$this->db->query("SELECT `order_date` FROM `order_summary` WHERE `order_date` BETWEEN :start_date AND :end_date");
		$this->db->bind(":start_date", $start_date);
		$this->db->bind(":end:date", $end_date);
		$results = $this->db->arraySet();
		return $results;

Open in new window


I am getting an error when just trying to see the data:

Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Then there is another bit about
Dashboard-&gt;listSales('2018-08-06', '2018-08-06')
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Looks like you have a typo in your Model:

$this->db->bind(":end:date", $end_date);

Should be:

$this->db->bind(":end_date", $end_date);

FYI - you don't need the colon when binding:

$this->db->bind("end_date", $end_date);
Avatar of Crazy Horse

ASKER

Oh, come on. Seriously, how did I miss that!? Haha, but thank you!

Is there a way I make it download automatically like the others?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
okay, I am going to give that a go and get back to you..
This seems to work perfectly, thank you!!!