2014-02-12 2 views
0

다음 코드를 사용하여 데이터베이스의 데이터에서 테이블을 생성하고 있습니다. 직원별로 데이터를 그룹화합니다. 필자가 필요로하는 것은 각 직원 테이블 끝에 마지막 네 열의 합계를 표시하는 것입니다. The Hours, OT, Travel 및 TOT 열.MYSQL PHP 쿼리에서 표에 합계를 추가하십시오.

코드 :

$current_user_name = false; 
while($row = mysql_fetch_array($result)) { 
    // listing a new employee? Output the heading, start the table 
    if ($row['user_name'] != $current_user_name) { 
      if ($current_user_name !== false) 
        echo '</table>'; echo '[divider_padding]';// if we're changing employee, close the table 
     echo ' 
      <h5>'.$row['last_name'].', '.$row['first_name'].'</h5>[minimal_table] 
       <table> 
       <tr> 
       <th>Date</th> 
       <th class="tableleft">Description</th> 
       <th class="tableleft">Job</th> 
       <th class="tableleft">Activity</th> 
       <th class="tableleft">Comments</th> 
       <th class="tableright">Hours</th> 
       <th class="tableright">OT</th> 
       <th class="tableright">Travel</th> 
       <th class="tableright">TOT</th> 

       </tr>'; 

      $current_user_name = $row['user_name']; 
    } 
    // output the row of data 
    echo '<tr> 
     <td style="width:75px">'.$row['labor_date'].'</td> 
     <td class="tableleft">'.$row['description'].'</td> 
     <td class="tableleft">'.strtoupper($row['job']).'</td> 
     <td class="tableleft">'.$row['activity'].'</td> 
     <td class="tableleft">'.$row['comments'].'</td> 
     <td class="tableright">'.$row['rthours'].'</td> 
     <td class="tableright">'.$row['othours'].'</td> 
     <td class="tableright">'.$row['trthours'].'</td> 
     <td class="tableright">'.$row['tothours'].'</td> 
     </tr> 
    ';} 

echo '</table>[/minimal_table]'; // close the final table 

} 
?> 

나는 몇 가지 테스트를 시도한 후 끼 었어이 알아낼 수 없습니다. 어떤 도움을 주시면 감사하겠습니다

$result = mysql_query("SELECT * FROM data WHERE labor_date BETWEEN '$start' AND '$end' Order by last_name, labor_date ASC"); 

:

데이터를 수집하는 쿼리입니다.

+1

... '총 $ rthours + = $의 다음 행 ['rthours를 ']', 출력 어떤 특정 직원과 일을 마치면 다른 곳에서. –

+0

예, 작동합니다. 어디에 넣을 지 알 수 없습니다. 코드를 배치 할 때마다 각 테이블의 맨 아래에 있지 않습니다. 나는 데이터 섹션의 출력의 닫는 테이블 행 후에 시도했다. 코드 상단에있는 닫기 괄호 앞에 시도했습니다. – Lawrence

답변

0

이 줄 개방 브래킷 누락 :

if ($current_user_name !== false) 

은 다음과 같아야하므로

if ($current_user_name !== false) { 
+0

그건 코드를 깰. 없을 때 코드가 작동합니다. 이 코드는 바로 위의 else 문으로 묶여 있기 때문에 괄호가 필요하다는 것을 알 수 있습니다. 맨 아래 브래킷은 else 코드를 래핑합니다. – Lawrence

+0

다음과 같이 변경했습니다 :'if ($ current_user_name! == false) { echo ''; echo '[divider_padding]';}' – Lawrence

+0

에코하기 바로 전에 ''; 합계 데이터로 을 에코로 나타내는 행을 추가하십시오. 테이블 태그가 닫혀 있으면 작동합니다. 각각의 신입 사원에 대해 새 테이블을 열 때 설정을 해제하거나 ($ rthours) 0으로 설정하는 것을 잊지 마십시오. – larsAnders

관련 문제