php - 如何在cakephp中创建一个函数来调用多个ctp文件
问题描述
我创建了 12 个不同的函数和 12 个不同的 ctp 文件,用于使用 12 个月 12 Tabs LIKE January ... December 将数据库中的值显示到我的网页中。我想显示每个月的三个最高值。根据 cakephp,我们为不同的 CTP 文件创建了不同的函数。就像一月份一样,我创建了一个函数名称 January 并创建了一个 CTP 文件名 january.ctp 并且像剩下的 11 个月一样,我使用月份名称和相同月份的 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
解决方案
唯一的区别是$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
变量来告诉您它是几月的。
推荐阅读
- algorithm - 这种光线投射算法有什么缺陷?
- c - printf 不会打印双精度类型的数组值
- c++ - 使用具有私有可访问性的重载继承函数声明
- python - 在 Python 中读取文件并打印出来
- python - Python中的表情符号
- powershell - 如何访问服务器 Powershell 实例中的特定脚本文件?
- mysql - docker在alpine linux中构建python django应用程序时出错
- javascript - 如何在 React js 中尚未安装的组件中显示通知
- google-app-engine - `gcloud app deploy index.yaml` 和 `gcloud datastore index create` 之间的任何区别
- react-admin - 如何在 Windows 上开始使用 React Admin