how can I add some data

1

I have the following question.

I'm doing a reservation system for hotels in PHP. in some dates the price increases and in others it decreases, now I have brought that from the DB but I want to know how to add the normal prices: $ PriceSystemArray ['price'] with the increased prices: $ price ['price']

File php:

$nameroom = Data::RoomsViewModel("rooms");


  if($item['name'] == 'Habitación Doble C/D'){
  $PriceSystemArray = array('in' => $dateinstr,
                          'out' => $dateoutstr,
                          'price' => $item['price']);


  for($i=$PriceSystemArray['in']; $i<=$PriceSystemArray['out']; $i = date("Y-m-d", strtotime($i ."+ 1 days"))){
    $price = Data::PriceSearch("price_hdcd");
    $UNIX = strtotime($i);
    $STR = date("d-m-Y", $UNIX); 
      foreach ($price as $row => $priceout) {
        if ($STR >= $priceout['dateini'] && $STR <= $priceout['datefin'] ) {
          echo '
          <tr>
          <th scope="row">'.$STR.'</th>
          <td>'.$priceout['price'].'</td>
          <tr>'; 
        } else {
          echo '
          <tr>
          <th scope="row">'.$STR.'</td>
          <td>'.$PriceSystemArray['price'].'</td>
          <tr>';
        }
      }
    }
} 
    
asked by Daniel Parra 01.04.2018 в 17:20
source

1 answer

0

As I had told you in comments.

You could create an initialized accumulator at 0 , outside the loop, and accumulate in it the different values.

In this case the accumulator will be called $total . In PHP, to accumulate values you can use the allocation operator += as follows: $total+=$valorASumar , that is, on the left side the variable that serves as the accumulator, and after the operator += the value that will be added to what the accumulator already has.

Then, when you exit the loop, you print what has been accumulated.

For example:

  $total=0;
  foreach ($price as $row => $priceout) {
    if ($STR >= $priceout['dateini'] && $STR <= $priceout['datefin'] ) {
      $precio=$priceout['price']; 
      $total+=$precio;
      echo '
      <tr>
      <th scope="row">'.$STR.'</th>
      <td>'.$precio.'</td>
      <tr>'; 
    } else {
      $precioSistema=$PriceSystemArray['price'];
      $total+=$precioSistema;
      echo '
      <tr>
      <th scope="row">'.$STR.'</td>
      <td>'.$precioSistema.'</td>
      <tr>';
    }
  }
  echo "<tr><td>$total</td></tr>";
    
answered by 01.04.2018 / 19:15
source