S
S
samorez7772018-05-28 22:26:46
PHP
samorez777, 2018-05-28 22:26:46

Progress Bar percentage calculation. how in redbeanphp?

Gentlemen, you need to calculate the percentage for each product, and display it in a loop!
There are tables with products:
1) product

id  title      paysystem_id   price(стоимость)
1   Nike           1           200
2  Adidas          2           200
3  Reebok          3          200

Table 2:
pay (payment for goods)
id   pay_id      sum(оплаченно)     
1        1       400   
2        1       400   
3        2       600

Как рассчитать процент: Сумма для id 1 400+400 (pay.sum) / 200 product.price id 1 * 100% = id 1 %
Вот переменная для цикла объединяет 2 таблицы,
$products= \R::getAll("SELECT * FROM product JOIN pay ON product.paysystem_id = pay.pay_id");
как мне в нее вставить запрос для расчета процентов.
<?php foreach ($products as $product): ?>
..............................
<?php endforeach; ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2018-05-29
@Immortal_pony

Unfortunately, I was unable to decipher your formula. Can you describe it in more detail?
Especially the "*100%" section. What it is? How do you imagine multiplying by 100%?
The general principle of making a request is as follows:

SELECT 
    `product`.*,
    SUM(`pay`.`sum`)/`product`.`price`*100 AS 'percent' -- исправьте эту строку, применив правильную формулу.
FROM 
    `product`
    JOIN `pay`  ON `product`.`paysystem_id` = `pay`.`pay_id`
GROUP BY `product`.`id`

Further:
<?php foreach ($products as $product): ?>
    <?= $product['percent'] ?>
<?php endforeach; ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question