Hi guys just need a tiny help , i have rows of data echoed out through a while loop from DBase. There are 2 variables named $closedb and $profitandloss. They are being defined in the while loop according to the presiding conditions in the loop . I used a function to transfer values from refreshable.php and moved it to delete.php. The code just keep retrieving the last row to insert into database. However the profit and loss and close(price) in the row dont match when inserted into db. The problem is it keeps getting the last row of value( $closedb and $profitable) in the while loop and inserting into DB. How do i fix this so the variable retrieves from the right row? Answers all welcome. questions for clarification welcome . really need your help guys . Thanks.

refreshable.php

$query = "SELECT * FROM opentrades where user_id =". $userid; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
echo "<tr><td>" . "Order Number" . "</td><td>" . "Selection" . "</td><td>" . "Date" . "</td><td>" . "Type" . "</td><td>" . "Size" . "</td><td>" . "Bid Price" . "</td><td>" . "Offer Price" . "</td><td>" ."Stop Loss" . "</td><td>" . "Take Profit" . "</td><td>" . "Current Price" . "</td><td>" ."Profit/Loss(USD)"."</td><td>" ."Close"."</td></tr>" ;  //$row['index'] the index here is a field name

while($row = mysql_fetch_assoc($result)){   //Creates a loop to loop through results
if ($row['selection']=='eur/usd')// TO RETRIEVE BID AND OFFER FOR EACH ROW
      {
      $bidpricepl=$bid;

      $offerpricepl=$bid1;

      }
  elseif ($row['selection']=='usd/jpy')
      {
      $bidpricepl=$bid2;

      $offerpricepl=$bid3;

      }
  elseif ($row['selection']=='usd/cad')
      {
      $bidpricepl=$bid4;

      $offerpricepl=$bid5;

      }
  elseif ($row['selection']=='eur/jpy')
      {
      $bidpricepl=$bid6;

      $offerpricepl=$bid7;

      }
  elseif ($row['selection']=='eur/chf')
      {
      $bidpricepl=$bid8;

      $offerpricepl=$bid9;

      }
  elseif ($row['selection']=='gbp/usd')
      {
      $bidpricepl=$bid10;

      $offerpricepl=$bid11;

      }
  elseif ($row['selection']=='aud/usd')
      {
      $bidpricepl=$bid12;

      $offerpricepl=$bid13;

      }
  elseif ($row['selection']=='usd/chf')
      {
      $bidpricepl=$bid14;

      $offerpricepl=$bid15;

      }
  if ($row['type']=="buy")
    {
      //$last3charsoffer = substr($row['offerprice'], -6);
      //$offernodecimal = str_replace('.', '', $last3charsoffer);
      //$last3charsoffer1 = substr($offerpricepl, -6);
      //$offernodecimal1 = str_replace('.', '', $last3charsoffer1);

      //$pips2 = ltrim($pips2, '0');
      //$calcpips2=$calcpips/$minipipskiller;
      //$last3charsoffer = substr($row['offerprice'], -6);
      //$offernodecimal = str_replace('.', '', $last3charsoffer);
      //$last3charsoffer1 = substr($offerpricepl, -6);
      //$offernodecimal1 = str_replace('.', '', $last3charsoffer1);
      //$minipipskiller='10';
      //$offeropen=$row['offerprice'];// to define variable
      //$pips=$offerpricepl-$offeropen;// to calculate difference STEP 1
      //$calcpips = str_replace('.', '', $pips); //removing the deci
      //$calcpips = ltrim($calcpips, '0');// remove zeros in front
      //$calcpips2=$calcpips/$minipipskiller;// to divide by 10 to cut mini pips  
      $minipipskiller='10';
      $offeropen=$row['offerprice'];
      $pips=$offerpricepl-$offeropen;
      $closedb=$offerpricepl;
      $pips1=round($pips, 6);
      $pips2 = str_replace('.', '', $pips1);
        if ($pips2<0)
      {
        $pips2 = str_replace('-', '', $pips2);
        $pips2 = ltrim($pips2, '0');
        $pips2 = -1 * abs($pips2);
      }
      else {
        $pips2 = ltrim($pips2, '0');
      }
      $pips3=$pips2/$minipipskiller;
    }// PIP COUNTING
  elseif ($row['type']=="sell")//FOR PIP COUNTING
    {
      //$last3charsbid = substr($row['bidprice'], -6);
      //$bidnodecimal = str_replace('.', '', $last3charsbid);
      //$last3charsbid1 = substr($bidpricepl, -6);
      //$bidnodecimal1 = str_replace('.', '', $last3charsbid1);
      $minipipskiller='10';
      $bidopen=$row['bidprice'];
      $pips=$bidopen-$bidpricepl;
      $closedb=$bidpricepl;
      $pips1=round($pips, 6);
      $pips2 = str_replace('.', '', $pips1);
      if ($pips2<0)
      {
        $pips2 = str_replace('-', '', $pips2);
        $pips2 = ltrim($pips2, '0');
        $pips2 = -1 * abs($pips2);
      }
      else {
        $pips2 = ltrim($pips2, '0');
      }
      $pips3=$pips2/$minipipskiller;
    }
    //echo $pips3;


      $ticksize= "0.0001";// FOR PROFIT AND LOSS
      $lot1 = "100000";
      $sizecalc=$row['size'] * $lot1;

        if ($row['type']=="buy")
      { 
        $profitandloss=$sizecalc*$ticksize*$pips3; //per TRADE
      }
      if ($row['type']=="sell")
      {
        $profitandloss=$sizecalc*$ticksize*$pips3; //per TRADE
      }
      //echo $lot1;
      //echo $ticksize;
      number_format($profitandloss, 2);
      $zero= '0';
      //if($profitandloss<$zero){
        //    echo "<div style=\"color: red;\">$profitandloss</div>";
      //}
      //elseif ($profitandloss>$zero){
          //  echo "<div style=\"color: green;\">$profitandloss</div>";
        //}

      if($profitandloss<$zero) {
            $profitText = "<div style=\"color: red;\">$profitandloss</div>";
        } elseif ($profitandloss>$zero) {
            $profitText = "<div style=\"color: green;\">$profitandloss</div>";
        }
        // for profit and loss counting

        $profitText=ltrim($profitText,'0');
$profit = round(floatval(trim($profitText)), 2);
$profit=$profitText;

       // $profit = intval(floatval($profitText)*100)/100.0;
    //round($profitText,2); 

$sum+= $profitandloss;
       //
    $row['type'] = strtoupper($row['type']);//CAPS
    $row['selection'] = strtoupper($row['selection']);//CAPS

    echo "<tr><td>" . $row['trade_id'] .         
        "</td><td>" . $row['selection'] . 
        "</td><td>" . $row['date'] .
        "</td><td>" . $row['type'] .
        "</td><td>" . $row['size'] .
        "</td><td>" . $row['bidprice'] .
        "</td><td>" . $row['offerprice'] .
        "</td><td>" . $row['stoploss'] .
        "</td><td>" . $row['takeprofit'] .
        "</td><td>" . $closedb . 
        "</td><td>" . $profit . 
        "</td><td><a href ='delete.php?id=".
        $row['trade_id']."'>X</a>
       </td></tr>";  


if($sum<$zero) {
    $sumText = "<div style=\"color: red;\">$sum</div>";
} elseif ($sum>$zero) {
    $sumText = "<div style=\"color: green;\">$sum</div>";
}
}
function callvariable($closedb,$profitandloss)
{
  $variable=array('$closedb','$profitandloss');

return $variable;
} 
echo "</table><br>";

delete.php

<?php 
//If(!isset($trade_id)){
require_once('connect.php');
        require_once 'refreshable.php';
         require_once "../core/init.php"; 

            require_once 'query.php';

        $trade_id= $_GET['id'];
           date_default_timezone_set("Asia/Singapore");
        $date = date('m/d/Y h:i:s a', time());
        $timezone = date_default_timezone_get();

       $userid=$user_data['user_id'];
        $var=callvariable($closedb,$profitandloss);




$mysqli = new mysqli($database_hostname, $database_username, $database_password, $database_name) or exit("Error connecting to database");
try {
    // First of all, let's begin a transaction
    $mysqli->autocommit(FALSE);

    // A set of queries; if one fails, an exception should be thrown

    $mysqli-> query("UPDATE `trade_history1` SET `dateclose` = CURRENT_TIMESTAMP,
                                             `close` = '{$closedb}',
                                             `profitandloss` = '{$profitandloss}'
                                            WHERE `trade_id`= ".$trade_id



    // If we arrive here, it means that no exception was thrown
    // i.e. no query has failed, and we can commit the transaction
    $mysqli->commit();
    $_SESSION['message'] = 'Successfully deleted';
} catch (Exception $e) {
    // An exception has been thrown
    // We must rollback the transaction
    $_SESSION['message'] = 'Unable to delete';
    $mysqli->rollback();

}


        // if we successfully delete this, we
$mysqli->close();
 //require_once 'query2.php';
// bid price,offer price, size,type,
header('location:test.php');
?>

Recommended Answers

All 2 Replies

important parts are

 echo "<tr><td>" . $row['trade_id'] .         
        "</td><td>" . $row['selection'] . 
        "</td><td>" . $row['date'] .
        "</td><td>" . $row['type'] .
        "</td><td>" . $row['size'] .
        "</td><td>" . $row['bidprice'] .
        "</td><td>" . $row['offerprice'] .
        "</td><td>" . $row['stoploss'] .
        "</td><td>" . $row['takeprofit'] .
        "</td><td>" . $closedb . 
        "</td><td>" . $profit . 
        "</td><td><a href ='delete.php?id=".
        $row['trade_id']."'>X</a>
       </td></tr>";  


if($sum<$zero) {
    $sumText = "<div style=\"color: red;\">$sum</div>";
} elseif ($sum>$zero) {
    $sumText = "<div style=\"color: green;\">$sum</div>";
}
}
function callvariable($closedb,$profitandloss)
{
  $variable=array('$closedb','$profitandloss');

return $variable;
} 
echo "</table><br>";

and

        $trade_id= $_GET['id'];
           date_default_timezone_set("Asia/Singapore");
        $date = date('m/d/Y h:i:s a', time());
        $timezone = date_default_timezone_get();

       $userid=$user_data['user_id'];
        $var=callvariable($closedb,$profitandloss);

            $mysqli-> query("UPDATE `trade_history1` SET `dateclose` = CURRENT_TIMESTAMP,
                                             `close` = '{$closedb}',
                                             `profitandloss` = '{$profitandloss}'
                                            WHERE `trade_id`= ".$trade_id);
Member Avatar for diafol

What's the point of callvariable?

BTW, I'm not sure about the delete procedure. When SELECTING from a db using a form, by all means use a GET / url parameters, but if you're making changes to the db with DELETE or UPDATE, then you should use POST.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.