首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何与动态列的金额相加?

如何与动态列的金额相加?
EN

Stack Overflow用户
提问于 2020-01-29 09:32:47
回答 1查看 118关注 0票数 1

每个动画可以有一个(只有一个)主题链接,其他主题可以创建,也没有动画链接。在这个表格的底部,我怎样才能把每个主题的数量相加呢?这将根据列的数量显示“Array”。(请原谅我的法语)

代码语言:javascript
复制
--------------------------------------------------------------------------
            | Theme A  | Theme B  | Theme C  | Theme D  |  Theme n  | ...
Animation 1 |       5  |       -  |       -  |       -  |     ...
Animation 2 |       -  |       -  |       -  |       7  |     ...
Animation 3 |       6  |       -  |       -  |       -  |     ...
Animation 4 |       -  |       -  |       9  |       -  |     ...
Animation 5 |       -  |       -  |       -  |       1  |     ...
Animation n ...
       Sums:       11          0          9          8        ...

在数据库中,“主题”是链接主题的id (来自“主题”表),“demiJournees”是求和的数量。

代码语言:javascript
复制
CREATE TABLE `animations` (
  `id` int(11) NOT NULL,
  `nom` varchar(50) NOT NULL,
  `dateAnim` varchar(11) DEFAULT NULL,
  `theme` int(11) DEFAULT NULL,   // One theme per animation
  `projet` int(11) DEFAULT NULL,
  `partenaires` int(11) DEFAULT NULL,
  `etablissement` int(11) DEFAULT NULL,
  `lieu` int(11) DEFAULT NULL,
  `public` int(11) DEFAULT NULL,
  `effectif` int(11) DEFAULT NULL,
  `demiJournees` int(11) DEFAULT NULL,   // The amount of the theme in an animation
  `matthias` int(11) DEFAULT NULL,
  `noellie` int(11) DEFAULT NULL,
  `benevoles` int(11) DEFAULT NULL,
  `notes` varchar(255) DEFAULT NULL
)

Model/AnimationManager.php

代码语言:javascript
复制
public function sumTheme()
{
    $sql = "SELECT theme, SUM(demiJournees) FROM animations GROUP BY theme";
    $req = $this->db->query($sql);
    $sum = array();
    while ($ligne = $req->fetch())
    {
        $sum[] = new Animation($ligne);
    }
    return $sum;
}

View/animation.php

第一个循环,在动画行(Works)中显示每个主题的数量:

代码语言:javascript
复制
foreach ($animations as $animation)
{
    foreach ($themes as $theme)
    {
        echo '<td id="' .    $theme->getId()  . '">';
        if ($animation->getTheme() == $theme->getId())
        {
            echo  $animation->getDemiJournees() . '</td>';
        } else {
            echo '&nbsp;';
        }
        echo '</td>';
    }
}

第二循环:每个主题列中的总和(错误:显示“Array”):

代码语言:javascript
复制
foreach ($themes as $theme)
{
    echo '<td id="' . $theme->getId() . '">';
    foreach ($animations as $animation)
    {
        if ($animation->getTheme() == $theme->getId())
        {
            echo $animationManager->sumTheme();
        }            
      }
    echo '</td>';
}
EN

回答 1

Stack Overflow用户

发布于 2020-02-14 20:02:06

找到它:更改第二个循环,创建一个变量来存储每个主题的和:

代码语言:javascript
复制
foreach ($themes as $theme) {
    echo '<td id="' . $theme->getId() . '">';

    // Initialise a variable for each theme
    $sumTheme = 0;
    foreach ($animations as $animation) {
        if ($animation->getTheme() == $theme->getId()) {
            // Ajoute la valeur à la variable du thème
            $sumTheme += $animation->getDemiJournees();
        }
    }
    // Display the total
    echo $sumTheme;
    echo '</td>';
}```

(移动的答案不恰当地发布为编辑到问题)

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59964136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档