Hello,
I am learning php using yii framework. Was using vba and sql before so don’t have much knowledge about javascript.
I have a sql query to which I want to pass date using DatePicker. I did the parameter binding and it works if I give values directly to the binded variables. I don’t know how to exactly pass the value after clicking submit button.
In my view I use TbActiveForm and datepicker widgets. So my view.php looks like:
<?php
$form = $this->beginWidget(
'bootstrap.widgets.TbActiveForm',array(
'id' => 'inlineForm',
'type' => 'inline',
'htmlOptions' => array('class' => 'well'),
));
?>
Start Date
<?php $this->widget('bootstrap.widgets.TbDatePicker',array(
'name' => 'from_date',
'id' => 'date1',
'value' => '2014-01-01',
'options' => array('format' => 'yyyy-mm-dd')
));
?>
End Date
<?php $this->widget('bootstrap.widgets.TbDatePicker',array(
'name' => 'to_date',
'id' => 'date2',
'value' => '2014-04-30',
'options' => array(
'format' => 'yyyy-mm-dd')
));
?>
<!-- Submit Button -->
<?php $this->widget('bootstrap.widgets.TbButton',array(
'buttonType' => 'Submit',
'label' => 'Submit',
'type' => 'primary',
));
$this->endWidget();
?>
And in the controller I have :from_date and :to_date binded to variables $from and $to respectively. If I provide values to the variables the query works fine so there is no problem in binding. The controller code looks like:
{
$from = '2014-01-01';
$to = '2014-01-28';
// $from = $_POST['from_date'];
// $to = $_POST['to_date'];
$sql = "SELECT [Ordernumber]
,[Order_Date]
FROM [Orders]
WHERE CAST(Order_Date As Date) BETWEEN :from_date AND :to_date
ORDER BY Order_Date ";
$sql2 = "SELECT COUNT(*)
FROM [Orders]
WHERE CAST(Order_Date As Date) BETWEEN :from_date AND :to_date";
$connection=Yii::app()->db;
$command=$connection->createCommand($sql2);
$command->bindParam(':from_date',$from,PDO::PARAM_STR);
$command->bindParam(':to_date',$to,PDO::PARAM_STR);
$dataProvider = new CSqlDataProvider($sql, array('keyField' => 'Order_Date',
'params' => array(
':from_date' => $from,
':to_date' => $to),
'totalItemCount' => $command->queryScalar(),
'pagination' => array(
'pageSize' => 20)));
$this->render('link', array('dataProvider' => $dataProvider));
}
So if I use
$from = $_POST['from_date'];
I get an error undefined index from_date. I want the query to initially return results based on default value of the DatePicker, and if user selects some date range and clicks submit, the result should be based on those dates. So in short I don’t know how to pass the value submit by user in the view to the controller.