每个动画可以有一个(只有一个)主题链接,其他主题可以创建,也没有动画链接。在这个表格的底部,我怎样才能把每个主题的数量相加呢?这将根据列的数量显示“Array”。(请原谅我的法语)
--------------------------------------------------------------------------
| 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”是求和的数量。
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
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)中显示每个主题的数量:
foreach ($animations as $animation)
{
foreach ($themes as $theme)
{
echo '<td id="' . $theme->getId() . '">';
if ($animation->getTheme() == $theme->getId())
{
echo $animation->getDemiJournees() . '</td>';
} else {
echo ' ';
}
echo '</td>';
}
}第二循环:每个主题列中的总和(错误:显示“Array”):
foreach ($themes as $theme)
{
echo '<td id="' . $theme->getId() . '">';
foreach ($animations as $animation)
{
if ($animation->getTheme() == $theme->getId())
{
echo $animationManager->sumTheme();
}
}
echo '</td>';
}发布于 2020-02-14 20:02:06
找到它:更改第二个循环,创建一个变量来存储每个主题的和:
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>';
}```(移动的答案不恰当地发布为编辑到问题)
https://stackoverflow.com/questions/59964136
复制相似问题