I 've created a module that searches special prices from product_special table for OC2.3, between given dates from a form and display them with ajax in same page. I 'm confused cause i 'm not sure if the form not pass the values to controller or my controller is misconfigured and doesn't pass them to model. The only results i get, are products saved with dates 0000-00-00. Any help is very appreciated. :-(
My view tpl contains..
Code: Select all
.........
<div class="form-group">
<div class="col-sm-2">
<div class="input-group date">
<input type="text" id="datestart" name="datestart" value="" placeholder="<?php echo $text_date_start; ?>" data-date-format="YYYY-MM-DD" class="form-control" />
<span class="input-group-btn">
<button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
</span>
</div>
</div>
<div class="col-sm-2">
<div class="input-group date">
<input type="text" id="dateend" name="dateend" value="" placeholder="<?php echo $text_date_end; ?>" data-date-format="YYYY-MM-DD" class="form-control" />
<span class="input-group-btn">
<button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
</span>
</div>
</div>
<button type="button" id="button_search" name="button_search" class="btn btn-primary"><i class="fa fa-search"></i> </button>
<button type="reset" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-primary"> <?php echo $button_cancel; ?></button>
</div><br>
<div id="product_summary"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript"><!--
$('#button_search').on('click', function() {
var datestart = $('input[name=\'datestart\']').val();
var dateend = $('input[name=\'dateend\']').val();
$.ajax({
url: 'index.php?route=extension/module/massSpecialPrices/search&token=<?php echo $_GET["token"] ?>&datestart=' + this.value+'&dateend=' + this.value,
dataType: 'html',
success: function(htmlText) {
$('#product_summary').html(htmlText);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
//--></script>
<script type="text/javascript"><!--
$('.date').datetimepicker({
pickTime: false
});
//--></script>
<?php echo $footer; ?>
Code: Select all
<?php
class ControllerExtensionModuleMassSpecialPrices extends Controller {
private $error = array();
public function index() {
.......
public function search() {
$this->load->language('extension/module/massSpecialPrices');
$this->document->setTitle($this->language->get('heading_title'));
//load the model defined
$this->load->model('extension/module/massSpecialPrices');
//It checks for a valid POST request
if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate()) {
// prepare the $data array based of tpl text input and call the getProductSpecials method
$data['datestart'] = $this->request->post['datestart'];
$data['dateend'] = $this->request->post['dateend'];
$this->model_extension_module_massSpecialPrices->getProductSpecials($data);
}
$results = $this->model_extension_module_massSpecialPrices->getProductSpecials($data);
foreach ($results as $special) {
$data['results'][] = array(
'product_id' => $special['product_id'],
'price' => $special['price'],
'date_start' => $special['date_start'],
'date_end' => $special['date_end']
);
}
// pass information from controller method to the view template.
$data['text_model'] = $this->language->get('text_model');
$data['column_total'] = $this->language->get('column_total');
$data['text_date_start'] = $this->language->get('text_date_start');
$data['text_date_end'] = $this->language->get('text_date_end');
$this->response->setOutput($this->load->view('extension/module/massSpecialPrices_ajax_product', $data));
}
Code: Select all
<?php
class ModelExtensionModuleMassSpecialPrices extends Model {
public function getProductSpecials($data) {
if (isset($data['datestart'])) {
$datestart = $this->db->escape($data['datestart']);
} else {
$datestart = '0000-00-00';
}
if (isset($data['dateend'])) {
$dateend = $this->db->escape($data['dateend']);
} else {
$dateend = '0000-00-00';
}
$sql = "SELECT * FROM " . DB_PREFIX ."product_special WHERE
(date_start BETWEEN '$datestart' AND ' $dateend' ) AND
(date_end BETWEEN '$datestart' AND '$dateend' ) group by product_id ";
$query = $this->db->query($sql);
return $query->rows;
}
}