如何在php中并排显示2行并显示新字段中的时间差异

时间:2016-04-07 04:17:02

标签: php mysql

我知道这个问题已经得到解答,但我的复杂程度有点高。我有一个包含5列的表,行可以是任意数字。从数据库检索的数据基于特定条件,输出是顺序的。我需要做的是我必须显示从数据库并排检索的2行,因此现在我们将总共有10列。我必须为所有行执行此操作。因此,如果db中有10行,那么php上将有5行,因为每一行都放在一边。

现在问题是第五列,第十列是unix时间戳。我必须从10列中的unix时间戳中减去第五列中的unix时间,并在PHP屏幕上显示11列中的差异。我编写的代码按顺序显示所有行。我不知道如何做到这一点。需要帮助!! //我不是开发人员我正在进行性能测试。只需要完成这个.. :( ..请在下面找到我的PHP代码  (数据库快照enter image description here)(预期输出enter image description here)在第10列之后的预期中我需要一个差异列。它计算第10和第5列之间的差异。提前谢谢..

<?php

include '_runQuery.php';


$host="10.139.157.84";
$username="testadmin";
$password="summer12";
$db_name="pvteamdb";

$printLimit = 100000;


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form

$application =$_POST['application'];
$lifecycle   =$_POST['lifecycle'];
$useability  =$_POST['useability'];


// 'application' needs to have a value ...

if ( $application == "" ) {
    echo "<br><br> *ERROR:  you must enter an application id !! ** " ;
    echo "<br><br><a href='print_selected_policies.php'>Back</a>";
    return;
}



$sqlfirstbit = "SELECT application,identifier,lifecycle,useability,epochtime FROM policies WHERE application = '$application'";
$orderByStmt = " order by identifier asc";

if ( $lifecycle == "" && $useability == ""  ){
    $sql = $sqlfirstbit . $orderByStmt;
} else if ( $useability == "" ){
    $sql = $sqlfirstbit . " AND lifecycle = '$lifecycle' " . $orderByStmt;
} else if ( $lifecycle == "" ){
    $sql = $sqlfirstbit . " AND useability = '$useability' " . $orderByStmt;
} else {
    $sql = $sqlfirstbit . "  AND lifecycle = '$lifecycle' AND useability = '$useability'  " . $orderByStmt;
}

$result=runQuery($sql);

$numRows=mysql_num_rows($result);
echo "<br><br> numRows = ". $numRows;


if ($numRows > $printLimit ){
    echo "<br><br> *ERROR:  more than $printLimit rows have been returned from query ($numRows rows returned)** " ;
    echo "<br><br><a href='print_selected_policies.php'>Back</a>";
    return;
}


if ($numRows == 0){
    echo "<br><br> -- No rows match filter criteria -- ";

} else {
    echo "<table border='1'>";

    echo "<tr><td>application</td><td>identifier</td><td>lifecycle</td><td>useability</td><td>epochtime</td></td>";
    echo "</tr>";

       // $rows=mysql_fetch_array($result);  

    while ($row = mysql_fetch_assoc($result)){
      $application= $row['application'];
      $identifier = $row['identifier'];
      $lifecycle  = $row['lifecycle'];
      $useability  = $row['useability'];
          //$otherdata  = htmlspecialchars($row['otherdata']);
          //$otherdata  =$row['otherdata'];
          //$lifecycle  = $row['lifecycle'];
      //$created  = $row['created'];
      //$updated  = $row['updated'];
          $epochtime  = $row['epochtime'];

      echo "<tr>";
      echo "<td>$application</td>";
      echo "<td>$identifier</td>";
      echo "<td>$lifecycle</td>";
      echo "<td>$useability</td>";
      //echo "<td>$otherdata</td>";
      //echo "<td>$created</td>";
      //echo "<td>$updated</td>";
          echo "<td>$epochtime</td>";
      echo "</tr>";
    } 
     echo "</table>";
    mysql_free_result($result);

}`enter code here`

echo "<br><br><a href='message_analyzer.php'>Back</a>";
mysql_close();

?>

1 个答案:

答案 0 :(得分:0)

<html>
    <head>
        <title>EMBS_AON</title>
        <link rel="stylesheet" href="style.css" media="all">
    </head>

    <body>
        <div id="container">


            <?php

$host="10.139.157.84";
$username="testadmin";
$password="summer12";
$db_name="pvteamdb";

$printLimit = 100000;

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$application =$_POST['application'];
$lifecycle   =$_POST['lifecycle'];
$useability  =$_POST['useability'];

                include '_runQuery.php';
                $query = "SELECT application,identifier,lifecycle,useability,created,updated,epochtime FROM policies WHERE application = '$application' AND useability = '$useability' ";
                                $orderByStmt = " order by epochtime asc";
                $run = mysql_query($query);

                echo "<table border=1>
                    <tr>
                        <td>appliaction</td>
                        <td>identifier</td>
                        <td>lifecycle</td>
                        <td>useability</td>
                        <td>epochtime</td>
                        <td>appliaction</td>
                        <td>identifier</td>
                        <td>lifecycle</td>
                        <td>useability</td>
                        <td>epochtime</td>
                        <td>Diff epochtime</td>

                    </tr>

                ";
                $rowCount = 1;
                while($row=mysql_fetch_array($run)){
                    $application = $row['application'];
                    $identifier = $row['identifier'];
                    $lifecycle = $row['lifecycle'];
                    $useability = $row['useability'];
                    $epochtime = $row['epochtime'];
                ?>

                    <?php 
                    if($rowCount%2==1) {
                        echo "<tr>";
                        $firstEpochTime = $epochtime;
                    }
                    if(strpos($lifecycle, 'Request') !== false) {
                        $applicationRequest = $application;
                        $idRequest = $identifier;
                        $lifecycleRequest = $lifecycle;
                        $useabilityRequest = $useability;
                        $epochtimeRequest = $epochtime;
                    } 
                    if(strpos($lifecycle, 'Reply') !== false) {
                        $applicationReply = $application;
                        $idReply = $identifier;
                        $lifecycleReply = $lifecycle;
                        $useabilityReply = $useability;
                        $epochtimeReply = $epochtime;
                    }
                    ?>

                    <?php 
                    if($rowCount%2==0) {
                        $lastEpochTime = $epochtime;?>

                        <td><?php echo $applicationRequest;?></td>
                        <td><?php echo $idRequest;?></td>
                        <td><?php echo $lifecycleRequest;?></td>
                        <td><?php echo $useabilityRequest;?></td>
                        <td><?php echo $epochtimeRequest;?></td>

                        <td><?php echo $applicationReply;?></td>
                        <td><?php echo $idReply;?></td>
                        <td><?php echo $lifecycleReply;?></td>
                        <td><?php echo $useabilityReply;?></td>
                        <td><?php echo $epochtimeReply;?></td>

                        <td><?php echo $epochtimeReply-$epochtimeRequest;?></td>
                    <?php   echo "</tr>";
                    }
                    $rowCount = $rowCount + 1;
                    ?>



        <?php }
        echo "</table>";
        ?>

        </div>





    </body>
</html>