How to add a select option based upon database table to a View(form)
Step 1:
In the Controller (foo_controller) which will be used to produce the
View(Form) "foo.cbt" that needs a select option populated by table
values
add
var $uses - array('Foo', 'Bar')
after
var $name = 'Foo';
where 'Foo' is the primary Model(table) for the form and 'Bar' is the
Model(table) which will populate the select option.
Step 2:
In that same Controller (foo_controller) add the following code to
collect the data for the select option.
Into the function viewname(){
$data_array = $this->Bar->find('all',array(fields => array('Bar.id',
'Bar.fieldname1'))); //For all of the records in the table
or
$data_array = $this->Bar-findAll('field = value', array('Bar.id',
'Bar.fieldname1')); //For selected records in the table
and
$data_combine = Set::combine($data_array,
'{n}.Bar.id','{n}.Bar.fieldname');
//The following makes the key,value array available to the view.
$this->set('selectdata',$data_combine);
Step 3
In the viewname View(form) the following structure creates the select
option with the Key Value pair that displays the "name" and passes the
"id" to the database.
echo $form->input('id', array('options' => array($selectdata)));
Wayne
On Jun 24, 11:15 am, Wayne Madison <[EMAIL PROTECTED]>
wrote:
> Hello,
>
> I want to create cakePHP(1.2.0.xxxx) Forms to "View", "Add", "Edit",
> "Delete" a "User".
>
> Constraints: 1 (No "Scaffold")
> 2 (No Deprecated "generateList()")
>
> I want the "Add" and "Edit" form to contain:
>
> 1) A select option box for "title" based upon the values in the
> "titles" table showing field values for "Title" saving "title_id" to
> the "users" table.
> 2) An input box for first_name
> 3) An input box for last_name
>
> I want the "User" "View" form to show:
>
> 1) The "title" selected not the "title_id"
> 2) The "first_name" value
> 3) The "last_name" value
>
> Table One: "Users"
>
> user_id integer primary key auto number
> title_id integer,
> first_name varchar(40)
> last_name varchar(40)
>
> Table One: Data Example:
> user_id titile_id first_name last_name
> 1 1 bill smith
> 2 2 jane murphy
> 3 3 elizabeth baker
>
> Table Two: "Titles"
> title_id: integer primary key autonumber
> title: varchar(40)
>
> Table Two: Data Example:
> title_id title
> 1 Mr.
> 2 Ms.
> 3 Mrs.
>
> Thanks
>
> Wayne
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---