简体   繁体   English

遍历结果PHP或Jquery

[英]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. 我试图在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. 自产品发布以来,每一小时的每一天价格都下降10%。 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. 在数据库中增加1行,并将其称为价格。 - 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. -编写一个CRON,它将每小时在服务器上运行一次,以查看所有产品并在该表中设置新价格。 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. 在数据库中添加10条额外的行,并在添加产品时生成价格。 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. 我猜我可以通过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. 这是我为3ed选项提供的代码。 As i dont know jquery i dont have one for the second one. 由于我不知道jQuery,我没有第二个。

$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. 不过,一个简单的javascript函数只是最简单的方法,该函数仅根据now和发布日期之间的差来计算减少百分比。

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. 这是唯一确定是否需要优化的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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