简体   繁体   English

从api调用ID时未定义的变量

[英]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. 我正在执行老师给我们的PHP项目,基本上,我们必须使网站动态并创建一个liste.php,其中的任务存储在老师拥有的API中。

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 ). 所以我有了index.php,上面有首页,还有所有正在进行的项目的清单(如旅行准备,购物清单和公寓装修)。 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 ). 我创建了一个liste.php,其中包含了每个项目中的所有任务(买票,预订酒店和购买明信片以准备旅行)。

Now I have to get all the tasks of each project from the API he gave us. 现在,我必须从他提供给我们的API中获取每个项目的所有任务。

Here's the API 这是API

http://todo_api.xx.firstname-lastname.com/tache.php?liste_id=1 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 ) 这是第1个项目的API,共有3个项目(我在上面提到了所有项目)

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. 我也有一个function.php和$ api_url存储,还有2个函数,一个获取项目列表,另一个获取每个项目的任务。

$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 : 但是当我尝试print_r,甚至只是显示每个任务时,我得到了:

Notice: Undefined index: liste_id in /Applications/MAMP/htdocs/todo/database/functions.php on line 17 注意:未定义的索引:第17行/Applications/MAMP/htdocs/todo/database/functions.php中的liste_id

So obviously something is wrong, and this keeps me from going on. 所以很明显,出了点问题,这使我无法继续前进。 Any idea ? 任何想法 ?

Edit : here's my html/php code regarding the tasklist 编辑:这是我关于任务列表的html / php代码

$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. libelle是所需信息的索引。

Thanks a lot ! 非常感谢 !

Your code in functions.php 您在functions.php中的代码

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

but html/php code 但是html / php代码

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

i guess if you pass the parameter to the function it will work. 我想如果您将参数传递给函数,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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