Jump to content

Need help with logic


peter_anderson

Recommended Posts

I have a file sharing script, where each file has a certain number of points. I need to subtract a set number of points from the total. I'm unsure about the logic of it.

 

This is how I total the number of points:

<?php
$fq = "SELECT * FROM `files` WHERE owner='$username' AND active=1";
$fw = $sql->query($fq);
$reward = 0;
while($fr = $fw->fetch_assoc()){
$reward = $fr['points'] + $reward;
}?>

 

I'm thinking I should use a loop, while a the number to subtract is greater than or equal than 0.

 

Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/220745-need-help-with-logic/
Share on other sites

You have not provided enough information. How are you determining how many points to subtract/ There may be a way to do it with a single query.

 

For example, you shouldn't even be using a loop to calculate the total

$fq = "SELECT SUM(points) as total FROM `files` WHERE owner='$username' AND active=1 GROUP BY owner";
$fw = $sql->query($fq);
$reward = 0;
$fr = $fw->fetch_assoc();
$total = $fr['total'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.