getting result of data between 2 dates in cake 3 php - php

I'm trying to make search form using cake3 , to get the values between 2 dates . But I'm getting error , and don't know why .
Here's my form :
<?php echo $this->Form->create('CustodyKeys', array('type' => 'get',
'url' => array(
'controller' => 'CustodyKeys','action' => 'resultypeLongTerm'
)));?>
<div class="row">
<div class="form-group col-md-12">
<?php
echo $this->Form->input('keys_custody_status_name', ['placeholder'=>__('Enter Custody statue'),'class' => 'form-control','label'=>__('Custody statue')]);
?>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<?php
echo $this->Form->input('received_date_from', ['type' => 'text', 'placeholder' => __('Received Date From'), 'label' => __('Received Date From'), 'class' => 'form-control hasGorgianDatePicker','value'=>'from']);
?>
</div>
<div class="form-group col-md-6">
<?php
echo $this->Form->input('received_date_to', ['type' => 'text', 'placeholder' => __('Received Date to'), 'label' => __('Received Date to'), 'class' => 'form-control hasGorgianDatePicker']);
?>
</div>
And here's my function at the controller :
$date_start = 'received_date_from';
$date_end = 'received_date_to';
$conditions = $this->CustodyKeys->find(
'all',array('conditions'=>array('CustodyKeys.received_date_from >=' => array($date_start),
'CustodyKeys.received_date_to <=' => array($date_end)))
) ;
$this->set('data', $conditions);
and here's the ctp to show the result of the search :
<?php foreach ($data as $custodyKey): ?>
<tr>
<td><?= $custodyKey->id ?></td>
The debug giving me that result :
'(help)' => 'This is a Query object, to get the results execute or iterate it.',
'sql' => 'SELECT CustodyKeys.id AS CustodyKeys__id, CustodyKeys.holders_keys_id AS CustodyKeys__holders_keys_id, CustodyKeys.keys_management_id AS CustodyKeys__keys_management_id, CustodyKeys.keys_custody_type_id AS CustodyKeys__keys_custody_type_id, CustodyKeys.keys_custody_status_id AS CustodyKeys__keys_custody_status_id, CustodyKeys.received_date AS CustodyKeys__received_date, CustodyKeys.return_date AS CustodyKeys__return_date, CustodyKeys.last_status_date AS CustodyKeys__last_status_date, CustodyKeys.notes AS CustodyKeys__notes FROM custody_keys CustodyKeys WHERE (CustodyKeys.received_date_from >= :c0 AND CustodyKeys.received_date_to <= :c1)',
'params' => [
':c0' => [
'value' => [
(int) 0 => 'received_date_from'
],
'type' => null,
'placeholder' => 'c0'
],
':c1' => [
'value' => [
(int) 0 => 'received_date_to'
],
'type' => null,
'placeholder' => 'c1'
]
],

To convert a query object into an array you can work with use:
$data->toArray()

try this
$conditions = $this->CustodyKeys->find('all',
array('conditions'=>
array('CustodyKeys.received_date_from >= '. $date_start,
'CustodyKeys.received_date_to <= '. $date_end))
);

Related

Trying to loop through an array key and display it

Array:
$forms = array (
'title' => 'Form Title',
'subtext' => 'Sub Text',
'fields' => [
[
'name' => 'Question A',
'type' => 'input',
'placeholder' => 'Eg. Test',
'value' => '',
'required' => true,
'size' => '6'
],
[
'name' => 'Question B',
'type' => 'textarea',
'placeholder' => 'Eg. Test',
'value' => '',
'required' => true,
'size' => '6'
]
]
);
Code:
<?php
$forms_keys = array_keys($forms['fields']);
foreach ($forms_keys as $forms_key)
{
?>
<div class="form-group col-md-12">
<label for="question3"><?php echo $forms_key['name']; ?></label>
</div>
<?php
}
?>
Im trying to get the fields => name to display inside the label. I tried the above but cant get it working, When I echo out $forms_key, I get "0" and "1"
From PHP Manual:
array_keys — Return all the keys or a subset of the keys of an array
You have an array of keys but no associated data as you've stripped this out. Remove array_keys from your first line:
<?php
$forms_keys = $forms['fields'];
foreach ($forms_keys as $forms_key)
{
?>
<div class="form-group col-md-12">
<label for="question3"><?php echo $forms_key['name']; ?></label>
</div>
<?php
}
?>
Your $forms['fields'] array does not have keys, it contains two array elements which contains keys for their inner elements. You can check them with $forms["fields"][0]["name"] or $forms["fields"][1]["name"].
Remove the array_key and set it as below and fields is a sub array. You need to take of it also.
<?php
$forms_keys = $forms['fields'];
for ($i = 0; $i < count($forms_keys); $i++) {
?>
<div class="form-group col-md-12">
<label for="question3"><?php echo $forms_keys[$i]['name']; ?></label>
</div>
<?php
}
?>

