Dynamic drop down

View: New views
4 Messages — Rating Filter:   Alert me  

Dynamic drop down

by kaushik-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thank you for this help.As per your advice i have changed my
controller to -

$categories = $this->Product->Category->find('list',
array( 'conditions' => array('isActive' => 'Y')));
$this->set(compact('categories'));

And view -

<?php echo $form->select('Product.categoryId', array('options' =>
$categories)); ?>

But it is giving html like this-

<option value=""></option>
<optgroup label="options">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

</optgroup>
</select>

But as per my requirement it should be

<option value="">Select</option>
<option value="1">CategoryName1</option>
<option value="2">CategoryName2</option>
<option value="3">CategoryName3</option>
</select>

What change I have to do? Plz help me.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Dynamic drop down

by Jonathan Snook-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Do you have a displayField set or a field called name?

On Wed, Jul 9, 2008 at 2:02 AM, kaushik <kaushikworld@...> wrote:

>
> Thank you for this help.As per your advice i have changed my
> controller to -
>
> $categories = $this->Product->Category->find('list',
> array( 'conditions' => array('isActive' => 'Y')));
> $this->set(compact('categories'));
>
> And view -
>
> <?php echo $form->select('Product.categoryId', array('options' =>
> $categories)); ?>
>
> But it is giving html like this-
>
> <option value=""></option>
> <optgroup label="options">
> <option value="1">1</option>
> <option value="2">2</option>
> <option value="3">3</option>
>
> </optgroup>
> </select>
>
> But as per my requirement it should be
>
> <option value="">Select</option>
> <option value="1">CategoryName1</option>
> <option value="2">CategoryName2</option>
> <option value="3">CategoryName3</option>
> </select>
>
> What change I have to do? Plz help me.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Dynamic drop down

by kaushik-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Your question is not very clear to me.

Here is my code details:
categories Table:

`categoryId` int(10) NOT NULL auto_increment,
  `categoryName` varchar(100) NOT NULL,
  `isActive` enum('Y','N') NOT NULL default 'Y',
  PRIMARY KEY  (`categoryId`)

products Table:

`productId` int(10) NOT NULL auto_increment,
  `productName` varchar(20) NOT NULL,
  `categoryId` int(10) NOT NULL,
  `isActive` enum('Y','N') NOT NULL default 'Y',
  PRIMARY KEY  (`productId`)

It is my product Model

class product extends AppModel
{
 var $name = 'Product';
 var $primaryKey = 'productId';
 var $validate = array(
       'productName'  => VALID_NOT_EMPTY,
   );

 var $belongsTo = array(
        'Category' => array(
            'className'  => 'category',
            'foreignKey' => 'categoryId',
        )
    );

}

and it is my category model:

class category extends AppModel
{
 var $name = 'Category';
 var $primaryKey = 'categoryId';
 var $validate = array(
       'categoryName'  => VALID_NOT_EMPTY,
   );

}

my product controller is-

$categories = $this->Product->Category->find('list',
array( 'conditions' => array('isActive' => 'Y')));
$this->set(compact('categories'));

And view -

<?php echo $form->select('Product.categoryId', array('options' =>
$categories)); ?>

But it is giving html like this-

<select name="data[Product][categoryId]" id="ProductCategoryId">
<option value=""></option>
<optgroup label="options">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</optgroup>
</select>

But as per my requirement it should be

<select name="data[Product][categoryId]" id="ProductCategoryId">
<option value="">Select</option>
<option value="1">CategoryName1</option>
<option value="2">CategoryName2</option>
<option value="3">CategoryName3</option>
</select>

On Jul 9, 5:02 pm, "Jonathan Snook" <jonathan.sn...@...> wrote:

> Do you have a displayField set or a field called name?
>
> On Wed, Jul 9, 2008 at 2:02 AM, kaushik <kaushikwo...@...> wrote:
>
> > Thank you for this help.As per your advice i have changed my
> > controller to -
>
> > $categories = $this->Product->Category->find('list',
> > array( 'conditions' => array('isActive' => 'Y')));
> > $this->set(compact('categories'));
>
> > And view -
>
> > <?php echo $form->select('Product.categoryId', array('options' =>
> > $categories)); ?>
>
> > But it is giving html like this-
>
> > <option value=""></option>
> > <optgroup label="options">
> > <option value="1">1</option>
> > <option value="2">2</option>
> > <option value="3">3</option>
>
> > </optgroup>
> > </select>
>
> > But as per my requirement it should be
>
> > <option value="">Select</option>
> > <option value="1">CategoryName1</option>
> > <option value="2">CategoryName2</option>
> > <option value="3">CategoryName3</option>
> > </select>
>
> > What change I have to do? Plz help me.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Dynamic drop down

by Jonathan Snook-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


The way the find('list') works is that it tries to find the primary
key and a field that it can use to display as a label in the select.
It looks for "name" by default. To tell CakePHP what field you want to
use, specify it in the Model like so:

class product extends AppModel
{
   var $name = 'Product';
   var $primaryKey = 'productId';
   var $displayField = 'productName';

   [...snip...]
}

If this is a new project, I recommend using CakePHP's naming
convention. It'll help avoid having to specify things explicitly.

Best of luck,

-Jonathan

On 7/9/08, kaushik <kaushikworld@...> wrote:
>
>  Your question is not very clear to me.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@...
To unsubscribe from this group, send email to cake-php-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

LightInTheBox - Buy quality products at wholesale price!