我有一个在API中查找并在表中显示结果的代码。如下。
<?php
$apikey = "api_key=my_api_key";
$url = "http://api.link.com/property?" . implode("&", array($apikey));
$result = json_decode(file_get_contents($url, TRUE));
<table>
<thead>
<tr>
<th>ID</th>
<th>Type</th>
<th>Name</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result->properties as $row) {
?>
<tr>
<td><?php echo $row->id;?></td>
<td><?php echo $row->type;?></td>
<td><?php echo $row->name;?></td>
<td><a href="api-item.php?id=<?php echo $row->id; ?>"><?php echo $row->title; ?></a></td>
</tr>
<?php } ?>
</tbody>
</table>
我需要在新页面中打开每个项目,并在其中显示每个唯一ID的所有选项。
<?php //api-item.php
$post_id = $_GET['id'];
$apikey = "api_key=my_api&id=".$post_id;
$url = "http://api.link.com/property?" . implode("&", array($apikey));
$result = json_decode(file_get_contents($url, TRUE));
foreach ($result->properties as $row) {
?>
<strong><?php echo $row->title; ?></strong><br><br>
<strong>DESCRIPTION</strong><br>
<?php echo $row->description; ?>
<strong>CONSIDERATIONS</strong><br>
<?php echo $row->considerations; ?>
<?php } ?>
谢谢大家!问题解决了!
答案 0 :(得分:-1)
(用户已用代码部分编辑了自己的问题,以链接 $ row-> action td出现在此答案中。)
我想您正在使用商品ID来标识每个页面,因此您可以尝试以下代码:
<tr>
<td><a href="theItemPage.php?id=<?php echo $row->id;?>" target="_blank"><?php echo $row->id;?></a></td>
<td><?php echo $row->type;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->action;?></td>
</tr>
当然,您可以将标签移动到所需的任何位置(例如,如果您希望用户点击名称)。
鉴于您的修改,这是您应该重构的代码 使用:
<?php //api-item.php
$post_id = $_GET['id'];
$apikey = "api_key=my_api&id=".$post_id;
$url = "http://api.link.com/property?" . implode("&", array($apikey));
$result = json_decode(file_get_contents($url, TRUE));
// You don't need to re-assign $post_id, you already did it on the top of the page.
$output = "";
foreach ($result->properties as $row) {
$output .= "<strong>".$row->title."</strong><br><br>";
$output .= "<strong>DESCRIPTION</strong><br>".$row->description."<br><br>";
$output .= "<strong>CONSIDERATIONS</strong><br>$row->considerations;
}
echo $output;