2016-07-30 14 views
0

したがって、ユーザーの合計ログイン時間は次のようになります。今度は、time_inとtime_outの両方の日時フィールドの合計数を取得し、合計時間と分を表示したいと考えています。ここで合計行時間と分を取得する

$query = " 
    SELECT member_id 
     , member_name 
     , team 
     , time_in 
     , time_out 
     , SEC_TO_TIME(UNIX_TIMESTAMP(time_out) - UNIX_TIMESTAMP(time_in)) totalhours 
    FROM hours 
    WHERE member_id = 7; 
    "; 

$result = mysql_query($query)or die(mysql_error()); 
$rowNo = 1; //Increment Row Number 
while($row = mysql_fetch_assoc($result)){ 
$time = $row['totalhours']; 

echo "<tr align='left'>";  
echo"<td><font color='white'>" .$rowNo++."</font>.</td>"; 
echo"<td><font color='white'>" .$row['member_name']."</font>.</td>"; 
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_in']))."</font>.</td>"; 
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_out']))."</font>.</td>"; 
echo"<td><font color='white'>" .$time." Hrs</font>.</td>"; 
echo "</tr>";   
} 

はあなたの助けがはるかに高く評価された画像出力

Output of current query

です。

+2

に役立ちます願っています。 'mysqli_ *'や 'PDO 'を使う – Jens

+0

これはちょうど私がpdoを使うオフラインプロジェクトです。 – lalthung

+0

'$行= mysql_num_rows($結果);' ?? – RamRaider

答えて

1

は、このコードを使用して、私は非推奨 `mysql_ *` APIを使用して**停止**これは

$query = "select member_id, member_name, team, time_in, time_out, sec_to_time(unix_timestamp(time_out) - unix_timestamp(time_in)) AS totalhours from hours WHERE member_id ='7'"; 

$result = mysql_query($query)or die(mysql_error()); 
$rowNo = 1; //Increment Row Number 
$total_time="00:00:00"; 
while($row = mysql_fetch_assoc($result)){ 
$time = $row['totalhours']; 

$secs = strtotime($time)-strtotime("00:00:00"); 
$total_time = date("H:i:s",strtotime($total_time)+$secs); 


echo "<tr align='left'>";  
echo"<td><font color='white'>" .$rowNo++."</font>.</td>"; 
echo"<td><font color='white'>" .$row['member_name']."</font>.</td>"; 
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_in']))."</font>.</td>"; 
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_out']))."</font>.</td>"; 
echo"<td><font color='white'>" .$time." Hrs</font>.</td>"; 
echo "</tr>";   
} 

echo $total_time; 
+0

はい、私はそれが間違いなく助けていると話していました。おかげで多くの仲間 – lalthung

+0

歓迎:)これはこのサイトのためのものです。 –

-1

まず、mysql_ *をmysqli_ *またはpdoに変更する必要があります。

count()はこれにあなたの友人です。

あなたの質問はこのようにする必要があります。

select count(*) as total,member_id, member_name, team, time_in, time_out, sec_to_time(unix_timestamp(time_out) - unix_timestamp(time_in)) AS totalhours from hours WHERE member_id ='7' 

$row['total']をエコーすると、レコードの合計数が表示されます。

UPDATE

そのままクエリを残し、および行の合計数を節約するために、変数TOTAL_ROWS $を作ります。

$ total_rows = mysql_num_rows($ result);

echo "<tr align='left'>";  
echo"<td><font color='white'>" .$rowNo++."</font>.</td>"; 
echo"<td><font color='white'>" .$total_rows."</font>.</td>"; 
echo"<td><font color='white'>" .$row['member_name']."</font>.</td>"; 
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_in']))."</font>.</td>"; 
echo"<td><font color='white'>" .date('Y-M-d - h:i:s a ', strtotime($row['time_out']))."</font>.</td>"; 
echo"<td><font color='white'>" .$time." Hrs</font>.</td>"; 
echo "</tr>"; 
+1

SQLインジェクションはmysql_ *またはmysqli_ *と全く関係ありません – Jens

+0

これはみんなプロジェクトですpdoを使用してください:) – lalthung

+0

@ラファエルは、そのクエリでは起こっていません私の1つのrocordはscreemから消えて、合計2時間を計っていますtime_inとtime_outから合計時間と分 – lalthung

0

行数がするmysql_query関数によって返さ戻りますPHPのはmysql_num_rows($結果)関数があります。

+0

しかし、それはtime_inとtime_out日時フィールドから時間と分の合計数を計算しません – lalthung

関連する問題