简体   繁体   中英

Loop Through results PHP or Jquery

I have been working on this for almost three days with no good result. I am trying to get this done in Laravel. But I am looking for any type of good ideas.

I have a product that has a MSRP price. The price for every product drops 10 percent every single day since the product was posted, to the hour. But i would like to show the price drop for every day as you can see in the picture posted.

Here are some way i have been trying to do it. None of them seem very clean.

  1. Add 1 extra row to the database and call it price. - write a CRON that will run every hour on the server looking at the all the products and setting the new price in that table. But that will mean that I will be using a lot of resources from the server.

  2. Add 10 extra rows in the database and generate the prices when the product is added. However I am having an issue trying to figure out how to show what day it is and highlighted in red. I am guessing I could do this via jquery. Dont know it well but will that be a way?

  3. Pull the products from that database and then cycle through each one and use this code that will generate the table and prices. But than again if the flow of traffic is huge it will eat a lot of the server.

Any other ideas? Anything will help that can help me streamline this.

Here is the code I was able to come up with for the 3ed options. As i dont know jquery i dont have one for the second one.

$msrp = 29.59;
$dayposted = "2016-1-26";
$cDate = Carbon::parse($dayposted);
$today = $cDate->diffInDays();
$days = [];
$i = 1;
$percent = 0;
for($n=0 ;$n<10; $n++) {
    setlocale(LC_MONETARY, 'en_US.UTF-8');
    if ($i == $today) {
        $day = array_add(['day' => 'Day '.$i], 'price' , number_format( $msrp *      ((100-($i*10))/100),2, '.', ''));
        $day = array_add($day, 'class', 'today');
        $price = number_format( $msrp * ((100-($i*10))/100),2, '.', '');
    }
    elseif($i == 10) {
        $day = array_add(['day' => 'Final'], 'price' , number_format( $msrp * ((100-($i*10))/100),2, '.', ''));
        $day = array_add($day, 'class', '');
    }
    else{
        $day = array_add(['day' => 'Day '.$i], 'price' , number_format( $msrp * ((100-($i*10))/100),2, '.', ''));
        $day = array_add($day, 'class', '');
    }
    array_push ($days, $day);
    $i++;
}

Also here is what I am looking to get done.

Press here to see the image

The answer is it depends on when you want the work to be done (the calculation).

I agree that doing the calculation every time a user requests the information if you don't have to is an obvious waste of resources. However , mathematical calculations are not very intensive so long as you're not doing a lot of iterating, querying or worst of all counting things in the database.

That being said, if you really want to make sure these calculations don't bog down your server the solution I'd suggest is to do it on the client.

You return the raw data to the client and calculate the pricing on the browser, this way the calculation is done off the server and only when requested. This may not be an option, however, because you may not want to expose your pricing method to the client. Still, a simple javascript function that just calculates the % reduction based on a difference between now and the date posted would be easiest.

Regardless, this might be a case of micro-optimization. If you're concerned, do some benchmarks and see how your server handles it under load. That's the only way to know for sure if this optimization is necessary or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM