【问题标题】:How to use a variable outside of the loop it came from? PHP如何在它来自的循环之外使用变量? PHP
【发布时间】: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-&gt;result() 没有返回任何内容,您的脚本甚至没有输入foreach。尝试先调试这个。 (是的,你应该总是事先初始化你的变量。)
  • 变量 $hours 确实输出正确。所以查询返回结果
  • 在不相关的注释中,在循环期间运行 SQL 查询是个坏主意。请考虑将此计算作为您的 $team_query 的一部分。
  • @SergeyVidusov 我会记住这一点
  • 可能是一个愚蠢的问题,但是:您确定在foreach 之后输出$total 吗?如果是:您确定在 foreach 之后和输出 $total 之前什么都没有发生吗? “圈外”听起来有点含糊。很高兴看到循环和 $total 输出之间的完整代码。

标签: php sql codeigniter foreach


【解决方案1】:

算术加法(+ 运算符)不适用于像“01:00:00”这样的字符串。您必须在添加之前将它们转换为秒数(通过TIME_TO_SEC() MySQL 函数),或者通过 SQL 计算整个值,如建议的here

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

【讨论】:

    【解决方案2】:

    解决办法

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

    【讨论】:

    • 我从数据库中获取的值是时间格式的。这可能就是他们没有正确添加的原因。有没有办法毫无问题地添加它们?
    【解决方案3】:

    可能是您的查询结果返回零行数。确保 $team_query 有结果。

    结果()作为$team_row) { $total += $小时; } ?>

    【讨论】:

    • 它确实返回行。变量 $hours 正确输出
    • 首先你必须为 $total 创建初始值。所以在循环之外你应该声明 $total=0;这是循环的开始。
    • 我添加了更多代码。我很肯定查询返回结果。是否与时间格式有关?
    • 试试这个 $this->db->select('SEC_TO_TIME( SUM( TIME_TO_SEC( USER_WORK_HOURS )/3600 ) ) AS totalHours'); $this->db->where('USER_EMAIL', $team_row->USER_EMAIL); $hours= $this->db->get('user_hours')->row()->totalHours;
    猜你喜欢
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2015-05-02
    相关资源
    最近更新 更多