【发布时间】:2016-05-30 02:15:20
【问题描述】:
我在发布后更新了我的代码。
我正在尝试在计算它的循环之外回显一个变量。
foreach ($query->result() as $row){ //loop to display all team members
echo '<table border="1"><tr><td>User</td><td>Hours</td></tr>';
$this->db->select('users.USER_EMAIL'); //we want to display the users' emails who are in this team.
$this->db->from('users');
$this->db->join('user_teams', 'users.USER_ID = user_teams.USER_ID'); //we need to join these tabels in order to get this
$this->db->join('teams', 'teams.TEAM_ID = user_teams.TEAM_ID');
$this->db->where('teams.TEAM_NAME', $row->TEAM_NAME); //search for the team name that we are currently looking at
$team_query = $this->db->get();
echo '<h2>Team: ';
echo $row->TEAM_NAME;
echo '</h2>';
$total = date('h:i:s', NULL);
var_dump($total); //outputs "01:00:00" as a string
foreach ($team_query->result() as $team_row) {
var_dump($total); //outputs "01:00:00" as a string
echo '<tr>';
echo '<td>';
echo $team_row->USER_EMAIL;
echo '</td>';
$this->db->select('SEC_TO_TIME( SUM( TIME_TO_SEC( `USER_WORK_HOURS` ) ) ) AS totalHours');
$this->db->where('USER_EMAIL', $team_row->USER_EMAIL);
$hours= $this->db->get('user_hours')->row()->totalHours;
echo '<td>';
echo $hours; //outputs "00:11:01" as a string, that is the correct value
echo '</td>';
var_dump($hours); //outputs "00:11:01" as a string. still correct
$total += $hours; //im guessing the += operators do not work on time
var_dump($total); //outputs 1 as int, not correct. should be "00:11:01" as a string for the first time around the loop
echo '</tr>';
echo '<br />';
}
echo '<tr>';
echo '<td>';
echo 'Total'; //outputs 1 as int
echo '</td>';
echo '<td>';
echo $total; //outputs 1 as int
echo '</td>';
echo '</tr>';
echo '</table>';
}
可能与时间格式有关吗?
添加时间的正确方法是什么,如果有的话?
如果这很重要,我正在使用 codeigniter 3x。
如果需要任何其他信息,请告诉我。
提前谢谢你。
【问题讨论】:
-
显然,您的
$team_query->result()没有返回任何内容,您的脚本甚至没有输入foreach。尝试先调试这个。 (是的,你应该总是事先初始化你的变量。) -
变量 $hours 确实输出正确。所以查询返回结果
-
在不相关的注释中,在循环期间运行 SQL 查询是个坏主意。请考虑将此计算作为您的 $team_query 的一部分。
-
@SergeyVidusov 我会记住这一点
-
可能是一个愚蠢的问题,但是:您确定在
foreach之后输出$total 吗?如果是:您确定在foreach之后和输出 $total 之前什么都没有发生吗? “圈外”听起来有点含糊。很高兴看到循环和 $total 输出之间的完整代码。
标签: php sql codeigniter foreach