foreach中的“return”只返回一组结果,但echo返回all

时间:2015-09-14 02:57:36

标签: loops return

我不知道为什么它会返回我正在寻找的所有值的回声,但只有1回复设置。

我删除了第一个foreach语句中的所有代码,以查看foreach语句之间是否存在某些冲突,但它返回的结果相同。

我在这里尝试了一些我认为可能相关的建议,但没有任何运气。

我可能做错了什么?

function getInvoiceTimeLog($project_unique_id, $user_id, $user_timezone, $time_format)
{

    global $db;

    $query = "SELECT t.task_id, t.unique_id, t.name, t.description, t.sub_total, p.project_id, p.user_id FROM task as t, task_project as tp, project as p WHERE t.task_id = tp.task_id AND tp.project_id = p.project_id AND p.unique_id = ".$db->prep($project_unique_id)." AND p.user_id = ".$db->prep($user_id)." ORDER BY t.name"; 
    $res = $db->query($query,'assoc');

    if($res != false) 
    {
        foreach($res as $row1): 

        $date = '';
        $prevDate = '';
        $return = '';

        $query = "SELECT a.track_id FROM task_track a INNER JOIN track_time b ON a.track_id = b.track_id WHERE a.task_id =".$db->prep($row1['task_id'])." ORDER BY FROM_UNIXTIME(b.time_start,'%Y-%m-%d %H:%i:%s') DESC";
        $resT = $db->query($query,'assoc');

        $return .= '<div class="table" id="InvoicetimeLog"><div class="thead"><div class="th date">Date</div><div class="th start">Start</div><div class="th stop">Stop</div><div class="th hours">Hours</div></div><ul class="list">';

        $return .= '<li>'.$row1['description'].'</li>';

        if($resT != false)
        {
            foreach($resT as $row): 

                $track_id = $row['track_id'];   
                $query = "SELECT comment_id FROM track_comment WHERE track_id = ".$db->prep($track_id);
                $resComLink = $db->query($query,'assoc');
                if($resComLink != false)
                {
                    $query = "SELECT comment,time_left FROM comment WHERE comment_id = ".$db->prep($resComLink[0]['comment_id']);
                    $resComment = $db->query($query,'assoc');
                    $comment_class = 'comment_has';
                }
                else
                {
                    $comment = '';
                    $comment_class = 'comment';
                }

                $query = "SELECT time_start,time_end FROM track_time WHERE track_id = ".$db->prep($track_id);
                $resTime = $db->query($query,'assoc');
                $date = format_time($resTime[0]['time_start'],'m/d/y');

                if($date != $prevDate)
                {
                    $prevDate = format_time($resTime[0]['time_start'],'m/d/y');
                    $timeLog_ex = '<div class="date bold">'.$prevDate.'</div>';
                    $group = 'grp_shw';
                }
                else
                {
                    $timeLog_ex = '<div class="date bold"></div>';
                    $group = 'grp_no';
                }

                if($time_format == 'g')
                {
                    $the_time_format = 'g:i a';
                } 
                else 
                {               
                    $the_time_format = 'G:i';
                }       

                if(empty($resTime[0]['time_end']))
                {
                    $return .= '<li class="'.$group.'" id="rtrack_'.$track_id.'">'.$timeLog_ex.'<div class="start inlineEdit">'.date('m/d/y - '.$the_time_format,$resTime[0]['time_start']).'</div><div class="stop" title="The current time is at '.date($the_time_format,$db->nowUnix($user_timezone)).'">Current</div><div class="hours">'.getTimeDifference($resTime[0]['time_start'],$db->nowUnix($user_timezone)).'</div></li>';
                }
                else
                {
                    $return .= '<li class="'.$group.'" id="rtrack_'.$track_id.'">'.$timeLog_ex.'<div class="start inlineEdit">'.date('m/d/y - '.$the_time_format,$resTime[0]['time_start']).'</div><div class="stop">'.date('m/d/y - '.$the_time_format,$resTime[0]['time_end']).'</div><div class="hours">'.getTimeDifference($resTime[0]['time_start'],$resTime[0]['time_end']).'</div></li>';
                }
            endforeach;

            $return .= '<li class="grp_no"><div class="date bold">Total Time</div><div class="total">'.tot_time($unique_id,$user_id, $user_timezone).'</div></li>';
        }   

        $return .= '</ul></div>';

        endforeach;
    }
    return $return;
} 

1 个答案:

答案 0 :(得分:0)

我是一个多么麻烦的人。这很简单....只需移动foreach循环的变量声明OUTSIDE!

将这些人移到第一个foreach之上:

$date = '';
$prevDate = '';
$return = '';