【发布时间】:2017-08-22 12:34:42
【问题描述】:
家庭控制器:
//my query in repository
public function Available (){
$Sql= ' SELECT DISTINCT
a.property_id, a.date, a.minimum_stay,
a.maximum_stay,a.quantity,
p.duration, p.persons, p.amount,
p.extra_person_price, p.minimum_stay AS price_minimum_stay,
p.maximum_stay AS price_maximum_stay, p.weekdays
FROM availabilities AS a
JOIN prices AS p
ON a.property_id=p.property_id
WHERE a.minimum_stay >0
AND a.maximum_stay < 22
AND a.date >= p.period_from
AND a.date <= p.period_till
';
class HomeController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$hotel_information = $this->retrieveInformationFromDB();
return $this->render('Home/index.html.twig', array(
'hotel_information'=>$hotel_information
)
);
}
private function retrieveInformationFromDB(){
$em= $this->getDoctrine()->getEntityManager();
$availables = $em->getRepository('AppBundle:Availabilities')->Available();
$hotel_information=array();
for($i = 0; $i< count($availables) ; $i++) {
$date = $availables[$i]['date'];
$duration = $availables[$i]['duration'];
$amount = $availables[$i]['amount'];
$extra_person_price = $availables[$i]['extra_person_price'];
$persons = explode('|',$availables[$i]['persons']);
$hotel_information_placeholder = $this->calculatePricePerPerson($persons, $extra_person_price, $date, $amount,$duration);
$hotel_information = array_merge($hotel_information, $hotel_information_placeholder);
}
return $hotel_information;
}
private function calculatePricePerPerson($persons, $extra_person_price, $date, $amount,$duration){
foreach ($persons as $person) {
if ($person > 1) {
$price_per_person[] = array(
"date" => $date,
"person" => $person,
"price_person" => number_format((($person * $extra_person_price) + $amount) / 100, 2, '.', ','),
"day2"=>number_format(((($person * $extra_person_price) + $amount)*2) / 100, 2, '.', ','),
"day3"=>number_format(((($person * $extra_person_price) + $amount)*3) / 100, 2, '.', ','),
"day4"=>number_format(((($person * $extra_person_price) + $amount)*4) / 100, 2, '.', ','),
"day5"=>number_format(((($person * $extra_person_price) + $amount)*5) / 100, 2, '.', ','),
"day6"=>number_format(((($person * $extra_person_price) + $amount)*6) / 100, 2, '.', ','),
"day7"=>number_format(((($person * $extra_person_price) + $amount)*7) / 100, 2, '.', ','),
);
} else {
$price_per_person[] = array(
"date" => $date,
"person" => $person,
"price_person" => number_format($amount / 100, 2, '.', ','),
"day2"=>number_format(($amount * 2)/ 100, 2, '.', ','),
"day3"=>number_format(($amount * 3)/ 100, 2, '.', ','),
"day4"=>number_format(($amount * 4)/ 100, 2, '.', ','),
"day5"=>number_format(($amount * 5)/ 100, 2, '.', ','),
"day6"=>number_format(($amount * 6)/ 100, 2, '.', ','),
"day7"=>number_format(($amount * 7)/ 100, 2, '.', ',')
);
}
}
return $price_per_person;
}
在函数私有函数calculatePricePerPerson中,我正在计算基于日期的人数和天数的价格。目前我是这样计算的。问题是计算的数量是从 1 到 21,因此我的 foreach 循环变得非常长而且非常难看。所以我的问题是如何以更好、更有效的方式做到这一点。
【问题讨论】:
-
那么使用另一个循环...?
-
那么我将有 3 个嵌套循环。可以举个例子吗?
-
首先规范化表格价格我看到你使用
explode('|',$availables[$i]['persons']);...表明设计不好..如果你规范化表格,你可以在你的数据库上进行价格计算,因为它应该是 -
“那么我将有 3 个嵌套循环” - 所以?你这么说好像它意味着什么...... “你能举个例子吗?” - 不,首先你尝试一些东西并向我们展示......(你需要使用一个临时变量要先组装数据,您不能再使用
$price_per_person[] = array(...一次性完成。) -
此价格有效的人数,以管道符号分隔我无法正常化。否则我可以在 sql 查询中做。这就是我问的原因。