Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

php - How to Minus 2nd row value from the 1st row in same HTML table

I'm struggling with a PHP Code, and I have no clue how to solve this. I have two columns with timestamps on which I've ordered by desc in my Html table which I'm generating with PHP code.

I need to find out the differences between the 2nd row StateEndTime and the 1st-row StateStarTtime nd so on and display this in a new row/column. The final table should look like this :

ID          Type         StateStarTtime    StateEndTime   Min Difference

xxx         YYY          03:57             03:59          00:02
xxx         ZZZ          03:53             03:55          00:04
xxx         ZZZ          03:46             03:49          

Below is the PHP code in my index file

<?php while($row = mysqli_fetch_array($search_result)):?>
  <tr>
      <td align="Center"><?php echo $row['id'];?></td>
      <td align="Center"><?php echo $row['Username'];?></td>
      <td align="Center"><?php echo $row['StateStarttime'];?></td>
      <td align="Center"><?php echo $row['StateEndtime'];?></td>
      <?php endwhile;?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
    <?php 
    $lasttime = 0;
    while($row = mysqli_fetch_array($search_result)):?>
    <tr>
      <td align="Center"><?php echo $row['id'];?></td>
      <td align="Center"><?php echo $row['Username'];?></td>
      <td align="Center"><?php echo $row['StateStarttime'];?></td>
      <td align="Center"><?php echo $row['StateEndtime'];?></td>
      <td align="Center"><?php echo ($row['StateStarttime'] - $lasttime);?></td>
      <?php 
$lasttime = $row['StateStarttime'];
endwhile;?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...