How to read only some form fields in php using yii framework

I am new at php and yii framework.can any one help me with my form.I have a update form which has 3 fields with drop down menu.how to make the from field value read only.it will be very much helpfull if any one provide me with code.here is my update form code:
Update form:
<div class="row">
<?php
$criteria=new CDbCriteria();
echo $form->dropDownListGroup(
$model,
'user_id',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(User::model()->findAll($criteria), 'id', 'user_id'),
'dataProvider'=>$model->searchByUserId(Yii::app()->user->getId()),
'htmlOptions' => array('prompt'=>'Select'),
)
)
); ?>
</div>
<div class="row" id="jobTitle">
<?php
$criteria = new CDbCriteria;
$criteria->condition = "status= 'active'";
echo $form->dropDownListGroup(
$model,
'job_title_id',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(JobTitle::model()->findAll($criteria), 'id', 'name'),
'htmlOptions' => array('prompt'=>'Select job title'),
)
)
); ?>
</div>
<div class="row" id="file_name">
<?php echo $form->textFieldGroup(
$model,'file_name',
array(
'wrapperHtmlOptions' => array(
'class'=> 'col-sm-5',
),
)
);
?>
</div>
<div class="row" id="statustype">
<?php
$is_current = array('yes'=>'Yes', 'no'=>'No');
echo $form->dropDownListGroup(
$model,
'is_current',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $is_current,
'htmlOptions' => array('prompt'=>'Select a status'),
)
)
); ?>
</div>
To make a value readonly - add 'readonly'=>true to the options array of the field you want to make readonly.
For example:
<?php
$is_current = array('yes'=>'Yes', 'no'=>'No');
echo $form->dropDownListGroup(
$model,
'is_current',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $is_current,
'htmlOptions' => array('prompt'=>'Select a status'),
)
'readonly' => true
)
);
?>
It's impossible to set select element readonly. With Yii you can set it disabled and add value into hidden field using unselectValue.
echo $form->dropDownListGroup(
// ...
array(
// ...
'widgetOptions' => array(
'htmlOptions' => array(
'disabled' => 'disabled',
'unselectValue' => $model->user_id,
),
)
)
);
Or use TbSelect2 it has readonly property.

Remove model name from submitted form url (GET)

Here's my form
<?php $form=$this->beginWidget('booster.widgets.TbActiveForm',array(
'id'=>'listing-main-form',
'enableAjaxValidation'=>false,
'action'=>Yii::app()->createUrl('site/search'),
'method'=>'get',
)); ?>
<div class="form-group" style="padding-bottom:0px;border:none">
<label class="control-label" for="selecttype">Type</label>
<?php echo $form->dropDownListGroup(
$model,
'prp',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(Type::model()->findAll(), 'id', 'type'),
'htmlOptions' => array('id'=>'selecttype'),
),
'label' => ''
)
); ?>
</div>
<div class="form-group">
<div id="resproperties">
<div class="resdv">
<?php echo $form->checkboxListGroup(
$model,
'rs',
array(
'widgetOptions' => array(
'data' =>CHtml::listData(ResourceCategory::model()->findAll(), 'id', 'res_category'),
),
'label' => ''
)
); ?>
</div>
</div>
............
............
When the form is submitted, I can read all the field's data fine. But the url appears with Model[field] for each fields and looks very ugly (see below). Is there any where I can remove the model name from there?
index.php?r=site/search&ItemModel[prp]=1&ItemModel[rs]=&ItemModel[rs][]=2&ItemModel[rs][]=3&ItemModel[rs][]=4&ItemModel[cm] ............
You can explicitly set input name.
...
'htmlOptions' => array(
'id'=>'selecttype',
'name' => 'fieldname'
)
...
Also you can override CHtml and CActiveForm classes.
In your array for each element, add
'name'=>'your_custom_name'
So...
<?php echo $form->dropDownListGroup(
$model,
'prp',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(Type::model()->findAll(), 'id', 'type'),
'htmlOptions' => array('id'=>'selecttype'),
),
'label' => '',
'name' => 'customName'
)
); ?>

Autocomplete in yii2

