How to get value from datepicker to query


(ramdanmono) #1

hallo , i want tosk about datepicker
how to get value from date picker to query ,
i have code date picker , if i select date and data show as date selected from date picker

<?php
use app\common\helpers\Timeanddate;
use app\models\DeviceLog;
use yii\helpers\Html;
use yii\widgets\DetailView;
use dosamigos\chartjs\ChartJs;
use yii\helpers\ArrayHelper;
use dosamigos\datepicker\DatePicker;
/ @var $this yii\web\View /
/ @var $model app\models\Device /
$logs = DeviceLog::find()
->where([‘id_device’ => $model->id_device])
->andWhere([‘log_date’=> ‘tanggal’])
->orderBy(‘log_time DESC’)
->all();
$sumbu_x_data = array();
$sumbu_y_data = array();
foreach($logs as $log){
//Label Sumbu X
$sumbu_x_data[] = Timeanddate::getTimeOnly($log->log_time);
$sumbu_y_data[] = $log->value1;
}
$this->title = $model->device_name;
$this->params[‘breadcrumbs’][] = [‘label’ => ‘Devices’, ‘url’ => [‘index’]];
$this->params[‘breadcrumbs’][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class=“device-view box box-primary”>
<?php /<h1><?= Html::encode($this->title) ?></h1>/ ?>

<?= DetailView::widget([
‘model’ => $model,
‘attributes’ => [
//‘id_device’,
‘device_name’,
‘device_id’,
‘address’,
‘param1’,
//‘param2’,
// ‘param3’,

// ‘last_value1’,
// ‘last_value2’,
// ‘last_value3’,
// ‘last_update’,
// ‘last_value4’,
],
]) ?>

</div>
<div class=“row”>
<div class=“col-md-6”>
<div class=“box”>
<div class=“box-header with-border”>
<h3 class=“box-title”>Grafik Harian <?= $this->title = $model->device_name; ?></h3>
</div>
<!-- /.box-header -->
<div class=“box-body”>
<div class=“row”>
<div class=“col-md-12”>
<?= DatePicker::widget([
‘name’ => ‘Test’,
// ‘value’ => ‘2019-Jul-26’,
‘value’ => ArrayHelper::getValue($_GET, “tanggal”),
‘template’ => ‘{addon}{input}’,
‘clientOptions’ => [
‘autoclose’ => true,
‘format’ => ‘yyyy-mm-dd’
]
]);?>
<?php //[
// ‘attribute’=>‘log_date’,
// ‘value’=>‘log_date’,
// ‘format’=>‘raw’,
// ‘filter’=> DatePicker::widget([
// ‘model’ => $model,
// ‘attribute’ => $logs,
// ‘clientOptions’ => [
// ‘autoclose’ => true,
// ‘format’ => ‘yyyy-m-dd’,
// ‘style’ => ‘width: 200px;’, ‘class’ => ‘text-center’
// ]
// ])
// ]
?>
</div>
</div>
</div>
</div>
</div>
</div>


(Ganesh Patel) #2

Do you want to display date from $_GET query string ?


(ramdanmono) #3

yes I want to take the value of the datepicker and place it in my query so that the data displayed is based on the date that I chose


(Ganesh Patel) #4

You have to use javascript for that
you can find some method in date picker widget so when you pick a new date an event will be called
write your code in that method


(Kpsklab) #5

The dates are passed in the input’s, not your code but another example of jquery date picker:

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="css/jquery-ui-1.10.3.custom.css" />
        <script type="text/javascript" src="js/jquery-1.9.1.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.10.3.custom.js"></script>
    </head>
    <body>
        <div style="margin-right:auto;margin-left:0px; width:900px;">


            <form method="post" action="tran_summ4_pdo.php">
                <table style="border:none; width: 700px;">
                    <tr>
                        <td>begin</td>
                        <td>
                            <!--<input type="text" name="dogpic" id="dogpic">-->
                            <input class="odate" name="begindate" type="text" size="30" />
                        </td>

                    </tr>




                    <tr>
                        <td>enddate</td>
                        <td>
                            <input class="odate" type="text" name="enddate" size="30" />
                        </td>
                    </tr>


                </table>

                <p><input type='submit' name='submit' value='Report'></p>
            </form>




        </div>
        <script>
            $(document).ready(function () {

                $(".odate").datepicker({
                    showOn: "button",
                    buttonImage: "/crudv22/public/overcast/images/calendar19.gif",
                    buttonImageOnly: true,
                    dateFormat: "yy-mm-dd",
                    changeMonth: true,
                    changeYear: true
                });
                //});

            });

        </script>    	



    </body>

</html>

The datepicker just fills the fields.

Then in controller the request gets those post values:

Again not a yii example, but you should get the idea:

    public function veryTemp()
    {
        $page = Request::input('page', '1');
        // other request as needed
        $bsdate = Request::input('begindate');
        $esdate = Request::input('enddate');
        $t1 = "b"; // thrown in to demo, report isn't paginated.
        $perpage = "5";
        $offset = ($page - 1) * $perpage;
        $krows = DB::select(); /// Put your count query here
        // above line is a query where you get total count////
        $numrows = $krows[0]->count;
        $pagingQuery = "LIMIT {$offset}, {$perpage}";
        $sql = "select distinct `account_types`.`AccountType` AS `AccountType`,`accounts`.`AccountNumber` AS `AccountNumber`,`accounts`.`AccountName` AS `AccountName`,sum(`transactions`.`Expense`) AS `Sum_Expense`,sum(`transactions`.`Income`) AS `Sum_Income` from ((`account_types` join `accounts` on((`account_types`.`AccountTypeID` = `accounts`.`AccountTypeID`))) join `transactions` on((`accounts`.`AccountID` = `transactions`.`AccountID`))) where (`transactions`.`TransactionDate` Between '$bsdate' and  '$esdate') group by `account_types`.`AccountType`,`accounts`.`AccountNumber`,`accounts`.`AccountName`";

        // a space and  " . $pagingQuery; would be added when paginating:
        //like long query ....  AccountName` " . $pagingQuery;

        $sth = DB::getPdo()->prepare($sql);
        $sth->execute();
        $quy = $sth->fetchAll(\PDO::FETCH_OBJ);
        $report = new LengthAwarePaginator($quy, $numrows, $perpage);
        $pagelinks = ['t1' => $t1, 'page' => $page]; // report has none, for demo only
        // here $pagelinks are appends to query string

        $title = 'Monthly Report';
        $view = 'acct/mreport';
        $layout = ViewLayout::getLayout('acct/reporttp');
        $content = View::make($view)
                ->with('report', $report)
                ->with('pagelinks', $pagelinks);
        return view($layout)->with('content', $content)->with('title', $title);
    }

But notice the dates:

        $bsdate = Request::input('begindate');
        $esdate = Request::input('enddate');

I just get them via the request. Sorry I didn’thave a yii example handy.

But you have to have the dates in the correct format for the database used, mysql is
2019-08-16 for example. Not any other format. The correct format will work in the query.