How to open an item from an API list in a new page with PHP
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!
You can add a view button in the last column of your table, or an anchor tag to your id/name, and onclick of that, using target="_blank" property you can redirect it to new tab
– gauri
Sep 3 at 7:50
If the issue is in
api-item.php
please show that code not the index– Lawrence Cherone
Sep 3 at 7:56
api-item.php
1 Answer
1
(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;
Thanks! I can make it open on another page. What I need is to see only the options of the ID selected on the new page.
– Goulart
Sep 3 at 7:56
Did you try anything to display data in the new page? I can't write an entire framework on your behalf :)
– Alberto
Sep 3 at 8:14
Yes, I tried a few things, but nothing really worked. So I'm here, trying to get some help for the page //api-item.php
– Goulart
Sep 3 at 8:28
You should update your question with the code you tried. Or almost with the code you use to populate the $result array.
– Alberto
Sep 3 at 8:31
Now it works flawless! Thank you very much!
– Goulart
Sep 3 at 9:52
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What's the link to the new page? Add somewhere a link with target='_blank'...
– Alberto
Sep 3 at 7:45