简体   繁体   中英

Undefined variable when calling an id from api

I'm working on a PHP project the teacher gave us, basically we have to make a website dynamic and create a liste.php with tasks stored in an API the teacher has.

So I have the index.php with the front page on it and the list of all the projects going on ( like trip preparation, shopping list and appartment decoration ). I created a liste.php with all the tasks contained in each project ( buying tickets, booking the hotel and buying a postcard for the trip preparation ).

Now I have to get all the tasks of each project from the API he gave us.

Here's the API

http://todo_api.xx.firstname-lastname.com/tache.php?liste_id=1

That's the API for project n°1, there are 3 projects in total ( I mentionned them all above )

I also have a functions.php with the $api_url stored, and 2 functions, one getting the list of projects, the other getting the tasks for each project.

$api_url = "http://todo_api.xx.firstname-lastname.com/";


function getAllListes(): array {
    global $api_url;

    $json = file_get_contents($api_url . "liste.php");
    return json_decode($json, true);
}



function getAllTaches(): array {
    global $api_url;
    $json = file_get_contents($api_url . "tache.php?liste_id=" . $id);
    return json_decode($json, true);
}

But when I try to print_r that, or even just display each task, I get this :

Notice: Undefined index: liste_id in /Applications/MAMP/htdocs/todo/database/functions.php on line 17

So obviously something is wrong, and this keeps me from going on. Any idea ?

Edit : here's my html/php code regarding the tasklist

$listes = getAllListes();
$taches = getAllTaches($id);

<ul>

        <?php foreach ($taches as $tache) : ?>
        <li><?php echo $tache["libelle"] ;?></li>
        <?php endforeach ; ?>
    </ul>

libelle is the index of the information required.

Thanks a lot !

Your code in functions.php

function getAllTaches(): array { //declared function without parameter

but html/php code

$taches = getAllTaches($id);// calling the function with parameter.

i guess if you pass the parameter to the function it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM