i am stuck in a logic. I want to generate a monthly base ranking of sales. The scenario of my ranking is, different sales man have different boxes to sold, they sold a box and i save there quantity of boxes. Now every person have different id and different no. of boxes sold. Can anyone suggest me how can i rank them?? I am using the code-igniter.
And i want to rank them in two different ways (no of boxes sold and amount of sales they generated)
Thanks in advance.
$rank = 0;
$array = array();
$j =0;
for($i = 0; $i <count($sales_ranking); $i++){
$quantity = 0;
$key = $sales_ranking[$i]->fmid;
if (in_array($key, $array)) {
echo "<br>__".$j = $j++;
continue;
}else{
array_push($array, $key);
$l = $i - $j;
for($j = 0; $j <count($sales_ranking); $j++){
if($sales_ranking[$i]->fmid == $sales_ranking[$j]->fmid){
$quantity = $quantity + $sales_ranking[$j]->qty;
$name[$l] = $this -> sales_rank -> get_name($sales_ranking[$i] -> fmid);
}else{
continue;
}
}
}
$qty[$l] = $quantity;
}
Related
Excuse me, I want to ask about the iteration step.
i have productdata like this
and criteria data
and alternative data as a liaison / place for the main data from the two tables above
i want to make iteration or loop $nilaiprefensi like below
with calculations like this
$nilaiprefensi[0]= [["A1,A2"],[2-4],[5-9],[10-9],[3-9],[6-8],[4-8],[120-120]]
$nilaiprefensi[1]= [["A1,A3"],[2-3],[5-8],[10-6],[3-8],[6-16],[4-9],[120-155]]
..
$nilaiprefensi[5]= [["A2,A1"],[4-2],[9-5],[9-10],[9-3],[8-6],[8-4],[120,120]]
I've tried several times to make this loop function, but it doesn't work, here's my latest code
for ($i = 0; $i <$loop; $i++) { //30
for ($j = 0; $j < count($products); $j++) {
if ($i !== $j) {
$p[$i][$j][0] = ('A' . $i + 1) . ' , A' . $j + 1;
for ($k = 1; $k < count($kriteria); $k++) {
$p[$i][$j][$k]=$alternatif[$k-1]->kriteria_data."-".$alternatif[$i]->kriteria_data;
}
}
}
}
dd($p);
What is the correct looping function to get the expected result, please help, thanks 🙏🙏
I am trying to fix this percent calculation, however it is just stumping me today.
Here is the code:
$entries = GFAPI::get_entries($form['id'], $search_criteria);
$score = 0;
$max = 0;
$percentage = array();
if(!empty($entries)) {
foreach ($entries as $entry) {
$score = GFSurvey::get_field_score($form_fields, $entry);
$max = end($form_fields['choices']);
if(empty($max['score'])) {
unset($form_fields['choices'][key($form_fields['choices'])]);
$max = end($form_fields['choices']);
}
$max = $max['score'];
$percentage[] = ($score / $max ) * 100;
}
}
$average = round(array_sum($percentage) / count($percentage), 2);
I have the form and i have Not Applicable radio buttons on the form. When a client fills out the form, sometimes on certain questions they need to be N/A because they do not apply and that does not need to counted in the overall total score.
So that is the report generated which the % is incorrect. That percent should read: 94%. In this picture you will see if you click on the graph you can see this:
Graph Once Clicked
It is showing the people who answered this question, and there are 20. There are a total of 5 max points for each person, or in this case I have the N/A box set for blank, which returns 0. What it is doing is totaling all the possible points which are 100. (20 people and 5 max points)
What I need it to do is NOT count the blank fields and in return give me for example in the image Graph Once Clicked there are only 5 people that answered so max points are 25. the total points is 23.5 so 23.5 / 25.
How about this code ? The total percentage of answered questions is in variable $total_percentage.
$entries = GFAPI::get_entries($form['id'], $search_criteria);
$score = 0;
$max = 0;
$total_max = $total_score = 0;
$percentage = array();
if(!empty($entries)) {
foreach ($entries as $entry) {
$score = GFSurvey::get_field_score($form_fields, $entry);
$max = end($form_fields['choices']);
if(empty($max['score'])) {
unset($form_fields['choices'][key($form_fields['choices'])]);
$max = end($form_fields['choices']);
}
$max = $max['score'];
if ($max) {
$total_score += $score;
$total_max += $max;
}
$percentage[] = ($score / $max ) * 100;
}
}
$average = round(array_sum($percentage) / count($percentage), 2);
$total_percentage = ($total_max > 0) ? round($total_score/$total_max/100, 2) : 0;
So, I want to distribute evenly lists across 3 columns. The lists cannot be broken up or reordered.
At the moment, I have 5 lists each containing respectively 4, 4, 6, 3 and 3 items.
My initial approach was:
$lists = [4,4,6,3,3];
$columns = 3;
$total_links = 20;
$items_per_column = ceil($total_links/$columns);
$current_column = 1;
$counter = 0;
$lists_by_column = [];
foreach ($lists as $total_items) {
$counter += $total_items;
$lists_by_column[$current_column][] = $total_items;
if ($counter > $current_column*$links_per_column) {
$current_column++;
}
}
Results in:
[
[4],
[4,6],
[3,3]
]
But, I want it to look like this:
[
[4,4],
[6],
[3,3]
]
I want to always have the least possible variation in length between the columns.
Other examples of expected results:
[6,4,4,6] => [[6], [4,4], [6]]
[4,4,4,4,6] => [[4,4], [4,4], [6]]
[10,4,4,3,5] => [[10], [4,4], [3,5]]
[2,2,4,6,4,3,3,3] => [[2,2,4], [6,4], [3,3,3]]
Roughly what you need to do is loop over the number of columns within your foreach(). That will distribute them for you.
$numrows = ceil(count($lists) / $columns);
$thisrow = 1;
foreach ($lists as $total_items) {
if($thisrow < $numrows){
for($i = 1; $i <= $columns; $i++){
$lists_by_column[$i][] = $total_items;
}
}else{
//this is the last row
//find out how many columns need to fit.
//1 column is easy, it goes in the first column
//2 columns is when you'll need to skip the middle one
//3 columns is easy because it's full
}
$thisrow++;
}
This will be an even distribution, from left to right. But you actually want a modified even distribution that will look symmetrical to the eye. So within the foreach loop, you'll need to keep track of 1.) if you're on the last row of three, and 2.) if there are 2 remainders, to have it skip col2 and push to col3 instead. You'll need to set that up to be able to play around with it,...but you're just a couple of logic gates away from the land of milk and honey.
So, I ended up using this code:
$lists = [4,4,6,3,3];
$columns = 3;
$total_links = 20;
$items_per_column = ceil($total_links/$columns);
$current_column = 1;
$lists_by_column = [];
for ($i = 0; $i < count($lists); $i++) {
$total = $lists[$i];
$lists_by_column[$current_column][] = $lists[$i];
//Loop until reaching the end of the column
while ($total < $items_per_column && $i+1 < count($lists)) {
if ($total + $lists[$i+1] > $items_per_column) {
break;
}
$i++;
$total += $lists[$i];
$lists_by_column[$current_column][] = $lists[$i];
}
//When exiting the loop the last time we need another break
if (!isset($lists[$i+1])) {break;}
//If the last item goes onto the next column
if (abs($total - $items_per_column) < abs($total + $lists[$i+1] - $items_per_column)) {
$current_column++;
//If the last item goes onto the current column
} else if ($total + $lists[$i+1] > $items_per_column) {
$i++;
$lists_by_column[$current_column][] = $lists[$i];
$current_column++;
}
}
I have a tricky question and would appreciate some suggestions.
A customer sets a price, then that price determines the number of customers that can purchase the item.
I think im looking to have an item price set from a form, then carry out a calculation in a loop, hold this in an array then echo out only whole numbers from the array to an options box which is populated by ajax onlclick.
At least I think thats how to do it.
E.G
itemPrice=50, therefore only the following no of customers can purchase the item.
50,25,10,5,2 or 1 as these are the only whole numbers from the calculation (this should be offered as a combo box or slider).
$itemprice = 50
for ($i=1, $i < $itemprice, ++$i) {
$array .= $i
}
for each {
is_int($array){
$NoOfCustomersList .= <option value="$NoOfCustomers">$NoOfCustomers</option>
//(or jQuery slider)
}
}
DISCLAIMER
At least this is how it would look in my mind, I am new to php so the above may be jibberish
If you mean you want only the numbers that divide 50, you can do this
$itemPrice = 50;
$array = array();
for ( $i = 1; $i <= $itemPrice; $i++ ){
if ( $itemPrice % $i == 0 ){ $array[] = $i; }
}
foreach( $array as $item ){
echo '<option value="', $item ,'"/>', $item ,'</option>';
}
I have a requirement where users are forced to choose the multiple of (n) quantity of a product.
The (n) value is set with each product that can be any number.
customer can only purchase the quantity of product in the multiple of (n) quantity set with product.
Suppose if (n) is 5 and user entered quantity as 4 and says Add to Cart. I have to add quantity of that product as 5 automatically.
and if user entered 6 as quantity then I have to add the 10 quantity of that product.
How I go about that?
I am not getting what logic should be applied here.
$entered_quantity = 6;
$suppose_n = 5;
$quantity = ceil($entered_quantity / $suppose_n) * $suppose_n;
echo $quantity;
prints 10
that's not php specific;
what you wonna do is to compute.
ceiling(q / n) * n
where q is the user's quantity,
n is the multiplicity
You could try getting the remainder of the number when dividing by the given n
e.g.:
$n = 5;
$amount = 6; // This would be the input, so replace the 6 with a $_POST/$_GET/etc.
$batches = floor($amount / $n);
$rest = $amount % $n;
if ($rest > 0) {
$batches += 1;
// You could give the user feedback here that they didn't put in a full multiple of $n
}
// $batches now contains the right amount of batches, so to get the total:
$total = $batches * $n;
Ofcourse this can be condensed a lot, but this might give a better overview of what happens :).
Try the below function.
function getNextMultipleOfFive($n) {
$tmp=explode('.',($n/5));
if($tmp[1]) {
return ($tmp[0]+1)*5;
}
return $tmp[0]*5;
}
With a do...while loop:
$q = 6; // quantity by user input
$n = 5; // per purchace amount
$i = 0;
if ($q > 0)
{
do
{
$i += $n;
}
while ($i <= $q);
}
echo $i; // 10
Note: not very effective if $q >> $n