0

I'm decoding JSON format, my API Endpoint is https://api.reliableserver.host/api/recent_activities (No Auth)

I've already managed to display the values of the first item like this:

<?php
$url2 = "https://api.reliableserver.host/api/recent_activities";
$obj2 = json_decode(file_get_contents($url2), true);
?>

<tr class="css-xlbdcw">

    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: left;">
        <div class="vx_text-body-md" style="color: rgb(44, 46, 47); padding-top: 7px; padding-bottom: 7px;">
            <div role="button">
                <?php echo $obj2['data'][0]['activity_date']; ?>
            </div>
        </div>
    </td>

    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: left;">
    <div data-testid="verticalList">
        <a tabindex="0" href="/activity/payment/87J62844XL761054H"><?php echo $obj2['data'][0]['activity_title']; ?></a>
        <div style="color: rgb(44, 46, 47); font-size: 11px;"><?php echo $obj2['data'][0]['activity_status']; ?></div>
    </div>
    </td>

    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: right;">
        <div class="vx_text-body-md" style="color: rgb(44, 46, 47); padding-top: 7px; padding-bottom: 7px;">
            <div role="button"><?php echo $obj2['data'][0]['activity_amount']; ?></div>
        </div>
    </td>

</tr>

This is working for the first item, how do i make it go through each item using foreach - I tried but couldn't get it working.

Thanks

2 Answers 2

1

Iterate over $obj2['data']:

$url2 = "https://api.reliableserver.host/api/recent_activities";
$obj2 = json_decode(file_get_contents($url2), true);

foreach ($obj2['data'] as $item) {?>
<tr class="css-xlbdcw">

    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: left;">
        <div class="vx_text-body-md" style="color: rgb(44, 46, 47); padding-top: 7px; padding-bottom: 7px;">
            <div role="button">
                <?php echo $item['activity_date']; ?>
            </div>
        </div>
    </td>

    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: left;">
        <div data-testid="verticalList">
            <a tabindex="0" href="/activity/payment/87J62844XL761054H"><?php echo $item['activity_title']; ?></a>
            <div style="color: rgb(44, 46, 47); font-size: 11px;"><?php echo $item['activity_status']; ?></div>
        </div>
    </td>

    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: right;">
        <div class="vx_text-body-md" style="color: rgb(44, 46, 47); padding-top: 7px; padding-bottom: 7px;">
            <div role="button"><?php echo $item['activity_amount']; ?></div>
        </div>
    </td>
</tr>
<?php
}
Sign up to request clarification or add additional context in comments.

2 Comments

superb, its working - ill accept the answer in 9 minutes as the system won't allow me now
Ths answer is more accurate than the other one
1

If you wanted to allow yourself flexibility in rendering items, you could use a filter array ($filter), as follows:

<?php
$response = file_get_contents('https://api.reliableserver.host/api/recent_activities');
$response = json_decode($response, true);
$filter = ['id', 'activity_amount']; // select which items to show
foreach ($response['data'] as $record) {
    echo '<tr class="css-xlbdcw">'; // open row
    foreach ($record as $key => $item) {
        if (in_array($key, $filter)) { // show filtered items only
            echo <<<HEREDOC
    <td style="width: unset; max-width: unset; padding: 5px 20px; text-align: left;">
        <div class="vx_text-body-md" style="color: rgb(44, 46, 47); padding-top: 7px; padding-bottom: 7px;">
            <div role="button">
                $item
            </div>
        </div>
    </td>
HEREDOC;
        }
    }
    echo '</tr>'; // close row
}

We use HEREDOC as it allows us to elegantly render HTML with variable(s) (e.g. $item) injected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.