For a marketing company, on the personal page of each marketeer I need to output the number of sales (made by the marketeer) from the current week, the commision, which is based on the number of sales made, and the amount of commision a marketeer made (number of sales * commision). Right now I have the following function for the number of sales from the current week:
function get_week(){
$sql = "SELECT SUM(sales_made)
FROM marketeer_sales
WHERE marketeer_id = ?
AND WEEKOFYEAR(sales_date) = WEEKOFYEAR(CURDATE())";
$stmt = &this->pdo->prepare($sql);
$stmt->execute(array(1));
$row = $stmt->fetch();
$num_of_sales = $row['SUM(sales_made)'];
return $num_of_sales;
}
I have the functions for the commision (get_commision()) and amount (get_deserves()) as well, but in both I am using the exact same query as in the get_week() function and in the get_deserves() function, I even used the values from the get_commision() function, to come to get results as you can see below.
This is working but, I’m sure there is a way more effective way, to get the same results by using the values from get_week() in the other two functions, without having to use the same query again but I can’t think of one
Any advise and/or tips would be highly appreciated.
So, the number of sales is always the same. Why not make a function ‘get_number_of_sales()’?
Basically, you would just rename the function get_week() for get_num_sales().
function get_num_sales(){
$sql = "SELECT SUM(sales_made)
FROM marketeer_sales
WHERE marketeer_id = ?
AND WEEKOFYEAR(sales_date) = WEEKOFYEAR(CURDATE())";
$stmt = &this->pdo->prepare($sql);
$stmt->execute(array(1));
$row = $stmt->fetch();
$num_of_sales = $row['SUM(sales_made)'];
return $num_of_sales;
}
You could also leave the name ‘get_week’ but IMO this function returns the number of sales, not ‘weeks’? Maybe call it get_num_sales_for_week() or something like that.
Anyway, this function returns the number of sales, which is what you need in your other functions?
I’m not entirely sure why you’re calling all these functions. If the information’s gonna be displayed regardless, seems a bit of a waste to hand things back and forth.
Execute the query once (any time you execute a query more than once should be a red flag in your coding process!); store the results, and if necessary to keep everything in functions, pass the relevant information into the function as a parameter.
Maths time!
You’re using a system that has a pattern, and can be simplified as follows:
What we’re missing is how you’re calling this information. Are these calculated all at once, or are they called at separate time? The answer changes depending on how it’s called.
If it’s separately, then the three method approach is appropriate. If it’s all at once, then the single hit approach is best:
You have broken it up incorrectly so you can’t understand the first and last part.
Here’s the way you break up a ?: statement like that into it’s component parts. It brreaks into three parts marked by the ? and the : and the part before the = applies to the parts on both sides of the?:
The if portion of the ?: is:
Like I said felgall, the entire middle part was confusing for me but after Dave’s answer I understood the principle. But thank you for the clear explanation