如何通过select和foreach过滤日期?

时间:2019-06-11 13:50:17

标签: php mysql

我想知道我该怎么做,只保留第一个选择的日期,然后从另一个选择中删除另一个,而不删除它,就像将其保留在变量中一样。

我已经尝试过进行一次foreach,但是我不知道该怎么做。


function generateRestaurant($pdo){
    echo "generateRestaurant()";

    $startTime = mktime(0, 0, 0, date('n'), date('j')+1, date('Y')) - ((date('N'))*3600*24);

    $dateDebut = date('Y-m-d', $startTime);

    $dateFin = date('Y-m-d');

    $query = 'SELECT * FROM events as e 
    LEFT JOIN restaurants as r ON e.restaurant_id =  r.id_restaurant
    WHERE jour >= '.$dateDebut.' AND jour <= '.$dateFin.' ';
    $prep = $pdo->prepare($query);
    $prep->execute(); 

    echo $query;
    print_r($datas);

        selectRestaurants($pdo);
}

selectRestaurants功能:

function selectRestaurants($pdo){
    $query = 'SELECT * FROM restaurants as r 
    LEFT JOIN types as t ON r.type_id = t.id_type
    LEFT JOIN agences as a ON r.agence_id =  a.id_agence'; // Tous les éléments de ta table => TABLEAU
    $prep = $pdo->prepare($query);
    $prep->execute(); 

    $datas = $prep->fetchAll(PDO::FETCH_ASSOC);

    return $datas;
}

数据库:


agences : not used in this example.
events : id_event --- restaurant_id --- jour.
restaurants : id_restaurant --- nom --- type_id --- lieu --- agence_id --- horaire --- tickets.
types : not used in this example.
users : not used in this example.

我需要的是只选择本周的餐厅,首先选择它们,然后删除其他餐厅以保留我想要的日子。

这是一个算法,可以通过使用FullCalendar生成我们要在哪里吃饭,以及我们在两周内已经吃过的东西,而不是在两天之内在同一家餐厅和不同类型的餐厅用餐。

编辑

我很好,我已经解决了我的问题。

这里是解决方案:

function generateRestaurant($ pdo){

setlocale (LC_TIME, 'fr_FR.utf8','fra'); 
$startTime = mktime(0, 0, 0, date('n'), date('j')+1, date('Y')) - ((date('N'))*3600*24);

$dateDebut = date('Y-m-d', $startTime);
$dateFin = date('Y-m-d');

$query = 'SELECT * FROM events as e 
LEFT JOIN restaurants as r ON e.restaurant_id =  r.id_restaurant
WHERE e.jour >= "'.$dateDebut.'" AND e.jour <= "'.$dateFin.'" ';
$prep = $pdo->prepare($query);
$prep->execute(); 


$restaurantsSemaine = $prep->fetchAll(PDO::FETCH_ASSOC);

$restaurants = selectRestaurants($pdo);

$currentDay = strftime("%A");


foreach($restaurants as $key => $data){
    $currentIdRestaurant = $data['id_restaurant'];

    foreach($restaurantsSemaine as $restaurant){
        if($currentIdRestaurant == $restaurant['id_restaurant']){
            unset($restaurants[$key]);
        }
    }

    if($data[$currentDay] == 0 && isset($restaurants[$key])){
        unset($restaurants[$key]);
    }

}

shuffle($restaurants);

return reset($restaurants);

}

0 个答案:

没有答案