why i am getting only rating of one business? - php

<?php
$business=$model->reviewBusinesses;
foreach ($business as $business) {
$c = $business->rating; ?>
<div class="ratings"> <!--use class in order to show rating horizontally -->
<?php
$this->widget('ext.DzRaty.DzRaty', array(
'name' => 'rating',
'value' => $c,
'options' => array(
'readOnly' => TRUE,
),
));
?>
</div>
<?php } ?>
I am working in yii and using dzraty extension for star rating. In one of my view file which is userbusiness.php of business table which has relationship with revewbusiness table. I am getting rating from reviewbusiness table and showing it in one of my view file of business as mentioned above. I am getting rating in numeric form, but from above code i can only get rating of 1st review in star form while the rest of the rating is coming in numeric form. Can someone find my error?
like this
first_star second_star third_star fourth_star fifth_star
2
3
4
5
For those who really want to understand, in reviewbusiness table, i am getting rating, reviews of users.

Try this (you are using business as data set and as key at the same time):
<?php $business = $model->reviewBusinesses;
$i = 0;
foreach ($business as $business_key) {
$c = $business_key->rating;
?>
<div class="ratings"> <!--use class in order to show rating horizontally -->
<?php
$this->widget('ext.DzRaty.DzRaty', array(
'name' => 'rating_'.$i,
'value' => $c,
'options' => array(
'readOnly' => TRUE,
),
'htmlOptions' => array(
'id' => 'rating_'.$i,
),
)); ?>
</div>
<?php
$i++;
}
?>

Related

CakePHP: Search for products after we have the array of categories' ID

at the moment I have a list of products and each product has an array of categories (every array has a least 1 category object), I also made a filter for the categories so when I chose them as checkboxes then clicked submit, I'll get an array of the categories' ID. But now I don't really know how I can retrieve all the resources that have categories that match with the filtered ones. I'm very sorry if my explanation is not very clear.
Here is my code:
filter.php
<section class="section service border-top">
<?= $this->Form->create(null, ['novalidate' => true, 'type' => 'get']) ?>
<fieldset>
<?php echo $this->Form->control('categories._ids', array('multiple' => 'checkbox', 'options' => $categories));?>
</fieldset>
</br>
<?= $this->Form->button(__('Filter'), ['class' =>'logbtn']) ?>
<?= $this->Form->end() ?>
</section>
productsController.php
public function customerAllProducts()
{
$products = $this->paginate($this->Products);
$this->paginate = [
'contain' => ["Categories"],
];
$categories_filter = $this->request->getQuery('categories._ids');
if(!empty($categories_filter)){
foreach ($categories_filter as $category_filter){
echo $category_filter; // Print out all filtered categories' IDs
}
}
$categries = $this->Productss->Categoriess->find('list', ['limit' => 200]);
$this->set(compact('products','categories'));
}
Thank you

Mulit select widget post large data in form

I am using XMultiSelects Widget to post large data in form.(Yii Framework)
<div class="row">
<?php echo $form->labelEx($model, 'list'); ?>
<div style="display: inline-block">
<?php
$this->widget('multiselects.XMultiSelects', array(
'id' => 'hd-programs', //since there are multiple selector pairs on this page we need to set identifier
'leftTitle' => 'Available',
'leftName' => 'availableList',
'leftList' => $availableList,
'rightTitle' => 'Selected',
'rightName' => get_class($model) . '[list][]',
'rightList' => $list,
'size' => 15,
'width' => '350px',
));
?>
<?php echo $form->error($model, 'list'); ?>
</div>
where $avaliableList is very large data set.
Now when i print value of form using print_r($_Post['MyFormName']) $list is not posted.
Any suggestion how could i handle large data with multiselect widget.

cakephp control dropdown population from database

I'm trying to populate a dropdown/select tag in cakephp,fortunately I was able to populate it with the values coming from my database,
however it displays ALL. How can I limit the population to a specific ID?
To make things clear here's what I want to achieve:
Apples:
form start
dropdown-contains only values that are associated with apples from the DB
button
form end
Currently here's how it looks like
Apples:
form start
dropdown-contains all fruits values from the DB
button
form end
Here's my current code tnx any help/suggestions is very much appreciated
Controller:
$fruits = $this->Model1->Model2->find('list',
array('fields' =>
array('id',
'fruit_name',
//'conditions' => array(''=>'')
)));
$this->set('fruitsList', $fruits);
View:
echo $this->Form->input('Model1.salad_fruits_id',
array('type' => 'select',
'options' => $fruitsList,
));
If you have association between models between models it would be easier. Just add conditions to second model.
//if you don't have association between models
$this->Model1->bindModel(array(
'belongsTo' => array( //example binding
'Model2' => array(
'className' => 'Model2',
'foreignKey' => 'model2_id',
)
)
));
$fruits = $this->Model1->find('list', array(
'conditions' => array(
'Model2.name' => 'apple'
),
'fields' => array(
'id','fruit_name',
)
));

Saving data of dependent drop-down - Yii

