简体   繁体   中英

How to open an item from an API list in a new page with PHP

I have a code that looks in an API and displays the results in a table. As follows.

<?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>

I need to open each of the items in a new page, where all the options for each unique id are displayed.

 <?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 } ?>

Thanks everyone! The problem was solved!

(User has edited his question with the portion of code to link the $row->action td present in this answer).

I guess you are using the item id to identify each page, so you can try the following code:

<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>

Of course you can move the tag anywhere you need (if you prefer users to click on the name, for example).

Given your edits, this is the code a little bit refactored you should use:

<?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;
  1. You don't need to re-declare $post_id inside the for loop, the ID value passed as parameter is only one per page hit.
  2. Be careful when you concatenate the $post_id to the api string on the top of the page, check the refactored code.

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