Range of dates

1

I have a range of dates that is checking if the dates entered by a user coincide with some day of offer in the database, to coincide, print the new price, but it gives me two problems: 1) If I have more than two offers in the DB, it prints twice the prices and dates 2) if an offer is from 06-04-2018 until 08-04-2018 it gives me the new price, but it is returned to me on 06-05-2018 until 06-05-2018

date range code:

        $price = Data::PriceSearch("price_hdcd");

for($i=$dateinstr; $i<=$dateoutstr; $i = date("Y-m-d", strtotime($i ."+ 1 days"))){ 
                    $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.'</td>
                                    <td>'.$priceout['cd'].' &euro;</td>
                                    <td>'.$priceout['mp'].' &euro;</td>
                                    <tr>';
                        } else {
                          $totalcd = $PriceSystemArray['cd'];
                          $totalmp = $PriceSystemArray['mp'];
                              echo '
                                    <tr>
                                    <th scope="row">'.$STR.'</td>
                                    <td>'.$totalcd.' &euro;</td>
                                    <td>'.$totalmp.' &euro;</td>
                                    <tr>';
                            }

                        }
                      }
                    }
                  }

db code:

public function PriceSearch($table){
        $stmt = Connection::Connect()->prepare("SELECT * FROM $table");
        $stmt->execute();
        return $stmt->fetchAll();
    }
    
asked by Daniel Parra 02.04.2018 в 18:44
source

1 answer

1

You could improve your query by adding conditions and a limit:

SELECT * FROM $table WHERE $fechaUsuario BETWEEN dateini AND datefin LIMIT 1
    
answered by 02.04.2018 в 18:54