简体   繁体   中英

cakephp: How can i add dropdownlist in default.ctp?

In default.ctp I want to add two dropdownlists. One is list of year from 2010 to 2020 and the another one is a list of month from January to December.

How can i do this ?

Do i have to create form for this ? like

echo $this->Form->create...
echo $this->Form->end..

Or can i just add two dropdownlists and a search button ?

The if i click the "Search" button it will go for posts/archive action and display those posts with that "year-month" in "created" column of posts table.

Can anyone post sample code for this?

This is what i tried so far : $listyear = array();

        for($i=2010;$i<=2020;$i++){
            array_push($listyear,$i);
        }


        $track=array(31,28,31,30,31,30,31,31,30,31,30,31);

        $months=array(1=>'January',
                      2=>'February',
                      3=>'March',
                      4=>'April',

                      5=>'May',
                      6=>'June',
                      7=>'July',
                      8=>'August',

                      9=>'September',
                      10=>'October',
                      11=>'November',
                      12=>'December');                                                  

        echo $this->Form->create('Post', array('url' => array('controller' => 'posts', 'action' =>'listarchive')));
        echo $this->Form->input('Select year',array('type'=>'select','options'=>$listyear) );           
        echo $this->Form->input('Select month',array('type'=>'select','options'=>$months) );
        echo $this->Form->end('Search');

Yes, you need to create a form (or use JavaScript, but a normal HTML form is easier and more reliable). You can use Form::year() to create a dropdown list for a year and Form::month() for months: http://book.cakephp.org/view/1416/year and http://book.cakephp.org/view/1417/month .

If you have problems with creating the form, it'd be better if you posted what you have tried and asked specific questions. Forms are covered quite extensively in the Cookbook ( http://book.cakephp.org/view/1383/Form ).

EDIT after seeing code: You could just do something like this and let Cake do the work:

echo $form->create( 'Post', array( /* whatever you need */ ) );
echo $form->year( 'year', 2010, 2020 );
echo $form->month( 'month' );
echo $form->end( 'Search' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM