AUTO UPDATE TRADE

 avatar
Herbie
php
2 years ago
1.5 kB
5
Indexable
Never
<?php
    
    include 'Functions.php';

    $select_trades = mysqli_query($GLOBALS['dbconn'], "SELECT * FROM `exo_trade_tbl` WHERE tr_status = '0'");

    $tr_row = mysqli_fetch_array($select_trades);

    foreach ($tr_row as $trades) {
        
        $endDate = $trades['tr_end_time'];

        $tradeReturns = $trades['tr_returns'];
    
        $currentDate = date('Y-m-d H:i:s');
    
        $client = $trades['tr_client'];
    
        $id = $trades['tr_id'];
        
        if ($currentDate > $endDate) {

            $randBool = (bool) mt_rand(0, 1);

            if ($randBool === 1) {

                $selectUser = mysqli_query($GLOBALS['dbconn'], "SELECT * FROM `exo_clients_tbl` WHERE u_email = '".$client."'");

                $u_row = mysqli_fetch_array($selectUser);

                $old_bal = $u_row['u_balance'];

                $new_bal = $old_bal + $tradeReturns;
                
                $updateBalane = mysqli_query($GLOBALS['dbconn'], "UPDATE `exo_clients_tbl` SET u_balance = '".$new_bal."'");
                
                $updateTrade = mysqli_query($GLOBALS['dbconn'], "UPDATE `exo_trade_tbl` SET tr_status = '1' WHERE tr_client = '".$client."' AND tr_id = '".$id."'");
            } elseif ($randBool === 0) {

                $updateTrade = mysqli_query($GLOBALS['dbconn'], "UPDATE `exo_trade_tbl` SET tr_status = '2' WHERE tr_client = '".$client."' AND tr_id = '".$id."'");
            }
        }
    }