Php 如何对动态列的金额求和?
每个动画只能链接一个主题,可以创建其他主题,也不能链接任何动画。 我怎样才能把每个主题的数量加在这个表的底部? 根据列的数量显示“数组”。。 请原谅我的法语Php 如何对动态列的金额求和?,php,mysql,Php,Mysql,每个动画只能链接一个主题,可以创建其他主题,也不能链接任何动画。 我怎样才能把每个主题的数量加在这个表的底部? 根据列的数量显示“数组”。。 请原谅我的法语 -------------------------------------------------------------------------- | Theme A | Theme B | Theme C | Theme D | Theme n | ... Animation 1 | 5
--------------------------------------------------------------------------
| 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 ...
在数据库中,“theme”是“themes”表中链接主题的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
View/animation.php
第一个循环,显示动画行中每个主题的数量:
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>';
}
}
第二个循环:每个主题列的总和错误:显示“数组”:
foreach ($themes as $theme)
{
echo '<td id="' . $theme->getId() . '">';
foreach ($animations as $animation)
{
if ($animation->getTheme() == $theme->getId())
{
echo $animationManager->sumTheme();
}
}
echo '</td>';
}
找到它:通过创建变量来存储每个主题的总和,更改了第二个循环:
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>';
}```
移动的答案不恰当地发布为“编辑到问题”您必须使用它来创建表行。如果您已经有了它,那么请展示您的代码的其余部分以及您得到的结果。您的问题是什么,您的问题是什么?我认为您在这里遇到的问题之一是,您将php耦合在一起,并且查看代码太紧。我建议不要在每个函数中重复原始html,只需将数据传递回模板文件即可。我想我会改变逻辑。。。
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>';
}```