I am implementing a dependent drop-down and want to save their IDs in the table CANDIDATE.
I have a table named as CENTER which has two columns ID, NAME.
My other table is DISTRICT which has three columns ID, NAME and CENTER_ID.
By selecting the CENTER , I am populating the DISTRICT drop-down but the district_id is not being saved in the table CANDIDATE .
_form
<div class="row">
<?php echo $form->labelEx($model, 'district_id'); ?>
<?php
$dist = CHtml::listData(Center::model()->findAll(array('order' => 'id')), 'id', 'name');
echo $form->dropDownList($model, 'center_id', $dist, array(
'prompt' => '–select district–',
'ajax' => array('type' => 'POST',
'url' => CController::createUrl('candidate/districts'),
'update' => '#center_id',
)
));
?>
<?php
echo CHtml::dropDownList('center_id','', array());
?>
</div>
CandidateController
public function actionDistricts() {
$centers = District::model()->findAll('center_id =:id', array(':id' => (int) $_POST['Candidate']['center_id']));
$return = CHtml::listData($centers, 'id', 'name');
foreach ($return as $centerId => $centerName) {
echo CHtml::tag('option', array('value' => $centerId), CHtml::encode($centerName), true);
}
}
1) It seems like your url is not properly created:
'url' => CController::createUrl('districts')
Try:
'url' => CController::createUrl('candidate/districts')
2) The form you post:
$form->dropDownList($model, 'district_id', $dist...
It posts district_id to CandidateController/actionDistricts(), where you do following:
$centers = Center::model()->findAll('id=:id', array(':id' => (int) $_POST['Candidate']['center_id']));
I can't see any input containing center_id, moreover, firing Center::model()->findAll('id:id'..) doesn't make sense, if you search by primary key (id), do Center::model()->findByPk($id);
Anyways, back to the problem, your firebug log sais it all, it can't read center_id form $_POST variable, so to finish it up, try rewriting actionDistricts().
I can't guess what kind of relation (one district, several centers per 1?) your db has, so i wont be able to fully fix the problem here. But you should rewrite
$centers = Center::model()->findAll('id=:id', array(':id' => (int) $_POST['Candidate']['center_id']));
to something that expects $_POST['Candidate']['district_id'].
Replace
'url' => CController::createUrl('districts'),
By
'url' => $this::createUrl('candidate/districts'),

Cakephp - saving multiple checkboxes to a join table

I need help with saving multiple checkboxes to a join table.
There are 3 tables:
Customers (id,name, address)
Products (id,product_name,desc) all products are entered.
Customers_Products (id,product_id,customer_id) <-(join table)
Step 1:
Customer selects the desired products (apples,oranges,cherries in checkbox)
and click <Next> button
Step 2:
Fills out the form about themselves (name, address and etc).
and click <Submit> button
Step 3:
I am saving personal info from Step 2 to Customers table and selected products id(s) from Step 1 to Customers_Products table.
It does save the personal info and product id to designated tables, but when customer selects more than one products, it does not save the products id.
it adds row but the products id field is empty.
Here is my view:
<?php echo $this->Form->checkbox('CustomerProduct.product.', array('value' => '1', 'style' => 'float: left; display: inline')); ?>
<?php echo $this->Form->checkbox('CustomerProduct.product.', array('value' => '2', 'style' => 'float: left; display: inline')); ?>
<?php echo $this->Form->checkbox('CustomerProduct.product.', array('value' => '3', 'style' => 'float: left; display: inline')); ?>
<?php echo $this->Form->input('Customers.name', array('value'=>'Jon Toshmatov')); ?>
Controller:
$this->Prospect->saveAll($this->request->data);
Model:
public $hasAndBelongsToMany = array(
'CustomerProduct' => array(
'className' => 'CustomerProduct',
'joinTable' => 'customers_products',
'foreignKey' => 'customer_id',
'associationForeignKey' => 'product_id',
'unique' => 'false',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
Desired result:
I want save the data in the following order:
Customers table:
id name
1 Jon T
Customers_Products *(join table)*
id product_id customer_id
1 4 1
2 3 1
3 5 1
The problem is that each checkbox will also add a 'hidden' input if created via the CakePHP formhelper. This hidden input is added to make sure that checkboxes always send a value (0 or 1) even if they are not checked.
However, in your example, you're creating 3 checkboxes with the same 'name', therefore each checkbox will overwrite the previous value.
The best way is to create a 'multiple' checkbox with the CakePHP formhelper;
echo $this->Form->input('Product', array('multiple' => 'checkbox'));
For this to work properly, there should be a $products viewVar, containing product-ids and labels;
Inside your controller:
public function some_action()
{
// your code here
$this->set('products', $this->Customer->Product->find('list'));
}
More on saving HABTM data here;
http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm
Note
unless you're using non-standard names for your models, it seems your hasAndBelongsToMany is incorrect;
I suspect your relation is Customer hasAndBelongsTo Product, not CustomerProduct?
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

Categories