yii how to combine CJuiAutoComplete option select with php variable - yii

How do I include a PHP variable in a CJuiAutoComplete jquery? My sample below was not successful. Sorry for my bad English.
<?php
echo $count=$t['id'];
$this->widget('zii.widgets.jui.CJuiAutoComplete',
array(
'model'=>$model,
'attribute'=>"jwtn".$t["id"],
'sourceUrl'=>$this->createUrl('KemsainsCalon/lookup'),
'htmlOptions'=>array('placeholder'=>$model->isNewRecord ? "Nama ahli" : "ok"),
'options'=>
array(
'showAnim'=>'fold',
'select'=>"js:function(parsoalan, ui) {
// below is my problem, #KemsainsCalon_jwtn_id<?php echo $count?>, how do I concat php variables in jquery
$('#KemsainsCalon_jwtn_id<?php echo $count?>').val(ui.item.id);
}"
),
'cssFile'=>'jquery-ui.css',
));
?>
`

You can handle $count like you would in a normal php string. select may be javascript code but it's still a php string for now.
...
'select'=>"js:function(parsoalan, ui) {
$('#KemsainsCalon_jwtn_id".$count."').val(ui.item.id);
}"

Related

YII dependent dropdown is working fine on create but on update first dropdown data is coming but second dropdown data is not showing

<div class="row">
<?php echo $form->labelEx($model,'ClassId'); ?>
<?php
$School=Yii::app()->session['Schoolid'];
echo CHtml::activeDropDownList($model,'ClassId',CHtml::listData(Classdetails::model()->findAll(array("condition"=>"classid >0 and School_Id='$School' and Status=1","order"=>"classid")),'classid','classname'),
array(
'empty'=>'--Select a Class--',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Studentmarks/Dynamiccities'), //url to call.
'data'=>array('Classid'=>'js: $(this).val()'),
'update'=>'#'.CHtml::activeId($model,'pid'),
)));
echo $form->error($model,'ClassId');
echo $form->labelEx($model,'pid');
echo CHtml::activeDropDownList($model,'pid', array(),array('prompt'=>'-- Select a Student --'));
echo $form->error($model,'pid');
?>
</div>
As per the code specified, you are explicitly setting the second dropdown empty which is why it's not working in update view.
In update mode, you need to simply populate the dropdown with possible values with the saved value selected.
You can probably do something like this.
if($model->isNewRecord)
echo CHtml::activeDropDownList($model,'pid', array(),array('prompt'=>'-- Select a Student --')); // create
else
echo CHtml::activeDropDownList($model,'pid', $model->populateStudentsDate($model->ClassId)); // update
In populateStudentsDate($model->ClassId) method you need to filter and show the list of students as per class selection.

changing my forms get url params

i made a simple form with checkboxlist() when i submit the form using get it goes to the next page, but the url is nasty looking.
http://mysite/dev/research/abc/all/?Abc[type]=N&Abc[type][]=1&Abc[type][]=2&Abc[type][]=3&yt0=Search
how would i change it to something like this?
http://mysite/dev/research/abc/all/?type=N&type[]=1&type[]=2&type[]=3
or if possible something nicer looking like
?type=1,2,3
note that i don't want &yt0=Search either
in my controller
public function actionIndex()
{
$model=new ABC;
$this->render('index',array(
'model'=>$model
));
}
in my view i have this
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'shop',
'action'=>$this->createUrl('/modulename/abc/all'),
'method'=>'get',
'enableAjaxValidation'=>false,
));
echo $form->checkBoxList($model,'type', $arr,
array('separator'=>'',
'template'=>'<div class="col-md-6 col-xs-12">{input} {label}</div>',
'uncheckValue'=>N,
)
);
echo CHtml::submitButton('Search', array('class'=>'btn btn-success'));
echo CHtml::button('Clear Filter', array('class'=>'btn btn-link', 'type'=>'reset'));
$this->endWidget();
?>
UPDATE:
figured it out. had to add 'name'=> 'type'
but how do i remove &yt0=Search
echo $form->checkBoxList($model,'type', $arr,
array('name'=> 'type',
'separator'=>'',
'template'=>'<div class="col-md-6 col-xs-12">{input} {label}</div>',
'uncheckValue'=>N,
you could use CHtml:button to not post the &yt0=Search
<?php
echo CHtml::button('Search',
array(
'submit'=>array('/modulename/abc/all'),
'class'=>'btn btn-success'
)
);
?>

Bigcommerce API getOrders() returns empty array

There is already a question posted on this topic, but no answer that works for me.
Bigcommerce PHP API - No data returned
I can connect to the online store from my PHP code, but GetOrders() returns an empty array
A json_encode gives me [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
getOrdersCount() returns 47317, so I know there are orders in there.
Any ideas?
Thanks. Here's my code:
<?php
require '...../bigcommerce.php';
use Bigcommerce\Api\Client as Bigcommerce;
Bigcommerce::configure(array(
'store_url' => 'https://store-xxxx.mybigcommerce.com',
'username' => 'xxxxx',
'api_key' => 'xxxxx'
));
Bigcommerce::setCipher('RC4-SHA');
Bigcommerce::verifyPeer(false);
$count = Bigcommerce::getOrdersCount();
echo 'number of orders:' . $count;
$orders = Bigcommerce::getOrders();
foreach($orders as $order) {
echo $order->name;
echo $order->price;
}
?>
Your problem is that "name" and "price" are not fields that belong to orders. That is why it is returning that data. For each order it is echoing empty (non existent) fields,
Instead try:
$orders = Bigcommerce::getOrders();
foreach($orders as $order) {
echo $order->id;
echo $order->total_inc_tax;
}

Yii CActiveForm expects object and not array

My Goal:
I am trying to filter a model, based on a GET parameter, and populate a form based on this.
What I require is that when users select the update icon on the Gridview
I will
grab the 'telephone number' they want to edit,
populate a form with the telephonenumber's data
allow the user to edit this data for the telephonenumber and submit
I then run my own custom sql update query based on the new data.
My Problem
My gridview can successfully capture the telephonenumber for a selected row.
It can successfully send this to the Controller Update method (see below)
I cannot however filter a model based on this telephonenumber and then populate a form with this model.
My Error
get_class() expects parameter 1 to be object, array given
/framework/web/helpers/CHtml.php(2220)
/framework/web/helpers/CHtml.php(2220): get_class(array())
/framework/web/helpers/CHtml.php(1236): CHtml::resolveName(array(), "TelephoneNumbers_TelephoneNumber")
/framework/web/widgets/CActiveForm.php(562): CHtml::activeLabelEx(array(), "TelephoneNumbers_TelephoneNumber", array())
/views/dateAudiid/editupdateform.php(18): CActiveForm->labelEx(array(), "TelephoneNumbers_TelephoneNumber")
/framework/web/CBaseController.php(126): require("/var/www/OMReport/protected/views/dateAudiid/editupdateform.php")
Here's my Gridview.
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'dateaudiidcondensed-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{update}',
'buttons'=>array
(
'update' => array
(
'url'=>'Yii::app()->createUrl("dateAudiid/Update/",array("TelephoneNumbers_TelephoneNumber"=>$data->TelephoneNumbers_TelephoneNumber))',
),
),
),
'TelephoneNumbers_TelephoneNumber',
'FormId_Formid',
'Date',
'AudibeneID_Audibene_ID'
),
));
Here's my Controller
public function actionUpdate($TelephoneNumbers_TelephoneNumber)
{
$criteria=new CDbCriteria;
$criteria->compare('TelephoneNumbers_TelephoneNumber',$_GET['TelephoneNumbers_TelephoneNumber'],true);
$criteria->limit = 1;
$criteria->offset = 1;
$model = DateAudiidCondensedByAudibeneId::model()->findAll($criteria);
$this->render('editupdate',array('model'=>$model));
}
Here's my View
<?php
/* #var $this DateAudiidController */
/* #var $model DateAudiidCondensedByAudibeneId */
?>
<h1>Update Assignments </h1>
<?php echo $this->renderPartial('editupdateform', array('model'=>$model)); ?>
Here's my Form
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'date-audiid-condensed-by-audibene-id-customupdate-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php
echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'TelephoneNumbers_TelephoneNumber'); ?>
<?php echo $form->textField($model,'TelephoneNumbers_TelephoneNumber'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'FormId_Formid'); ?>
<?php echo $form->textField($model,'FormId_Formid'); ?>
<?php echo $form->error($model,'FormId_Formid'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Date'); ?>
<?php echo $form->textField($model,'Date'); ?>
<?php echo $form->error($model,'Date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'AudibeneID_Audibene_ID'); ?>
<?php echo $form->textField($model,'AudibeneID_Audibene_ID'); ?>
<?php echo $form->error($model,'AudibeneID_Audibene_ID'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My Thoughts / What I've tried so far
I am returning the model as an array...but my form wants it as an object.
I need to change something in the way I filter my model in the Controller Update action, however I cannot see how to do this.
All my other methods use similar model filtering code.
here is what you can do to fix this :
your form is trying to use models labeling but you have given an array, ->findAll() will return an array of models, here you need a single object of model
the error your having is because $model in filter of your grid needs to be an object of model which here will try to validate using this model, so won't work when you are giving it an array of your models,
if you want to filter the results based on something, you need to do this where the dataprovider of your grid is being populated, so in this case, is in $model->search()
get the parameter and append it to that criteria
so your grid could look like this:
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'dateaudiidcondensed-grid',
'dataProvider' => $model->search(), //create a new model with search scenario
'filter' => $model, // here use that model to validate fields
'columns'=>array(
.
.
.
),
));
and in your models search method:
public function search() {
$criteria = new CDbCriteria;
// grab the sent data and use it here
$tel = Yii::app()->request->getParam('TelephoneNumbers_TelephoneNumber' , null);
if(!empty($tel))
$criteria->compare('TelephoneNumbers_TelephoneNumber' , $tel , true);
.
.
.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Here's what eventually worked for me
The usage of 'findByAttributes' to filter and return a model to the Form
public function actionUpdate($TelephoneNumbers_TelephoneNumber)
{
$model = DateAudiidCondensedByAudibeneId::model()->findByAttributes(array("TelephoneNumbers_TelephoneNumber" => $_GET['TelephoneNumbers_TelephoneNumber']));
$this->render('editupdate',array('model'=>$model));
}

Display name instead of Id in Yii

I want to display the name of Shift instead of shift_id
I have a dropboxlist from other table that is like this
<div>
<?php echo $form->labelEx($model,'mon'); ?>
<?php echo $form->dropDownList($model, 'mon', CHtml::listData(
Shift::model()->findAll(), 'shft_id', 'name'),
array('prompt' => 'Select a Department')
); ?>
<?php echo $form->error($model,'mon'); ?>
I have two tables that is
Day: id_day,mon,tues,wed,etc
Shift: shft_id,start,end,name,status
Here is the relation in the day
'shift'=>array(self::HAS_MANY,'Shift','shft_id'),
For Shift:
'day'=>array(self::BELONGS_TO,'Day','id_day'),
It is already working. The choices in the dropbox was the name of the Shift, and puts the shft_id in the mon,tues,wed,etc. In the view of the form it looks like
id_user: 3
mon:5
tues:5
wed:6
what I wanted to be is that in the view.
id_user: 3
mon: 6am-5pm
tues: 7am-6pm
etc.etc.
I dont know what command it is. I have no idea. Help me please
Instead of User::getusername() method.
I think you can simply use this in one line.
// format models resulting using listData
<?php echo $form->dropdownlist($model,'user_id', CHtml::listData(User::model()->findAll(),'id', 'name')); ?>
below is an example which I think may solved your problem
in _form.php
<?php echo $form->dropdownlist($model,'user_id', User::getusername()); ?>
<?php echo $form->error($model,'user_id'); ?></th>
in User model
public function getusername()
{
$criteria2=new CDbCriteria;
$criteria2->select='*';
$quser=User::model()->findAll($criteria2);
foreach($quser as $r)
{
$user_id= $r->user_id;
$user_name=$r->user_name;
$user_array[$user_id]=$user_name;
}
return $user_array;
}
I'm trying to understand what you are really asking. I believe you were saying that you are already able to populate your records correctly from the dropdowns. But when you are displaying, the view is only showing the shift_id value? The relation for the day should be able to get your shift name. How are you displaying the list:
mon: 5
tue: 5
wed: 6
I'm trying to understand if you need help displaying the dropdown items properly, or how to display the shift name later after the values have been set in the database. Also, your 'Day' table has me confused. What are the field names? Are they 'day_id' and 'day'? Or do you actually name the fields after each day of the week?
If you are displaying the data through a foreach and are getting each day (let's assume it is $day) object, then the relationship to 'shift' can give you the name like this: echo $day->shift->name; if the name displays as '#am-#pm'. Otherwise you can display it like this:
echo sprintf('%d a.m. - %d p.m.',$day->shift->start,$day->shift->end);
Thanks for the people who helped me. It did lead me to the answer anyone who has the same problem here is what i did.
In my model/Day
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'monday'=>array(self::BELONGS_TO,'Shift','mon'),
'tuesday'=>array(self::BELONGS_TO,'Shift','tue'),
'wednesday'=>array(self::BELONGS_TO,'Shift','wed'),
'thursday'=>array(self::BELONGS_TO,'Shift','thurs'),
'friday'=>array(self::BELONGS_TO,'Shift','fri'),
'saturday'=>array(self::BELONGS_TO,'Shift','sat'),
'sunday'=>array(self::BELONGS_TO,'Shift','sun'),
);
}
and In my models/Shift
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'day_mon' =>array(self::HAS_MANY,'Day','mon'),
'day_tue' =>array(self::HAS_MANY,'Day','tue'),
'day_wed' =>array(self::HAS_MANY,'Day','wed'),
'day_thurs'=> array(self::HAS_MANY,'Day','thurs'),
'day_fri'=>array(self::HAS_MANY,'Day','fri'),
'day_sat'=>array(self::HAS_MANY,'Day','sat'),
'day_sun'=>array(self::HAS_MANY,'Day','sun'),
);
}
The problem is with my relations. its not set properly, so you need to set it properly then go to Day/view
<?php $this->widget('bootstrap.widgets.TbDetailView',array(
'data'=>$model,
'attributes'=>array(
'id_day',
array(
'name'=>'mon',
'value'=>CHtml::encode($model->monday->name)
),
Here is the output
If you have a detail veiw then use the following code:
widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
array(
'label' => $model->shift->getAttributeLabel('shift_name'),
'value' => $model->shift->shift_name
),)); ?>
In case anybody else will be in my boat, what you will need to do is edit view.php in view/modelname/view.php
widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
'country_id')); ?>
into
widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
array(
'label' => $model->country->getAttributeLabel('country'),
'value' => $model->country->name
),)); ?>