The database is like this. Owner has many cats.
Owner ownerID ownerName
Cat catID catType ownerID
I am trying to add the new cat and on the field of ownerID, I want to show the droplist of all the ownerName. How can I do that?
I'm assuming that you already have owner model, for dropdown list of owner
In Conrtoller
$Owner = $this->Owner->find('list', array('fields' => array('Owner.ownerID', 'Owner.ownerName')));
$this->set('owners', $owner);
In View
<?php echo $form->input('owner', array(
'label' => false,
'id' => 'owner_id',
'options' => $owners
)); ?>
Write in your contoller:
$all_security_question = $this->Security_question->find('list',array('fields'=>array('id','question')));
$this->set("all_security_question",$all_security_question);
Write in your Model:<?php echo $this->Form->input('security_question_id', array(
'label' => false,
'id' => 'id',
'options' =>$all_security_question
)); ?>
Related
in my project I have database
"Products" hasAndBelongsToMany "Sizes"
"ProductsSize" belongsTo "Products"
"ProductSize" belongsTo "Size"
In ProductsSize, one product_id may have many size_id.
hence, i want to do a form that have a select type input that list down all the size_id where the product_id = $id.
What i have done is:
in controller:
$size = $this->Product->ProductsSize->find('all', array(
'conditions' => array('ProductsSize.product_id' => $id),
'fields' => array('ProductsSize.size_id'),
));
in view:
<?php echo $this->form->create('Cart', array('action' => 'add')); ?>
<?php echo $this->form->input('size_id', array('label' => 'Size', 'options' => $size)); ?>
then i got error: Undefined index: ProductsSize
but when i put foreach, the data shown:
<?php foreach ($size_apparel as $size): ?>
<?php echo $size['ProductsSize']['size_id'];?><br/>
<?php endforeach; ?>
can anyone please help me to do the foreach in options.
The problem is that you are using an invalid array for the options. You are requesting:
$size = $this->Product->ProductsSize->find('all', array(
'conditions' => array('ProductsSize.product_id' => $id),
'fields' => array('ProductsSize.size_id'),
));
What you should be requesting is:
$size = $this->Product->ProductsSize->find('list', array(
'conditions' => array('ProductsSize.product_id' => $id),
));
This will return the options for the form field in the format it expects which is:
array(
{id} => {size},
{id} => {size},
{id} => {size},
{id} => {size},
...
)
I think you meant to say
<?php echo $this->form->input('size_id', array('label' => 'Size', 'options' => $size)); ?>
note 'options' instead of 'value'
$List = Set::classicExtract($size, '{n}.ProductSize.product_id');
$List will contain key value(ProductSize.product_id) pair.
OR you can change your query as
$size = $this->Product->ProductSize->find('list', array(
'conditions' => array('ProductSize.product_id' => $id),
));
Hope it will work for you
now i got a table contact.
the contact have 2 row which is id and name.
now i got 3 user in my contact,
1 A
2 B
3 C
question.
how do i make a select input as the code of below ( in cakephp ):
<select name="contact" id="UserField">
<?php for($i=1;$i<=3; $i++) ?>
<option value="1"><?php echo $contact['Contact']['name']; ?></option>
</select>
In your controllers action you can set a list of the names
$names = $this->Contact->find('list', array(
'contain' => array(),
'fields' => array(
'Contact.id',
'Contact.name'
),
'order' => array(
'Contact.name' => 'ASC'
)
));
$this->set(compact('names'));
And then in your view
echo $this->Form->input('contact', array(
'label' => 'Contact',
'options' => $names
));
I am new to cake php.I have to enter multiple city id in a database with comma separator like 10,11,12 where 10,11,12 is city id
When I use this code
**<?php
echo $form->input('city_id', array('options' => $city, 'class'=>'input_box2',
'empty' => 'Please select a city'), null, array('id' => 'city_id', 'label' => 'City'
));**
I am able to insert one city id at a time,but i need to insert multiple city id with comma separator .
I am useing the following code but it is not working.please help
**<?php
echo $form->input('city_id', array('options' => $city, 'type'=>'select','multiple'
=> 'true', 'empty' => 'Please select city'), null, array('id' => 'city_id', 'label' => 'City'));**
You need to modify POST data in your controller:
in CakePHP 2.x
$city_ids = Set::extract('/YourModel/city_id', $this->request->data);
$this->request->data['YourModel']['city_id'] = implode(",", $city_ids);
in CakePHP 1.x
$city_ids = Set::extract('/YourModel/city_id', $this->data);
$this->data['YourModel']['city_id'] = implode(",", $city_ids);
How can I create two radio buttons with one being preselected based on the value of $foo? The snippet below creates them fine but does not select either of the two buttons.
$options = array('standard' => ' Standard','pro' => ' Pro');
$attributes = array(
'legend' => false,
'value' => false,
'checked'=> ($foo == "pro") ? FALSE : TRUE,
);
echo $this->Form->radio('type',$options, $attributes);
It's simple.. use the default value to $foo:
$options = array(
'standard' => 'Standard',
'pro' => 'Pro'
);
$attributes = array(
'legend' => false,
'value' => $foo
);
echo $this->Form->radio('type', $options, $attributes);
As you can see on the documentation:
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::radio
you should preselect the value for any form field from the controller
#see http://www.dereuromark.de/2010/06/23/working-with-forms/ "Default Values"
This is the way to go
$attributes = array();
$options = array('standard' => 'Standard', 'pro' => 'Pro');
if($foo === 'pro') {
$attributes['default'] = 'pro';
}
echo $this->Form->radio('type', $options, $attributes);
A better Solution is to set the defaults in the controller as Mark has pointed. That way you can set defaults at the end of your controller's action like...
Let's assume your Model is Member with membership_type field
$this->data['Member']['membership_type '] = 'pro';
For CakePHP 3.x, the following syntax should work.
$options = array('Y'=>'Yes','N'=>'No');
$attributes = array('div' => 'input', 'type' => 'radio', 'options' => $options, 'default' => 'Y');
echo $this->Form->input('add to business directory',$attributes);
HTH
I have two models, Category and Point. The associations are defined as:
Category hasMany Point
Point belongsTo Category
I would like, when adding Points to my database, to be able to select the category it belongs to from a <select> box, along with the rest of the form data.
Where would I need to set the category list and how could I do it? And how would I produce the select box?
I assume it could be done with
$form->input('categorieslist',array('type'=>'select')); //categorieslist needs
//setting somewhere.
Also to generalize a bit:
In a View with access to the Form helper
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
));
?>
The above will render a select input with two options. You can also place an empty option as the first item. Passing a value of true will simply append an empty option with a blank value to the beginning of the options rendered in the HTML.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'key1' => 'val1',
'key2' => 'val2',
),
'empty' => true,
));
?>
You can pass a string to the 'empty' key to have it display custom text as the key field for the empty option.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'California' => 'CA',
'Oregon' => 'OR',
),
'empty' => 'choose a state',
));
?>
One last example, you can also pre-select an option with the selected key. The value should match the value of one of the select options, not the key.
<?php
echo $form->input( 'dataKey', array(
'type' => 'select',
'options' => array(
'California' => 'CA',
'Oregon' => 'OR',
),
'empty' => 'choose a state',
'selected' => 'California',
));
?>
From the Model
Model->find( 'list', array( ... )); will always return an array formatted for use with select box options. If you pass data to your view stored in a variable with a lowercase plural model name, that is, ( $this->set( 'categories', $categories );, then you will automagically generate drop downs for related models by using the form helper in the view and passing it a data index of the same model name in singular form suffixed with "_id".
Aziz's answer at #2 is the example of that automagic kicking in.
CakePHP 1.3 Form Helper
CakePHP1.2 Form Helper
In the controller:
$categories = $this->Point->Category->find('list');
$this->set(compact('categories'));
In the view:
$form->input('category_id',array('type'=>'select'));