我已将Json数据存储在我的主本地文件夹中,并且如果我单击查看用户数据href =“ view.php?id = 1”自动在其中显示用户ID = 1 ,我想将用户数据显示到另一个页面“ view.php”中view.php文件。
data.json
{
"UserData": [
{
"id": "1",
"fname": "Joey",
"lname": "Tulang",
"age": "29",
"gender": "Male"
},
{
"id": "2",
"fname": "Angelica",
"lname": "Bacle",
"age": "20",
"gender": "Female"
},
{
"id": "3",
"fname": "Mj",
"lname": "King",
"age": "21",
"gender": "male"
}
]
}
index.php
$jsondata = file_get_contents("data.json");
$json = json_decode($jsondata, true);
$output = "<ul>";
foreach ( $json['UserData'] as $display ) {
$output .="<li>" .$display['fname']. " <a href='view.php?".$display['id']."'>View User Data</a></li> </br>";
}
$output .= "</ul>";
echo $output;
输出:
view.php
<!DOCTYPE html>
<html>
<head>
<title>View User Data</title>
</head>
<body>
<!-- Display User Data Here -->
<p>First Name: </p>
<p>Last Name: </p>
<p>Age: </p>
<p>Gender: </p>
</body>
</html>
index.php
您忘记了id
参数,只是设置了数字。
$jsondata = file_get_contents("data.json");
$json = json_decode($jsondata, true);
$output = "<ul>";
foreach ( $json['UserData'] as $display ) {
$output .="<li>" .$display['fname']. " <a href='view.php?id=".$display['id']."'>View User Data</a></li> </br>";
}
$output .= "</ul>";
echo $output;
view.php
使用$_GET['id']
查询您的JSON:
<?php
$jsondata = file_get_contents("data.json");
$json = json_decode($jsondata, true);
$key = array_search((int) $_GET['id'], array_column($json['UserData'], 'id'));
// if $key false, was not found, presume it was...
$user = $json['UserData'][$key];
?>
<!DOCTYPE html>
<html>
<head>
<title>View User Data</title>
</head>
<body>
<!-- Display User Data Here -->
<p>First Name: <?= $user['fname'] ?></p>
<p>Last Name: <?= $user['lname'] ?></p>
<p>Age: <?= $user['age'] ?></p>
<p>Gender: <?= $user['gender'] ?></p>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句