我正在尝试使用 PHP 变量从 XML 文件中加载某些元素,因此如果加载了 index.php?id=1,它将从 $projects->project[1] 中提取信息。以下代码中没有出现错误,但没有显示任何内容。任何帮助表示赞赏:)
<?php
$projects = simplexml_load_file('portfolio.xml');
$id = $_GET["id"];
echo $projects->project[$id]->title;
?>
它可能是两件事之一(或两者兼而有之):
也就是说,尝试使用 print_r 或 var_dump 来获取数据。
print_r($_GET); 死; 检查你是否在 get 中获取数据。
error_reporting(~0); // show me everything i do wrong
$projects = simplexml_load_file('portfolio.xml');
var_dump($projects); // make sure $projects actually has something
var_dump($_GET['id']); // are you getting the request data?
$id = (int) $_GET["id"]; // cast to int for good measure
// i'm assuming it's numeric, don't if its not
echo $projects->project[$id]->title; // still not showing anything?
var_dump($projects->project[$id]->title); // dump it for good measure