使用php

时间:2018-02-15 17:37:23

标签: php mysql mysqli

我有一个名为coins的数据库表

id | owner_id | amount
1  |    3     |  1000 
2  |    22    |  250 
3  |    45    |  450 
4  |    21    |  300
......

问题是: 有2000个硬币归其他人所有。

如果用户购买600个硬币,则必须从其他用户处取走600个硬币。 选择的最佳方式是什么(只有总共600个硬币的行)并更新表硬币?

因为没有人拥有600个硬币。硬币必须从拥有至少600个硬币的人或最少600个人的几个人那里拿走

1 个答案:

答案 0 :(得分:0)

$MoreThan600 = mysqli_query($conn, "SELECT `owner_id` FROM `table` WHERE `amount` > 599 ORDER BY `amount` DESC");
if(mysqli_num_rows($MoreThan600) > 0){
    $ownerQ = mysqli_query($conn, "SELECT `owner_id` FROM `table` WHERE `amount` > 599 ORDER BY `amount` DESC LIMIT 1");
    $owner_id = mysqli_fetch_array($ownerQ);
    // access owner id with $owner_id[0] and do the coin exchange here
}else{
    // do others have in total ahve more than 600?
    $x = 0;
    $participants = array();
    $people = mysqli_query($conn, "SELECT * FROM `table`");
    while($person_info = mysqli_fetch_array($people)){
        $x+=$person_info['amount'];
        if($x > 599){
            break; // no more people needed.
        }else{
            array_push($participants, $person_info['owner_id']);
        }
    }

    // now take all their money(fairly)!
    $tax = 600 / count($people);
    $leftovers = 0;
    for($i = 0; $i < count($people); $i++){
        $walletQ = mysqli_query($conn, "SELECT `amount` FROM `table` WHERE `owner_id`='$people[$i]'");
        $old_amount = mysqli_fetch_array($walletQ);
        if($old_amount < round($tax)){
            // take all their money
            $leftovers += (round($tax) - $old_amount);
            mysqli_query($conn, "UPDATE `table` SET `amount`='0' WHERE `owner_id`='$people[$i]'");
        }else{
            $new_amount = $old_amount[0] - round($tax);
            mysqli_query($conn, "UPDATE `table` SET `amount`='$new_amount' WHERE `owner_id`='$people[$i]'");
        }
    }

    if($leftovers > 0){
        // Someone (or people) will pay for leftovers
        $debtQ = mysqli_query($conn, "SELECT `owner_id` FROM `table` WHERE `amount` > '$leftovers'");
        if(mysqli_num_fields($debtQ) > 0){
            $owner_id = mysqli_fetch_array($debtQ);
            // access owner id with $owner_id[0] and make him pay for debt
        }else{
            // take everyones money until its paid (unfairly)
            while($person = mysqli_fetch_array($people)){
                $money = $person['amount'];
                mysqli_query($conn, "UPDATE `table` SET `amount`='0' WHERE `owner_id`='$person[$i]'");
                $leftovers -= $money;
                if($leftovers < 1){
                    // success. debt paid
                    echo "success";
                    // give 600 to the kiddo
                    break;
                }
            }

            // check if the while loop worked to pay debt
            if($leftovers > 0){
                // i guess everyone is poor. thus as a collective they all cant pay 600
                echo "No one can pay 600, everyone cant pay 600 even when combined";
            }
        }
    }else{
        // give 600 to the kiddo
    }


    # END! :D
}

&#34;几个人的总数至少为600&#34; &lt; - 他们的id存储在一个名为$ participant

的数组中

如果香港专业教育学院评论为孩子们提供600,那么你应该向获胜者提供600个硬币。如果我做了硬币交换,你需要从$id = $owner_id[0]拿走600并将其交给获胜者

任何questinons?我在某处出错了吗?我怎样才能改进我的代码?