Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How can I create an HTML table from a JSON file that includes a working link from a URL input in an HTML form?

The code below is what I've tried but I can't get it to display an actual html table and have the form be on a different page.
PHP:
<?php
$json = file_get_contents('data.json');
$data = json_decode($json, true);
$url = $_POST['url'];
echo '<table>';
echo '<thead><tr><th>Name</th><th>Link</th></tr></thead>';
echo '<tbody>';
foreach ($data as $item) {
    echo '<tr>';
    echo '<td>' . $item['name'] . '</td>';
    echo '<td><a href="' . $url . $item['link'] . '">' . $item['link'] . '</a></td>';
    echo '</tr>';
}
echo '</tbody>';
echo '</table>';
?>
 
Well, I searched about it on internet and I found that there are some problem with your code.
You can replace your code with below one.

Code:
<?php
$json = file_get_contents('data.json');
$data = json_decode($json, true);
$url = $_POST['url'];
?>


<!DOCTYPE html>
<html>
<head>
    <title>HTML Table</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Link</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($data as $item): ?>
            <tr>
                <td><?php echo $item['name']; ?></td>
                <td><a href="<?php echo $url . $item['link']; ?>"><?php echo $item['link']; ?></a></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</body>
</html>


Thanks
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom