创建2x1矩阵系统

时间:2017-03-20 12:57:32

标签: php mysql database xampp

我创建了2x1矩阵系统,其中一个人捐钱,捐赠后他从两个人那里收钱。我试图创造一种情况,即一个人第一次进入系统(注册后),系统将检查系统中是否有人可以从该人那里接收资金,如果有的话然后,我正在检查该人是否已经有两次付款,如果该人得到2次付款,系统将检查系统中的下一个人,并将再次进行该过程以检查该人是否已收到两次或不。 现在的问题是,系统只检查2个人,就好像系统找到一个人并且该人已经复活两次,支付系统将用于下一个,但如果下一个收到,则系统应该转到下一个,依此类推,除非他找到了用户或没有用户。但我的系统最多只有两个人,我不知道应该如何创建一个循环,以便系统检查数据库中的整个用户。

public function allotDonar($id) {
    $date = date('Y-m-d h:i:s a');
    $currentDate = strtotime($date);
    $futureDate = $currentDate+(60*1);
    $formatDate = date("Y-m-d h:i:s a", $futureDate);
    $endDate = date("Y-m-d h:i:s a", strtotime('+30 hours'));

    $get = $this -> db -> query("Select user.id, user.super, user.name, 
user.canRecieve, user.active, user.blocked, user_packages.packageID, user.type, 
user_packages.userID from (Select user.id AS USERID, canRecieve, type, super, 
status, packageID, active from user JOIN user_packages ON user.id='$id' AND 
user.type='0' AND user.super='0' AND user.canRecieve='0' AND user.active='1' AND 
user_packages.userID=user.id AND user_packages.status='1') AS DONOR JOIN user ON 
user.id!='$id' AND user.canRecieve='1' AND user.active='1' AND user.blocked='0' 
AND user.type='0'  AND user.super='0' JOIN user_packages ON 
user.id=user_packages.userID AND user_packages.status='1' AND 
user_packages.packageID=DONOR.packageID ORDER BY user.id ASC");
    if($get -> num_rows() > 0) {
        $UserInfo = $get -> row();
        $key = $get -> result();
        $sql = "Select * from donar where packageID=? AND reciever_id=?";
        $query = $this -> db -> query($sql, array($UserInfo -> packageID, $UserInfo -> id));
        // return $query -> result();
        if($query -> num_rows() >= 2) {
            // return 'Persons';
            // if user cycle has not been completed but user has tow recievings in the table then again search fot the next user in the array
            $nexPerson = current(array_slice($key, array_search($key, array_keys($key)) + 1, 1));
            if(!empty($nexPerson)) {
                $nextPersonID = $nexPerson -> id;
                // check if next pserons has paid money or not
                $checkIfNextPaid = $this -> db -> query("Select * from user where canRecieve='1'");
                if($checkIfNextPaid -> num_rows() > 0) {
                    $sql = "Select * from donar where packageID=? AND reciever_id=?";
                    $query = $this -> db -> query($sql, array($nexPerson -> packageID, $nexPerson -> id));
                    // return $query -> result();
                    if($query -> num_rows() >= 2) {
                        //$this -> allotDonar($id);
                    }
                    else {

                        $sql = "Select * from donar where packageID=? AND donarID=?";
                        $query = $this -> db -> query($sql, array($UserInfo -> packageID, $id));
                        if($query -> num_rows() >= 1) {
                        }
                        else {
                        // $currentID = $UserInfo -> id;
                            $allotDonar = $this -> db -> insert('donar', array('donarID'    =>  $id, 'reciever_id'  =>  $nexPerson -> id, 'packageID'    =>  $nexPerson -> packageID, 'date_added'    =>  $date, 'date_expiry'    =>  $endDate, 'wait_time' =>  $formatDate, 'wait_start' =>  $date));
                            return $this -> db -> insert_id();
                        }
                    }
                }
            }
        }
        else {
            $sql = "Select * from donar where packageID=? AND donarID=?";
            $query = $this -> db -> query($sql, array($UserInfo -> packageID, $id));
            if($query -> num_rows() >= 1) {
            }
            else {
                // if the rows of the user are less then 2 record then simply insert the erecord of the user                  
                $allotDonar = $this -> db -> insert('donar', array('donarID'    =>  $id, 'reciever_id'  =>  $UserInfo -> id, 'packageID'    =>  $UserInfo -> packageID, 'date_added'    =>  $date, 'date_expiry'    =>  $endDate, 'wait_time' =>  $formatDate, 'wait_start' =>  $date));
                return $this -> db -> insert_id();
            }
        }
    }
    else {
        return 'No person found';
    }

请帮我对数据库中的所有用户进行此系统检查。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

public function allotDonar($id) {
    $date = date('Y-m-d h:i:s a');
    $currentDate = strtotime($date);
    $futureDate = $currentDate+(60*1);
    $formatDate = date("Y-m-d h:i:s a", $futureDate);
    $endDate = date("Y-m-d h:i:s a", strtotime('+30 hours'));

    $get = $this -> db -> query("Select user.id, user.super, user.name, user.canRecieve, user.active, user.blocked, user_packages.packageID, user.type, user_packages.userID from (Select user.id AS USERID, canRecieve, type, super, status, packageID, active from user JOIN user_packages ON user.id='$id' AND user.type='0' AND user.super='0' AND user.canRecieve='0' AND user.active='1' AND user_packages.userID=user.id AND user_packages.status='1') AS DONOR JOIN user ON user.id!='$id' AND user.canRecieve='1' AND user.active='1' AND user.blocked='0' AND user.type='0'  AND user.super='0' JOIN user_packages ON user.id=user_packages.userID AND user_packages.status='1' AND user_packages.packageID=DONOR.packageID ORDER BY user.id ASC");
    if($get -> num_rows() > 0) {
        $UserInfo = $get -> row();
        $key = $get -> result();
        $sql = "Select * from donar where packageID=? AND reciever_id=?";
        $query = $this -> db -> query($sql, array($UserInfo -> packageID, $UserInfo -> id));
        // return $query -> result();
        if($query -> num_rows() >= 2) {
            // return 'Persons';
            // if user cycle has not been completed but user has tow recievings in the table then again search fot the next user in the array
            $next = 1;
            for ($i = 1; $i < count($key); $i++) {
                $nexPerson = current(array_slice($key, array_search($key, array_keys($key)) + $i, 1));
                $found = $this -> findNextPerson($UserInfo, $nexPerson, $id);
            }
        }
        else {
            $sql = "Select * from donar where packageID=? AND donarID=?";
            $query = $this -> db -> query($sql, array($UserInfo -> packageID, $id));
            if($query -> num_rows() >= 1) {
                // return false
            }
            else {
                // if the rows of the user are less then 2 record then simply insert the erecord of the user                  
                $allotDonar = $this -> db -> insert('donar', array('donarID'    =>  $id, 'reciever_id'  =>  $UserInfo -> id, 'packageID'    =>  $UserInfo -> packageID, 'date_added'    =>  $date, 'date_expiry'    =>  $endDate, 'wait_time' =>  $formatDate, 'wait_start' =>  $date));
                return $this -> db -> insert_id();
            }
        }
    }
    else {
        return 'No person found';
    }

}

function findNextPerson($UserInfo, $nexPerson, $id) {
    $date = date('Y-m-d h:i:s a');
    $currentDate = strtotime($date);
    $futureDate = $currentDate+(60*1);
    $formatDate = date("Y-m-d h:i:s a", $futureDate);
    $endDate = date("Y-m-d h:i:s a", strtotime('+30 hours'));

    if(!empty($nexPerson)) {
        $nextPersonID = $nexPerson -> id;
        // check if next pserons has paid money or not
        $checkIfNextPaid = $this -> db -> query("Select * from user where canRecieve='1'");
        if($checkIfNextPaid -> num_rows() > 0) {
            $sql = "Select * from donar where packageID=? AND reciever_id=?";
            $query = $this -> db -> query($sql, array($nexPerson -> packageID, $nexPerson -> id));
            // return $query -> result();
            if($query -> num_rows() >= 2) {
                return 'Go back';
            }
            else {
                $sql = "Select * from donar where packageID=? AND donarID=?";
                $query = $this -> db -> query($sql, array($UserInfo -> packageID, $id));
                if($query -> num_rows() >= 1) {
                }
                else {
                // $currentID = $UserInfo -> id;
                    $allotDonar = $this -> db -> insert('donar', array('donarID'    =>  $id, 'reciever_id'  =>  $nexPerson -> id, 'packageID'    =>  $nexPerson -> packageID, 'date_added'    =>  $date, 'date_expiry'    =>  $endDate, 'wait_time' =>  $formatDate, 'wait_start' =>  $date));
                    return $this -> db -> insert_id();
                }
            }
        }
    }
}