Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用来自循环之外的变量?PHP_Php_Sql_Codeigniter_Foreach - Fatal编程技术网

如何使用来自循环之外的变量?PHP

如何使用来自循环之外的变量?PHP,php,sql,codeigniter,foreach,Php,Sql,Codeigniter,Foreach,自从我发布后,我已经更新了我的代码。 我试图在循环外回显一个变量,该变量是在循环中计算的 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->

自从我发布后,我已经更新了我的代码。

我试图在循环外回显一个变量,该变量是在循环中计算的

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>';

}    
foreach($query->result()作为$row){//循环以显示所有团队成员
回声“用户小时”;
$this->db->select('users.USER_EMAIL');//我们希望显示此团队中用户的电子邮件。
$this->db->from('users');
$this->db->join('user\u teams','users.user\u ID=user\u teams.user\u ID');//我们需要加入这些选项卡才能得到这个
$this->db->join('teams'、'teams.TEAM_ID=user_teams.TEAM_ID');
$this->db->where('teams.TEAM_NAME',$row->TEAM_NAME);//搜索我们当前正在查看的团队名称
$team_query=$this->db->get();
echo“团队:”;
echo$行->团队名称;
回声';
$total=日期('h:i:s',空);
var_dump($total);//以字符串形式输出“01:00:00”
foreach($team\u query->result()作为$team\u行){
var_dump($total);//以字符串形式输出“01:00:00”
回声';
回声';
echo$team\u row->USER\u EMAIL;
回声';
$this->db->select('SEC_TO_TIME(总和(TIME_TO_SEC('USER_WORK_HOURS'))作为totalHours));
$this->db->where('USER\u EMAIL',$team\u row->USER\u EMAIL);
$hours=$this->db->get('user_hours')->row()->totalHours;
回声';
echo$hours;//以字符串形式输出“00:11:01”,这是正确的值
回声';
var_dump($hours);//以字符串形式输出“00:11:01”。仍然正确
$total+=$hours;//我猜+=操作员没有按时工作
var_dump($total);//将1输出为int,不正确。在循环中第一次应将“00:11:01”作为字符串
回声';
回声“
”; } 回声'; 回声'; echo'Total';//输出1作为int 回声'; 回声'; echo$total;//以int形式输出1 回声'; 回声'; 回声'; }
它是否与时间格式有关?

如果有的话,增加时间的正确方法是什么

我正在使用codeigniter 3x,如果这有什么关系的话

如果需要任何其他信息,请告诉我


提前感谢。

可能是您的查询结果返回的行数为零。确保$team\u查询有结果

结果()为$team_行) { $total+=$hours; }这是解决方案

       $hours= $this->db->get('user_hours')->row()->totalHours;

        make sure that $hours variable contains an integer value 
        or float value 
        it should not a be string and also not be empty.


You can initialize  $total = 0;  $hours=0;
算术加法(
+
运算符)对“01:00:00”这样的字符串不起作用。您必须在添加之前将它们转换为秒(通过
TIME\u to_SEC()
MySQL函数),或者根据建议通过SQL计算整个值


另外,在循环之前查看SQL(通过$query检索的SQL)也会很有趣:我很确定您可以一次性获得所需的所有数据,而无需嵌套查询和PHP计算。

它确实返回行。变量$hours正确输出首先必须为$total创建初始值。因此,在循环之外,您应该声明$total=0;这是循环的开始。我添加了更多的代码。我肯定查询会返回结果。它可能与时间格式有关吗?试试这个$this->db->select('SEC_to_time(总和(time_to_SEC(
USER_WORK_HOURS
)/3600))作为totalHours)$这->数据库->其中('USER\u EMAIL',$team\u row->USER\u EMAIL)$小时数=$this->db->get('user_hours')->row()->totalHours;显然,您的
$team\u query->result()
没有返回任何内容,您的脚本甚至没有输入
foreach
。试着先调试这个。(是的,您应该始终事先初始化变量。)变量$hours的输出正确。因此,查询返回的是一个不相关的结果,在循环期间运行SQL查询是个坏主意。考虑把这个计算作为你的$Teang-Quy查询的一部分。@ SergyyViuSov我将把它记在脑子里可能是个愚蠢的问题,但是:你确信你在代码< > Frach < /代码>之后总共输出了$$吗?如果是:您确定在
foreach
之后和输出$total之前没有发生任何事情吗?“圈外”听起来有点模糊。很高兴看到循环和$total输出之间的完整代码。我从数据库中获取的值是时间格式的。这可能就是他们没有正确添加的原因。有没有办法添加它们而不产生任何问题?