Home controller:
//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;
}
In function private function calculatePricePerPerson I am calculating the price for a number of person and number of days based on date. At the moment I have calculated like this. The problem is a calculating number of is from 1 to 21 so therefore my foreach loop get really long and it is really ugly. So My question is how can I do this in better and more efficient way.