-2

I know the question is already answered but the complexity with me is a bit high. I have a table with 5 columns the rows can be any number. The data retrieved from the database is based on a certain condition and the output is sequential. What I need to do is I have to display 2 rows retrieved from db side by side, as a result now we will have 10 columns in total. I have to do this for all the rows. So if there are 10 rows in db there will be 5 rows on php as every second row is placed side.

Now the thing is the fifth and the tenth column is having unix timestamp. I have to substract the unix time in the fifth column from the unix timestamp in the 10 column and display as a difference in the 11 column on a PHP screen. The code that I wrote displays all the rows sequentially. I am not sure on how to do this. Need help!!// I am not a developer I am into performance Testing. Just have to get this done.. :(.. Please find my PHP code below (Database snapenter image description here) (Expected outputenter image description here) in the expected after the 10'th column i need a difference column. Which calculates the difference between 10th and fifth column. Thanks in advance..

<?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();

?>
Pankaj Harde
  • 55
  • 1
  • 11
  • 2
    without providing enough details it's tough to replicate the problem. Please add all the necessary details. Necessary details include : `your related tables, sample data, expected output and what have you tried so far`. Thank you. – 1000111 Apr 07 '16 at 04:19
  • Added the database snapshot – Pankaj Harde Apr 07 '16 at 04:29
  • Added expected output with comment. Cannot paste current output screen as there is a limit to attachments – Pankaj Harde Apr 07 '16 at 04:38
  • I am able to get all the values sequentially but unable to get two rows in one and and do the substration of time (epochtime) as in snapshot – Pankaj Harde Apr 07 '16 at 04:53
  • The thing is we don't want to increase a column in DB to store the difference just want to do it using php and show on the page. – Pankaj Harde Apr 07 '16 at 05:04
  • Please read [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). These functions have been deprecated for years and are **no longer a part of PHP** and should therefor really not be used anymore. – Oldskool Apr 07 '16 at 05:05

1 Answers1

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>
Pankaj Harde
  • 55
  • 1
  • 11
v7d8dpo4
  • 1,399
  • 8
  • 9