0

I have a problem where I use this foreach code. I receive an undefined variable: task error.

<?
  $data = file_get_contents('data.json');
  $array = json_decode($data, 1);
  foreach ($array as $task) { ?>
    <tr>
      <td>
        <?= $task['name'] ?>
      </td>
      <td>Data</td>
      <td>Data</td>
      <td>Data</td>
      <td><button class="btn btn-primary"><?= icon('stop'); ?></button></td>
      <td><button class="btn btn-danger"><?= icon('times'); ?></button></td>
    </tr>
  <? } 
?>
3
  • 6
    What does this have to do with JS/jQuery? Commented Feb 20, 2017 at 11:55
  • use the classical arsenal to see what happens, ini_set('display_errors', 'On'), var_dump, print_r, json_last_error. Commented Feb 20, 2017 at 12:05
  • I suspect you don't have short_tags enabled in your php.ini. Commented Feb 20, 2017 at 12:10

1 Answer 1

1

You forget to add the start of php file <?php and at the end <?php } ?>, which will result in different errors

<?php
$data = file_get_contents('data.json');
$array = json_decode($data , 1);
foreach ($array as $task) { ?>
    <tr>
        <td>
            <?= $task['name']; ?>
        </td>
        <td>Data</td>
        <td>Data</td>
        <td>Data</td>
        <td><button class="btn btn-primary"><?= icon('stop'); ?></button></td>
        <td><button class="btn btn-danger"><?= icon('times'); ?></button></td>
    </tr>
<?php }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Why should he try this? The answer should explain what was wrong in the original code and how you fixed it.

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.