Echo whole numbers (from array) from calculation from item price - php

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>';
}

Related

Buy One Get One Half Price in PHP shopping cart total

I am creating a shopping cart in PHP and a particular item is buy one get one half price. When the user purchases the item, I would get like the offer to be deducted from the total but I'm stuck on how I would do this mathematically.
So far I have something like this in a if loop getting data from database:
$total = $total+($arraycart['Price']*$quantity);
Then I think it will be something along the lines of:
if ($arraycart['Item'] == "A1" and $quantity > 1) {
//calculate here buy one get one half price
}
Any help appreciated.
<?php
$total = 0;
$arraycart['Price'] = 10;
$arraycart['Item'] = 'A1';
$quantity = 3; // change item quantity here
if ($arraycart['Item'] == "A1" and $quantity % 2 == 0 ) {
//calculate here buy one get one half price
$real = ($quantity/2)*$arraycart['Price'];
$half = ($quantity/2)*($arraycart['Price']/2);
$total = $real+$half;
} else {
$quantity = $quantity-1;
$real = ($quantity/2)*$arraycart['Price'];
$half = ($quantity/2)*($arraycart['Price']/2);
$total = $real+$half+$arraycart['Price'];
}
echo $total;
?>

How to get sales ranking in PHP Codeigniter?

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;
}

Evenly reducing an indexed array by 10%

I have an array filled with data over the period of a month. The data is computed for every 15 minutes over that period, meaning it's got about 2880 entries.
I need to reduce it by about 10% in order to display the data in a chart (288 data points will render much more nicely than 2880).
Here's what I've tried (it works, but it might be a very bad method):
$count = count($this->Data1Month);
for($i = 0; $i < $count; $i += 10) {
$tempArray[] = $this->DataMonth[$i];
}
$this->Data1Month = $tempArray;
I think you have the most efficient solution, but you do have a mistake though. Array indexes start at zero so 0+10 needs to be 9, like so:
$count = count($this->Data1Month);
for($i = 0; $i < $count; $i += 9) {
$tempArray[] = $this->DataMonth[$i];
}
$this->Data1Month = $tempArray;

Deducting 25% from more than one item in loop

I wonder if someone could please help me out a little. I have a loop that loops through the contents of a shopping cart. I wish to apply a 25% discount to additional items purchased. So basically 1st item is full price and every other item is reduced by 25%. I've tried various methods but all i seem to get is the discount apply to all or nothing.
The loop below works perfectly if i remove the if statement and its contents thus not wishing to apply a discount. As it currently stands it does not add a discount at all. If i remove the if condition and use it's contents then it will apply a 25% discount to all items.
for($Loop = 0; $Loop < count($Cart); $Loop++)
{
$Total += $ShoppingCart[$Loop][Price];
if($Loop > 1) {
$Total += $ShoppingCart[$Loop][Price];
$PercentageAmount = 25;
$TotalPrice = $TotalPrice * ((100-$PercentageAmount) / 100);
}
}
Edited:
Unfortunately none of the answers, although maybe technically good, do not fix my problem. I have had to result to placing 2 if statements within a loop and then calculating their combined total. Not an ideal solution but works perfectly never the less. Somehow i need the sort it so then the most expensive item is at full price. It would be much easier if i was not tied to using a loop in this fashion and instead could use array functions.
$i = 0;
for($Loop = 0; $Loop < count($Cart); $Loop++)
{
if($i == 0) {
$Total += $ShoppingCart[$Loop][Price];
}
if($i > 0) {
$TotalMulti += $ShoppingCart[$Loop][Price];
$TotalMulti = $TotalMulti * .75;
}
$i++;
}
$NewTotal = $Total + $TotalMulti;
Here's how I would do it:
$prices = array_column($ShoppingCart, 'Price');
array_walk($prices, function(&$price, $i) { if($i) $price *= .75; });
$total = array_sum($prices);
How it works:
The prices are pulled out in their own array -- I much prefer this because the discount code does not mess with the "normal" prices, which might cause unexpected complications.
The array of prices is iterated over, and every element but the first is set to 75% of its value.
The total price is just the sum of the discounted prices.
This code depends on array_column, which is only available starting with PHP 5.5. For earlier versions you can either grab an implementation from here or substitute this:
$prices = array_map(function($el) { return $el['Price']; }, $ShoppingCart);
If the discount percentage is a variable you will also need this modification:
$discount = .25;
array_walk(
$prices,
function(&$price, $i) use($discount) { if($i) $price *= (1 - $discount); }
);
for($Loop = 0; $Loop < count($Cart); $Loop++){
if($Loop > 0) {
$Total += ($Cart[$Loop]['Price']*0.75);
} else {
$Total += $Cart[$Loop]['Price'];
}
}
Hope this helps:
for($Loop = 0; $Loop < count($Cart); $Loop++)
{
# if not first item then discount price
if($Loop > 1) {
$PercentageAmount = 25;
# grab the price
$itemPrice = $ShoppingCart[$Loop][Price];
# what should the user pay?
$itemDiscountPrice = $itemPrice * (100.0 - $PercentageAmount) / 100.0
# add it to total
$Total += $itemDiscountPrice;
} else {
# if first item -> full price
$Total += $ShoppingCart[$Loop][Price];
}
}

Find out the nearest bigger multiple of (n) PHP

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

Categories