Variable qui est signalée comme undefined dans un foreach

Résolu/Fermé
kalen - 26 oct. 2022 à 17:43
 kalen - 26 oct. 2022 à 18:38

Bonjour,

Dans mon code je defini une variable $yTasks qui contien un array.

quand je fais un var_dump de cette variable tout marche mais quand je veut l'utiliser juste plus bas dans un foreach ces erreurs sont retournées

Warning: Undefined variable $ytasks in D:\Xammp\htdocs\my_php_projects\todo_list\index.php on line 66

Warning: foreach() argument must be of type array|object, null given in D:\Xammp\htdocs\my_php_projects\todo_list\index.php on line 66

voici mon code php:

$yTasks = array_filter($allTasks,function($e){
    return $e["date"] > $e['beforeYesterday'] AND $e["date"] < $e['today'];
});
var_dump($yTasks);
$new_y_tasks=array();
foreach( $ytasks as $key => $task ) {
    if($task['parentId']===0){
        array_push($new_y_tasks,$task);
    }
}
var_dump($new_y_tasks);

Et mon code en entier si besoin :

<?php
if(!isset($_COOKIE['connectionCookie'])){
    header('location:/my_php_projects/todo_list/login_page.php');
}
include "db.php";
$mail=$_COOKIE['connectionCookie'];
$date=DateTime::createFromFormat('d-m-Y H:i:s', date('d-m-Y').' 00:00:00')->getTimestamp();

$beforeYesterdayTimestamp=$date-(3600*24*2);
$yesterdayTimestamp=$date-(3600*24);
$todayTimestamp=$date;
$tomorowTimestamp=$date+(3600*24);

// delete old tasks

$delSQL="DELETE FROM tasks WHERE mail='$mail' AND date < '$yesterdayTimestamp' ";
$delReq= $db->prepare($delSQL);
$delReq->execute();

// moove in today yesterday's tasks which aren't checked

$getTasksToMooveSQL="SELECT id FROM tasks WHERE date = $yesterdayTimestamp AND checked = 0";
$getTasksToMooveReq= $db->prepare($getTasksToMooveSQL);
$getTasksToMooveReq->execute();

if($getTasksToMooveReq->rowCount()>0){
    $ids=$getTasksToMooveReq->fetchAll();
    $newIds=array();
    foreach ($ids as $id) {
        array_push($newIds,$id['id']);
    }

    $mooveSQL="UPDATE tasks SET date = $todayTimestamp WHERE ";
    foreach ($newIds as $key => $id) {
        $mooveSQL=$mooveSQL." id = ".$id." OR parentId = ".$id;
        if($key < count($newIds)-1){
            $mooveSQL=$mooveSQL." OR";
        }
    }
    $mooveReq= $db->prepare($mooveSQL);
    $mooveReq->execute();
}

$allTasksSQL="SELECT * FROM tasks WHERE mail='$mail' ORDER BY id";
$allTasksReq=$db->prepare($allTasksSQL);
$allTasksReq->execute();
$allTasks = $allTasksReq->fetchAll();

// add day to tasks
foreach ($allTasks as $key => $task) {
    $allTasks[$key]['beforeYesterday']=$beforeYesterdayTimestamp;
    $allTasks[$key]['yesterday']=$yesterdayTimestamp;
    $allTasks[$key]['today']=$todayTimestamp;
    $allTasks[$key]['tomorow']=$tomorowTimestamp;
}




// get tasks by day
$yTasks = array_filter($allTasks,function($e){
    return $e["date"] > $e['beforeYesterday'] AND $e["date"] < $e['today'];
});
var_dump($yTasks);
$new_y_tasks=array();
foreach( $ytasks as $key => $task ) {
    if($task['parentId']===0){
        array_push($new_y_tasks,$task);
    }
}
var_dump($new_y_tasks);

$todTasks = array_filter($allTasks,function($e){
    return $e["date"] > $e['yesterday'] AND $e["date"] < $e['tomorow'];
});

$tomTasks = array_filter($allTasks,function($e){
    return $e["date"] > $e['today'] ;
});



function displayTasks($tasks){
    foreach ($tasks as $task) {
        $value=$task['value'];
        $id=$task['id'];
        $checked=$task['checked']===1?'checked':'';
        echo "
        <div class='task $checked' data-id='$id'>
            <div class='content'>
                <input type='checkbox' $checked id='taskCheck$id' />
                <label for='taskCheck$id'>$value</label>
                <button class='addChild'>+</button>
                <button class='delete'>X</button>
            </div>
        </div>
        ";
    }
}

?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>TO DO List</title>
    <link rel="stylesheet" href="css/baseColor.css" />
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/index.css" />
    <script src="js/index.js" defer></script>
  </head>
  <body>

    <div class="container">
        <div class="scroll">
            <div class="yesterday">
                <h1>Yesterday</h1>
                <div class="content">
                    <?php
                        displayTasks($yTasks);
                    ?>
                    <form action="" method="post">
                        <input type="text" placeholder="Enter a new task..." name="new_task"/>
                    </form>
                </div>

            </div>
            <div class="today">
       
                <h1>Today</h1>
                <div class="content">
                    <?php
                        displayTasks($todTasks);
                    ?>
                    <form action="" method="post">
                        <input type="text" placeholder="Enter a new task..." name="new_task"/>
                    </form>
                </div>
            </div>
            <div class="tomorow">
                <h1>Tomorow</h1>
                <div class="content">
                    <?php
                        displayTasks($tomTasks);
                    ?>
                    <form action="" method="post">
                        <input type="text" placeholder="Enter a new task..." name="new_task"/>
                    </form>
                </div>
            </div>
        </div>

    </div>
  </body>
</html>

Merci d'avance pour votre aide je ne comprend vraiment pas d'où viens le problème
Windows / Chrome 106.0.0.0

3 réponses

jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
26 oct. 2022 à 17:48

Bonjour,

$yTasks n'est pas la même chose que $ytasks

0

Oh merci je n'avais pas vu 

Sinon j'ai un autre problème,

quand j'exécute ce code:

$yTasks = array_filter($allTasks,function($e){
    return $e["date"] > $beforeYesterdayTimestamp AND $e["date"] < $todayTimestamp;
});

j'ai ces erreurs:

Warning: Undefined variable $beforeYesterdayTimestamp in D:\Xammp\htdocs\my_php_projects\todo_list\index.php on line 62

Warning: Undefined variable $todayTimestamp in D:\Xammp\htdocs\my_php_projects\todo_list\index.php on line 62

alors que j'ai défini ces variables avant comme ça :

$date=DateTime::createFromFormat('d-m-Y H:i:s', date('d-m-Y').' 00:00:00')->getTimestamp();

$beforeYesterdayTimestamp=$date-(3600*24*2);
$yesterdayTimestamp=$date-(3600*24);
$todayTimestamp=$date;
$tomorowTimestamp=$date+(3600*24);

Savez vous d'où viens le problème ?

0
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
26 oct. 2022 à 18:12
$yTasks = array_filter($allTasks,function($e){
  global $beforeYesterdayTimestamp ;
  global $todayTimestamp;
  return $e["date"] > $beforeYesterdayTimestamp AND $e["date"] < $todayTimestamp;
});
0

Merci beaucoup pour votre aide!

0