如何创建一个函数来在cakephp中调用多个ctp文件

问题描述 投票:0回答:1

我创建了12个不同的函数和12个不同的ctp文件,使用12个月的12个选项卡,如January ... December,将数据库中的值显示到我的网页中。我想显示每个月的三个最高价值。根据cakephp,我们为不同的CTP文件创建了不同的功能。像一月一样,我创建了一个函数名一月,并创建了一个CTP文件名january.ctp,像其他十一个月一样,我使用了具有同一个月明智CTP文件的月份名称来创建了不同的不同函数。每个函数都不同,是否有任何方法可以编写一个函数并将变量发送到12个ctp文件中。如何提高代码效率,我在下面添加了代码

<?php
namespace App\Controller;
use Cake\Core\Configure;
use App\Controller\AppController;
use Cake\ORM\TableRegistry; 

public function january()
{
      $userDetails=Configure::read('userdetails');
      $profile=Configure::read('profile');
      $this->loadModel('Users');
      $usersID = $this->Auth->user('id');
      $profileImageShow=$this->Users->getUploadPath($usersID);
      $categoryTable=TableRegistry::getTableLocator()->get('categories');
      $results=$categoryTable->find("all")->toList();
      $votes;
      $month  = 01;
      $year=date("Y");
      $categoriesArray;

      foreach($results as $result)
      {        
            $votesCount=$this->Sjvotes->find("all",[
                'conditions'=>['month'=> $month,'year'=>$year,'category_id'=>$result->id]])->select(['count' => $this->Sjvotes->find()->func()->count('*')])->group(['user_id','category_id'])->order(['count'=>'desc'])->limit(2)->toList();

            $categoriesArray[]=$result->id;

            $temp=0;
            foreach($votesCount as $key => $count)
            {
               if($temp!=$count->count)
               {
               $userVotes=$this->Sjvotes->find("all", ['conditions'=>['year'=>$year,'category_id'=>$result->id]])->
                select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname', 'profile_image'=>'Users.profile_image'])
                ->contain(['Users'])
                ->group(['user_id','category_id'])
                ->having(['count'=>$count->count])
                ->order(['count'=>'desc'])
                ->toList();
                $temp=$count->count;

                if($key==0)
                {
                   $class="gold";
                }else 
                    if($key==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                $votes[$result->id][$class]=$userVotes;
               } 
            }
      }     

        $voteToppers=$this->Sjvotes->find("all", ['conditions'=>['year'=>$year,'category_id in ' =>$categoriesArray]])->
            select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname', 'profile_image'=>'Users.profile_image'])
            ->contain(['Users'])
            ->group(['user_id'])
            ->order(['count'=>'desc'])
            ->toList();
        $temp=0;
        $changes=0;
        foreach($voteToppers as $key => $topper)
        {
              if($temp!=$topper->count)
              {
                if($changes==0)
                {
                   $class="gold";
                }else 
                    if($changes==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                    $temp=$topper->count;
                    $changes++;
              }
              if($changes>3)
              {
                break;
              }

              $topUsers[$class][]=$topper;
        }
       $this->set(compact("results"));
       $this->set(compact("votes"));
       $this->set(compact(["topUsers", "userDetails", "profile"]));

}

//For February 

public function february()
{
      $userDetails=Configure::read('userdetails');
      $profile=Configure::read('profile');
      $this->loadModel('Users');
      $usersID = $this->Auth->user('id');
      $profileImageShow=$this->Users->getUploadPath($usersID);
      $categoryTable=TableRegistry::getTableLocator()->get('categories');
      $results=$categoryTable->find("all")->toList();
      $votes;
      $month  = 02;
      $year=date("Y");
      $categoriesArray;

      foreach($results as $result)
      {        
            $votesCount=$this->Sjvotes->find("all",[
                'conditions'=>['month'=> $month,'year'=>$year,'category_id'=>$result->id]])->select(['count' => $this->Sjvotes->find()->func()->count('*')])->group(['user_id','category_id'])->order(['count'=>'desc'])->limit(2)->toList();

            $categoriesArray[]=$result->id;

            $temp=0;
            foreach($votesCount as $key => $count)
            {
               if($temp!=$count->count)
               {
               $userVotes=$this->Sjvotes->find("all", ['conditions'=>['month'=>$month,'year'=>$year,'category_id'=>$result->id]])->
                select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname', 'profile_image'=>'Users.profile_image'])
                ->contain(['Users'])
                ->group(['user_id','category_id'])
                ->having(['count'=>$count->count])
                ->order(['count'=>'desc'])
                ->toList();
                $temp=$count->count;

                if($key==0)
                {
                   $class="gold";
                }else 
                    if($key==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                $votes[$result->id][$class]=$userVotes;
               } 
            }
      }     

        $voteToppers=$this->Sjvotes->find("all", ['conditions'=>['month'=>$month,'year'=>$year,'category_id in ' =>$categoriesArray]])->
            select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname', 'profile_image'=>'Users.profile_image'])
            ->contain(['Users'])
            ->group(['user_id'])
            ->order(['count'=>'desc'])
            ->toList();
        $temp=0;
        $changes=0;
        foreach($voteToppers as $key => $topper)
        {
              if($temp!=$topper->count)
              {
                if($changes==0)
                {
                   $class="gold";
                }else 
                    if($changes==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                    $temp=$topper->count;
                    $changes++;
              }
              if($changes>3)
              {
                break;
              }

              $topUsers[$class][]=$topper;
        }
       $this->set(compact("results"));
       $this->set(compact("votes"));
       $this->set(compact(["topUsers", "userDetails", "profile"]));
}

//March 

public function march()
{
      $userDetails=Configure::read('userdetails');
      $profile=Configure::read('profile');
      $this->loadModel('Users');
      $usersID = $this->Auth->user('id');
      $profileImageShow=$this->Users->getUploadPath($usersID);
      $categoryTable=TableRegistry::getTableLocator()->get('categories');
      $results=$categoryTable->find("all")->toList();
      $votes;
      $month  = 03;
      $year=date("Y");
      $categoriesArray;

      foreach($results as $result)
      {        
            $votesCount=$this->Sjvotes->find("all",[
                'conditions'=>['month'=> $month,'year'=>$year,'category_id'=>$result->id]])->select(['count' => $this->Sjvotes->find()->func()->count('*')])->group(['user_id','category_id'])->order(['count'=>'desc'])->limit(2)->toList();

            $categoriesArray[]=$result->id;

            $temp=0;
            foreach($votesCount as $key => $count)
            {
               if($temp!=$count->count)
               {
               $userVotes=$this->Sjvotes->find("all", ['conditions'=>['month'=>$month,'year'=>$year,'category_id'=>$result->id]])->
                select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname', 'profile_image'=>'Users.profile_image'])
                ->contain(['Users'])
                ->group(['user_id','category_id'])
                ->having(['count'=>$count->count])
                ->order(['count'=>'desc'])
                ->toList();
                $temp=$count->count;

                if($key==0)
                {
                   $class="gold";
                }else 
                    if($key==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                $votes[$result->id][$class]=$userVotes;
               } 
            }
      }     

        $voteToppers=$this->Sjvotes->find("all", ['conditions'=>['month'=>$month,'year'=>$year,'category_id in ' =>$categoriesArray]])->
            select(['count' => $this->Sjvotes->find()->func()->count('*'), 'category_id','user_id','fname'=>'Users.firstname','lname'=>'Users.lastname','profile_image'=>'Users.profile_image'])
            ->contain(['Users'])
            ->group(['user_id'])
            ->order(['count'=>'desc'])
            ->toList();
        $temp=0;
        $changes=0;
        foreach($voteToppers as $key => $topper)
        {
              if($temp!=$topper->count)
              {
                if($changes==0)
                {
                   $class="gold";
                }else 
                    if($changes==1)
                    {
                        $class="silver";
                    }else 
                        {
                            $class="bronz";
                        }

                    $temp=$topper->count;
                    $changes++;
              }
              if($changes>3)
              {
                break;
              }

              $topUsers[$class][]=$topper;
        }
       $this->set(compact("results"));
       $this->set(compact("votes"));
       $this->set(compact(["topUsers", "userDetails", "profile"]));

}

//april
php cakephp cakephp-3.0
1个回答
1
投票

唯一的区别是$month变量的值?

public function january() {
    return $this->_month(1, 'January');
}

public function february() {
    return $this->_month(2, 'February');
}

// etc...

protected function _month($month, $monthName) {
    // Existing implementation of each month's function here,
    // except without initializing the $month variable

    $this->set(compact('results', 'votes', 'topUsers', 'userDetails', 'profile', 'month', 'monthName'));
    $this->render('month');
}

然后将january.ctp重命名为month.ctp,您现在可以在其中使用$month$monthName变量来告诉您它是哪个月份。

© www.soinside.com 2019 - 2024. All rights reserved.