php 设计问题 - 助手会在这里提供帮助吗?

发布于 2024-10-04 15:17:44 字数 1285 浏览 4 评论 0原文

我必须列出来自数据库源 A 的几个元素,它们是: team_id、team_name 和 team_score(为了解释而翻译)。

我需要循环它们并显示该信息。

所以,我在 DAO 方面:

public function listOfTeams()
{
  $select = $this->select()
    ->from(array('t'=>'teams'), array('cod_team','name','score'));
  return $this->fetchAll($select);

}

在我的团队控制器上:

public function listAction()
{
  $teamsDao = new TeamsDao();
  $this->view->infoTeam = $teamsDao->listOfTeams();                    
}

在视图上:

<?php for($i = 0; $i < 30; $i++): ?>

  <?php if(isset($this->infoTeam[$i])): ?>

现在,问题是,在每个列出的项目上,我需要添加更多信息。

该信息并非直接来自数据库,而是一些计算的结果。

以完成游戏的百分比为例。 (翻译);

$totalGamesNumber > 0 ? ($gamesDone*100)/$totalGamesNumber : 0);

因此,我需要从数据库中获取总比赛数,然后,对于每个球队,我需要完成比赛数,以便可以显示百分比。

$gamesDone 是通过以下方式获得的:

$gameTeamDao->countGamesPerTeam($gameVo, $teamVo);

我被困在这里,因为我看不到我应该在哪里/如何调用并制定计算百分比的方法,以便允许完成游戏的百分比与其他数据。

我可以从这个泥巴中得到一些帮助吗?

如果你必须为此编写一个助手,或多或少,它会是什么样子?

提前致谢, MEM

PS - 如果您需要更详细的信息。我可以提供。我可能会忘记一些对我来说已经被拿走的东西,但对于那些想要帮助它的人来说却不是。所以,请告诉我。再次非常感谢。

更新:为了帮助起见,将所有帖子翻译成英文。

I have to list several elements that are coming from a database source A and they are:
team_id, team_name and team_score (translated for explanation sake).

I need to loop over them, and display that information.

So, I have, on the DAO side:

public function listOfTeams()
{
  $select = $this->select()
    ->from(array('t'=>'teams'), array('cod_team','name','score'));
  return $this->fetchAll($select);

}

On my team controller:

public function listAction()
{
  $teamsDao = new TeamsDao();
  $this->view->infoTeam = $teamsDao->listOfTeams();                    
}

And at the view:

<?php for($i = 0; $i < 30; $i++): ?>

  <?php if(isset($this->infoTeam[$i])): ?>

Now, the thing is, on each of those listed items, I need to add more information.

That information doesn't come directly from a database, but it's a result of some calculations.

Take for example the percentage of games done. (translated);

$totalGamesNumber > 0 ? ($gamesDone*100)/$totalGamesNumber : 0);

So, I need to grab the total games number from the database, then, for each team, I need to have the number of games done, so that I can have the percentage to be displayed.

The $gamesDone are obtained by:

$gameTeamDao->countGamesPerTeam($gameVo, $teamVo);

I'm stuck here, because I cannot see where/how should I call and make the method for calculating the percentage, in order to allow the percentage of games completed to be presented along with the other data.

Can I have some help out of this mud ?

If you had to write a helper for this, more or less, how will it looked like?

Thanks in advance,
MEM

PS - If you need more detailed information. I can provide. I could be forgeting something that for me is taken, but that, for those who want to help it isn't. So, just let me know. Thanks a lot again.

Update: translated all post to English for help sake.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万人眼中万个我 2024-10-11 15:17:44

由于您使用的是 ZF,因此您可以在表行的类中进行所有这些计算。方法如下。假设您的团队类名为 Application_Model_DbTable_Teams。

class Application_Model_DbTable_Teams extends Zend_Db_Table_Abstract
{
    protected $_name = 'teams'; // table name
    protected $_id = 'teamId'; // table primary key

    // rows returned by fetchAll() will be of this class
    protected $_rowClass = 'Application_Model_DbRow_Teams';

}

然后,您需要创建 Application_Model_DbRow_Teams 类,您将在其中进行额外的计算。

class Application_Model_DbRow_Teams extends Zend_Db_Table_Row_Abstract
{
    public $percentGamesDone;

    public function init()
    {
        // This method gets called automatically by ZF when you instantiate a new
        // object from this class
        // This is where you will put your extra calculations, 
        // like percentage games done, etc
        $this->percentGamesDone = $this->getPercentGames();
    }

    public function getPercentGames() 
    {
        $calculation = 3; // replace this with real data
        return $calculation;
    }
}

完成此操作后,您可以直接从视图中使用任何行声明的属性(例如percentGameDone),因为它们将在您实例化时立即计算行。

Since you are using ZF, you could make all this computations in the table row's class. Here's how. Let's say your teams class is called Application_Model_DbTable_Teams.

class Application_Model_DbTable_Teams extends Zend_Db_Table_Abstract
{
    protected $_name = 'teams'; // table name
    protected $_id = 'teamId'; // table primary key

    // rows returned by fetchAll() will be of this class
    protected $_rowClass = 'Application_Model_DbRow_Teams';

}

Then you need to create the Application_Model_DbRow_Teams class where you will put the extra calculations

class Application_Model_DbRow_Teams extends Zend_Db_Table_Row_Abstract
{
    public $percentGamesDone;

    public function init()
    {
        // This method gets called automatically by ZF when you instantiate a new
        // object from this class
        // This is where you will put your extra calculations, 
        // like percentage games done, etc
        $this->percentGamesDone = $this->getPercentGames();
    }

    public function getPercentGames() 
    {
        $calculation = 3; // replace this with real data
        return $calculation;
    }
}

Once you've done this, you can simply use any of the row's declared properties (like percentGameDone) directly from the view as they will be calculated right when you instanciate the rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文