I have two tables store and users.They have one to one relation.After bake store I get user list in a select box.I want to make all selection to check box because farther I want to select multiple users for one store.Now I just need to convert this select box to check box.
I have tried
<?php
echo $this->Form->input('mad_stores_id');
?>
To
<?php
echo $this->Form->checkbox('mad_stores_id');
?>
But this is giving me only one check box.I need to display all option which have given in selection box.
Here is the controller find methods
$users = $this->UserStoreSelection->Users->find('list',array('fields' => array('id','username')));
How can I show all select option in check box ?
If you're relationship is base on One to One, then you shouldn't allow end users to select multiple users for store. That would be a hasMany relationship.
Anyway, here you go
<?php echo $this->Form->input('mad_stores_id', array(
'multiple' => 'multiple')); ?>
Edit: if its multiple checkboxed yo want, then its the following:
<?php echo $this->Form->input('mad_stores_id', array(
'multiple' => 'checkbox')); ?>
Related
Suppose I have 3 tables property_details, property_feature_details, property_feature_relations.
PropertyDetail Model has the hasMany relation with PropertyFeatureRelation.
AT the time saveing my data array looks like that:
$this->data = array('PropertyDetail' => array('name'=>'xyz'),'PropertyFeatureRelation' => array('0' => array('feature_id'=>1),'1' => array('feature_id'=>5),'2' => array('feature_id'=>0),'3' => array('feature_id'=>0)));
Those feature_ids values are coming out from the checkboxes. Those are checked they are contains ids value and non checked are having 0 value. But in the child table saves all the data zero and non zeros.
Actually I want to save those checkboxes values which are only checked. Please do not provide any manual controller logic. Help me.
I am explaining you with my example which is having question ,answer and its survey
you can use multiple checkbox syntax as below:
$answers=array();
$tmp_ans=$question['Answer'];
for($j=0;$j < count($tmp_ans);$j++)
$answers[$tmp_ans[$j]['id']]=$tmp_ans[$j]['answer'];
echo '<li>';
echo $this->Form->input('Answer', array(
'type' => 'select',
'name'=>'Answer['.$question['Question']['id'].']',
'class'=>'test',
'label'=>false,
'multiple' => 'checkbox',
'options' =>$answers ,
));
echo '</li>';
after that you will have only those answers which you have checked others will not come in array of post so to save data you can use below syntax
if(!empty($this->data['Answer'])){
foreach ($this->data['Answer'] as $key=>$val):
if(!empty($val)):
if(is_array($val)):
foreach ($val as $ans):
$post_data['UserAnswer']['id']='';
$post_data['UserAnswer']['survey_id']=$id;
$post_data['UserAnswer']['user_id']=$this->Auth->user('id');
$post_data['UserAnswer']['question_id']=$key;
$post_data['UserAnswer']['answer_id']=$ans;
$this->UserAnswer->save($post_data['UserAnswer']);
endforeach;
else:
$post_data['UserAnswer']['id']='';
$post_data['UserAnswer']['survey_id']=$id;
$post_data['UserAnswer']['user_id']=$this->Auth->user('id');
$post_data['UserAnswer']['question_id']=$key;
$post_data['UserAnswer']['answer_id']=$val;
$this->UserAnswer->save($post_data['UserAnswer']);
endif;
endif;
endforeach;
}
created a view function and any time i click the link to view a template, the url at the top of the page is correct but it spits out the same list of fields in the database.
the fields are
accounts - id, company name, abn
template - id, name, description, account_id
field - id, name, field type, template_id
function view(){
$accounts=$this->User->AccountsUser->find('list',
array('fields'=>array('id', 'account_id'),
'conditions' =>array('user_id' =>
$this->Auth->user('id'))));
$templates=$this->Template->find('first',
array('conditions' => array(
'Template.account_id' => $accounts)));
$fields=$this->Field->find('all',
array('conditions' => array(
'Field.template_id' => Set::extract('/Template/id', $templates))));
$this->set('template', $templates);
$this->set('account', $accounts);
$this->set('field', $fields);
}
here is the view
<div class = "conlinks">
</br></br></br></br></br><h2>Here is your template fields</h2></br>
<?php foreach($field as $fields): ?>
<tr>
<td align='center'><?php echo $fields['Field']['name']; ?>
</tr></br>
<?php endforeach; ?>
</div>
so the problem is its grabbing the exact same list of fields, not the correct template_id when it prints out the fields
You should be able to debug this for yourself. Just narrow the bug down step by step.
For starters, in your view function, do a print_r on the following variables, and make sure each one contains a logical result:
$accounts
$templates
$fields
If you find unexpected results there, I'd be looking at the parameters you pass into each of your finds, and making sure they're OK. You're passing in $accounts as an array to your find condition - make sure it matches the format that cake expects. Do the same for Set::extract('/Template/id', $templates).
Also look at the SQL that Cake is producing.
If you're not already using it, I'd highly recommend installing Cake's Debug Kit Toolbar - https://github.com/cakephp/debug_kit/ because it makes debugging variables and SQL much easier.
If you do the above steps and can't solve your problem, you should at least be able to narrow it down to a line or two of code. Update your answer to show what line or two is causing the problem, and include print_r's of some of the variables you're working with. That should help others on StackOverflow to give you a specific answer.
Hope that helps!
the issue was I wasn't getting the parameters when click the link
function view($name){
$fields = $this->Template->Field->find('list',array(
'fields'=> array('name'),
'conditions' => array(
'template_id'=> $name)));
$this->set('field', $fields);
}
and the view
<div class = "conlinks">
</br><h2>Here is your template fields</h2>
<?php foreach($field as $name): ?>
<tr>
<td align='center'>
<?php echo $name; ?>
</tr></br>
<?php endforeach; ?>
</br>
<?php
echo $this->Html->link('Back', '/templates/view', array('class' => 'button'));?>
</div>
So, I have models associated via HABTM. In one view, I'm using checkboxes to save associations. I can save data no problem. But what I can't determine is how cleanly check boxes of pre-existing associations, say, when I go onto an Edit page and want to edit associations.
In this case, I have a User model and a Survey model.
Here's the code for my view:
foreach ($users as $user) {
echo $this->Form->input('User.User.', array('type' => 'checkbox', 'value' => $user['User']['id'], 'hiddenField' => false, 'label' => false )) . ' ' . $user['User']['username'];
}
There must be a way to simply mark boxes as selected where appropriate.
I am very new to CakePHP. Could you please explain me the steps needed to populate a select drop-down with values from the database. Please also suggest me some links to the reference.
Simple, if it's a related model in your controller you pass 'list' into the find(); an cake will make an id => value array for you, and the form helper will know exactly what to do with it.
For example say you want to get the list of categories for a product model, this is in your contoller:
$categories = $this->Product->Categories->find('list');
$this->set(compact('categories'));
Then in your view using the form helper, simply create the select element how you normally would any input:
$form->input('category_id');
The form helper will automatically load the $categories variable we set with $this->set().
You make a find in the db and then set the variable via $this->set(yourvariable) in the controller.
In the view you use the "yourvariable" in the select tag
Fill select 1
Fill select 2
Fill select 3
There is a cakeish way to do it with very less effort
$this->set('arrMain',$this->Post->find('list',
array(
'fields'=>array('id','title')
)));
Will give output as ==>
<select id="UserAge" name="data[User][Age]">
<option value="1">The title</option>
<option value="2">A title once again</option>
<option value="3">Title strikes back</option>
in the controller you do:
$this->loadModel('MyModel'); //if it's not already loaded
$options = $this->MyModel->find('all'); //or whatever conditions you want
$this->set('options',$options);
and in the view
<select...>
<?php foreach ($options as $option): ?>
<option value="<?php echo $option['MyModel']['id']; ?>"><?php echo $option['MyModel']['field']; ?></option>
</select>
Example
in your controller Class
$categories = $this->Articles->find('list')->select(['id', 'title'])->toArray();
$this->set(compact('categories')); // pass result dataset to the view
In Your view file
<?php echo $this->Form->select('articles_categories_id', $categories); ?>
Reading Source
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html
I am very new in cake php, i want to know how to create form in cake php,please describe,when we go to create a form then what i have to do,like create model and controller everything
In the view file, something like this would work:
<?php
echo $this->Form->create();
echo $this->Form->input('firstname', array('label' => 'Enter your first name:'));
echo $this->Form->input('email', array('label' => 'Enter your email address:'));
echo $this->Form->input('password', array('label' => 'Enter your password:'));
echo $this->Form->end('Save');
?>
In your controller:
if($this->request->is('post')){
$this->User->save( $this->request->data );
}
You can apply some sort of validation in your model, look in the documentation for that.
Best option to learn about cakephp is it's own doc book
But I'm providing you some basic code to create form :
$this->Form->create('ModelName');
$this->Form->input('ModelName.fieldname', array('type'=>'text', 'label'=>'Modified-Name'));
$this->Form->end(__('Submit'));
Here array('type'=>'text'...): type shows which type of input field you want.
(...'label'=>'Modified-Name'): By default it shows field text as fieldname but by using 'label' you can modify your field text.
$this->form->create('controlpage',
array(
'action'=>'controll',
'class'=>'class',
'enctype' => 'multipart/form-data',
'onsubmit'=>'return valid()'
));
Block quote
Create form in html save it as ctp
Block quote
And call it in view. enter code hereUse cake php book to read further.