Crazy Horse
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.
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.
That goes to my controller:
And here is the model:
I am getting an error when just trying to see the data:
Then there is another bit about
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>
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);
})
});
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);
}
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;
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->listSales('2018-08-06' , '2018-08-06')
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?
Is there a way I make it download automatically like the others?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
okay, I am going to give that a go and get back to you..
ASKER
This seems to work perfectly, thank you!!!
$this->db->bind(":end:date
Should be:
$this->db->bind(":end_date
FYI - you don't need the colon when binding:
$this->db->bind("end_date"