K
K
kirill3215922019-02-03 21:53:08
PHP
kirill321592, 2019-02-03 21:53:08

Php referral system, how to shorten the code?

I am writing a multi-level referral system with ranks. Those. 5 levels and ranks for the number of invitees, each has a different percentage of deductions. When registering, I enter the id of the inviting referral. Further, when a referral buys a package, I make deductions

$dataRef = $this->db->column('SELECT ref FROM accounts WHERE id = :id', 
      ['id' => $data['uid']]);
        if ($dataRef === false) {
            return false;
        }
        if ($dataRef != 0) {
            $refSum = round((($data['amount'] * 15) / 100), 2);
            $params = [
                'sum' => $refSum,
                'id' => $dataRef,
            ];
            $this->db->query('UPDATE accounts SET refBalance = refBalance + :sum WHERE id = :id', $params);

Then along the chain to a person one level higher
$dataRef2 = $this->db->column('SELECT ref FROM accounts WHERE id = :id', ['id' => $dataRef]);
                if ($dataRef2 === 0) {
                    return false;
                }
                if ($dataRef2 != 0) {
                    $refSum = round((($data['amount'] * 10) / 100), 2);
                    $params = [
                        'sum' => $refSum,
                        'id' => $dataRef2,
                    ];
                    $this->db->query('UPDATE accounts SET refBalance = refBalance + :sum WHERE id = :id', $params);

The question is whether it is possible to somehow make it more elegant and shorten the code? And then I will have 40 such pieces, to check for level and rank.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitsliputsli, 2019-02-03
@Vitsliputsli

Make repetitive code a separate function

D
Danil Sapegin, 2019-02-03
@ynblpb_spb

banal loop do { ... } while();

S
sergmit, 2019-02-04
@sergmit

Try using nested sets

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question