0

I have looked around in stack overflow but nothing really helped me further.. So I hoped someone could answer my question/help directly. I can't seem add a sum and this with a limit. I want to get the average result for every week (sum with a limit basically).

<form action="inft.php" method="POST">
    <button type="submit" name="asubmit">Average</button>
</form>

<?php

    include_once 'dbh.inc.php';

    $dpl = $db->query("SELECT SUM(`gewicht`) as `total` FROM bgewicht");

    $row = $dpl->fetch_assoc();

    echo $row['total'];

?>

My apologies if this question was asked somewhere before, my apologies for my ignorance too. I just started programming.

With kind regards,

D.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Did you see "https://stackoverflow.com/questions/9877872/mysql-select-sum-of-results-with-a-limit" – AMIT Dec 04 '17 at 10:47
  • 3
    Do you mean `GROUP BY WEEK(datefield)`? or `YEARWEEK()` whats `sum and this` and `AVERAGE` posta to mean, is it a SUM or an AVG – ArtisticPhoenix Dec 04 '17 at 10:47
  • Did you see "https://stackoverflow.com/questions/9877872/mysql-select-sum-of-results-with-a-limit" – AMIT Dec 04 '17 at 10:49
  • @ArtisticPhoenix I want to fetch the row "gewicht", it basically represents your weight. short explained (the website). You fill in your weight for 7 days and the website should output an average of those 7 numbers. but I dont want the second week to get mixed with week one. so every 7 days it should start all over again but without losing the average of the first week. I'm sorry if this isn't clarifying enough. –  Dec 04 '17 at 10:59
  • Dude based on your last comment, you need to store only the avg of each week in the DB. if so. you need to change the structure as I'm guessing, and if not please explain more, the piece of code you have posted is almost not related and it doesn't contain limit or clear code. I would love to help you but I did not get your question! – Mohamed Mo Kawsara Dec 04 '17 at 11:19
  • @MohamedKawsara I'm sorry, the code displayed was something I read and tried (the php code). basically I want an output average of the first week, second week, third week etc. but I don't want the first weeks average to be combined with the second week its average. I went on to search a lot and basically tried every coding that came close to what I wanted. but guessing from your reaction the code isn't exactly the code I had to use for my end result. –  Dec 04 '17 at 11:25
  • Unfortunately, you are far away from it, I advise you to describe the problem in details (which you did I guess in your last comment) and then try to solve it, but don't use vanilla PHP, use Laravel ;) – Mohamed Mo Kawsara Dec 04 '17 at 17:41

1 Answers1

0
$dpl = $db->query("SELECT SUM(`gewicht`) 
 FROM (SELECT gewicht
  FROM bgewicht
  ORDER BY gewicht DESC
  LIMIT 3
) as `total` FROM bgewicht");
Javid Karimov
  • 435
  • 5
  • 22