In Yii2 I want one of my input field to be autocomplete when user starts to type.Below is my code which uses Jui Autocomplete.
<?php
$items= ArrayHelper::map(Company::find()->all(), 'c_id', 'name');
echo AutoComplete::widget([
'model' => $model,
'attribute' => 'company',
'clientOptions' => [
'source' => $items,
],
]);?>
This is not working.When i printed my array, i got like
Array ( [1] => abc [2] => xyz [4] => pqr )
I got it working when i manually set like
$items=['abc','xyz','pqr'];
The reason may be my c_id's are not ordered?But i want to get the c_id value to be submitted!Any idea how to fix this?
This can be solved with the help of a hidden field input.Hope this will help somebody!
<?php
use yii\web\JsExpression;
$data = Company::find()
->select(['name as value', 'name as label','c_id as id'])
->asArray()
->all();
echo AutoComplete::widget([
'name' => 'Company',
'id' => 'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'4',
'select' => new JsExpression("function( event, ui ) {
$('#user-company').val(ui.item.id);
}")
],
]);
?>
<?= Html::activeHiddenInput($model, 'company')?>
Autocomplete just helps you fill the field with required value.
If you need to submit c_id look to dropdownList or Select2 plugin.
Check this http://demos.krajee.com/widget-details/select2 yii2 widget for ideas.
Here example code:
<?php
use kartik\widgets\Select2;
use app\models\Modelname;
$model = new Modelname;
$data = ['qwe1'=>'color1','key2'=>'color3']
?>
<?= Html::beginForm() ?>
<?= Select2::widget([
'model' => $model,
'attribute' => 'color',
'data' => array_merge(["" => ""], $data),
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
<?= Html::endForm() ?>
It also supports ajax loaded data: http://demos.krajee.com/widget-details/select2#ajax
I wanted to use the Jui Autocomplete so that when I click or focus on autocomplete textbox, it should display options.
I wrote following code and it seems to be working
$floorOptionsArray = ['Basement', 'Ground Floor', 'First floor', 'Second floor', 'Third floor'];
// $floorOptionsArray = array_combine($floorOptionsArray, $floorOptionsArray);
$model = new Customer();
echo $form->field($model, 'floor')
->widget(\yii\jui\AutoComplete::classname(), [
'value' => (!empty($model->floor) ? $model->floor : ''),
'clientOptions' => [
'source' => $floorOptionsArray,
'enabled' => true,
'minLength' => 0
],
'options' =>
[
'placeholder' => 'Floor',
'class' => 'form-control autocomplete-input-bg-arrow ',
'onclick' => "(function ( ) {
$( '#customer-floor' ).autocomplete( 'search', '' );
})();",
'onfocus' => "(function ( ) {
$( '#customer-floor' ).autocomplete( 'search', '' );
})();",
],
])->label(true);

CakePHP Making a form to be global with validation

My application has contacts controller with add action which able to post contacts message to the database table comments. It works fine with its validation.
Now I want to use the add view (The contacts form) as a global form to be rendered in all pages of the application.
I know, to do that, I have to make an element contains the form (or the add view) as follows:
elements/contact.ctp
<div class="panel">
<h4><?php echo __('contact'); ?></h4>
<?php echo $form->create('Contact',array('action'=>'index', 'class' => ''));?>
<?php echo $form->input('name', array('size' => 45, 'class' => 'input-text', 'label' => array( 'text' => __('name',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php echo $form->input('email', array('size' => 45, 'class' => 'input-text', 'label' => array('text' => __('email',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php echo $form->input('subject', array('type' => 'select', 'options' => array(null => __('choose subject',true), 'g' => __('general', true), 'r' => __('report', true)), 'class' => 'input-text', 'label' => array('text' => __('subject', true).'<sup>*</sup>'),'error' => array('class' => 'error', 'wrap' => 'small'))); ?>
<?php echo $form->input('content', array('class' => 'input-text', 'style' => 'height: 140px', 'title' => __('message', true), 'label' => array('text' => __('message',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php //echo $fck->load('Contact.message','Mini'); ?>
<span>
<?php
App::import('Vendor','FoxCaptcha', array('file' => 'Fox_captcha.php'));
$cap = new Fox_captcha(120,30,5);
$cap->image_dir = $html->url('/').'img/';
$cap->lines_amount = 13;
$cap->en_reload = $html->image('reload.png', array('alt' => __('reload', true), 'title' => __('reload the captcha', true), 'id' => 'frel', 'style' => 'vertical-align:middle'));
?>
</span>
<div>
<span class="display:inline"><?php echo $cap->make_it('HTML');?></span>
<?php echo $form->input('vcode', array('size' => 45, 'class' => 'small input-text', 'label' => array('text' => __('captcha', true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
</div>
<?php echo $form->submit(__('send',true), array('class' => 'nice medium radius white button'));?>
<?php echo $form->end();?>
<div class="alert-box warning"><?php echo __('fields label with * are required');?></div>
</div>
The problem is: When I use this form from any page (for example posts/view/22) it submits to contacts/add. I want after submit posts/view/22 is rendered with any validation message triggered.
There is no solution for this requirement without Ajax and JSON. It includes submitting the form with Ajax to the contacts controller index action with event handler component activated and then check isAjax then render JSON output retrieved by the post's view and then populates the results.

Categories