0

I'm trying to get data into the tabular format by invoking JSON data into PHP code.

JSON code written to use for converting into tabular format.

[

  {
   "#":"3586 "
   "Project" :"SSMT",
   "Tracker" :"Maintenance",
   "Status" : "To Do"
   "Subject" : "Test the following scenario and provide the relevant scripts in centos7"
   "Author" : "Anil K"
   "Assignee" : "SSMT Group"
  },
  {
   "#" :"3517"
   "Project" : "SSMT"
   "Tracker" : "Improvement"
   "Status" : "In Progress"
   "Subject" : "Image server daily backup"
   "Author" : "Lakshmi G"
   "Assignee" : "Pooja S"
  },
  {
   "Project" : "SSMT"
   "Tracker" : "Improvement"
   "Status" : "In Progress"
   "Subject" : "setup openstack all-in-one in centos7 instance on ORAVM1 box."
   "Author" : "Anil K"
   "Assignee" : "Bhanuprakash P"
  }
]

The below php code is written to fetch the above JSON data.

<!DOCTYPE html>
<html lang = "en-US">

<?php

$url = 'data.json';
$data = file_get_contents($url);
$characters = json_decode($data);

echo $characters[0]->name;

foreach ($characters as $character) {
         echo $character->name . '<br>';

}
?>

 <table>
        <tbody>
                <tr>
                        <th>#</th>
                        <th>Project</th>
                        <th>Tracker</th>
                        <th>Status</th>
                        <th>Subject</th>
                        <th>Author</th>
                        <th>Assignee</th>

                </tr>
                        <?php
                        foreach ($characters as $character) {
                                echo '<tr>'
                                echo '<td>' . $character-># . '</td>';
                                echo '<td>' . $character->project . '</td>';
                                echo '<td>' . $character->tracker . '</td>';
                                echo '<td>' . $character->status . '</td>';
                                echo '<td>' . $character->subject . '</td>';
                                echo '<td>' . $character->author . '</td>';
                                echo '<td>' . $character->assignee . '</td>';
                                echo '</tr>';
                        }
                        ?>
        </tbody>

 </table>

</html>

I received following error PHP Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /var/www/html/tabularformat.php on line 34

can anyone suggest othis? ?

after adding ; at the end of each 'echo' statement. also getting the same error. please find it.

1 Answers1

0
  • I recommend using true as the second parameter to form array elements.
  • Your json is invalid (I fixed it in my code)
  • You need to access the keys case-sensitively.
  • Check that an element exists before trying to access it.

Code: (Demo)

$json='[
  {
   "#":"3586 ",
   "Project" :"SSMT",
   "Tracker" :"Maintenance",
   "Status" : "To Do",
   "Subject" : "Test the following scenario and provide the relevant scripts in centos7",
   "Author" : "Anil K",
   "Assignee" : "SSMT Group"
  },
  {
   "#" :"3517",
   "Project" : "SSMT",
   "Tracker" : "Improvement",
   "Status" : "In Progress",
   "Subject" : "Image server daily backup",
   "Author" : "Lakshmi G",
   "Assignee" : "Pooja S"
  },
  {
   "Project" : "SSMT",
   "Tracker" : "Improvement",
   "Status" : "In Progress",
   "Subject" : "setup openstack all-in-one in centos7 instance on ORAVM1 box.",
   "Author" : "Anil K",
   "Assignee" : "Bhanuprakash P"
  }
]';

$characters = json_decode($json,true);
foreach ($characters as $character) {
    echo '<tr>';
        echo '<td>',(isset($character['#'])?$character['#']:''),'</td>';
        echo '<td>',(isset($character['Project'])?$character['Project']:''),'</td>';
        echo '<td>',(isset($character['Tracker'])?$character['Tracker']:''),'</td>';
        echo '<td>',(isset($character['Status'])?$character['Status']:''),'</td>';
        echo '<td>',(isset($character['Subject'])?$character['Subject']:''),'</td>';
        echo '<td>',(isset($character['Author'])?$character['Author']:''),'</td>';
        echo '<td>',(isset($character['Assignee'])?$character['Assignee']:''),'</td>';
    echo '</tr>';
}

Output:

<tr>
    <td>3586 </td>
    <td>SSMT</td>
    <td>Maintenance</td>
    <td>To Do</td>
    <td>Test the following scenario and provide the relevant scripts in centos7</td>
    <td>Anil K</td>
    <td>SSMT Group</td>
</tr>
<tr>
    <td>3517</td>
    <td>SSMT</td>
    <td>Improvement</td>
    <td>In Progress</td>
    <td>Image server daily backup</td>
    <td>Lakshmi G</td>
    <td>Pooja S</td>
</tr>
<tr>
    <td></td>
    <td>SSMT</td>
    <td>Improvement</td>
    <td>In Progress</td>
    <td>setup openstack all-in-one in centos7 instance on ORAVM1 box.</td>
    <td>Anil K</td>
    <td>Bhanuprakash P</td>
</tr>

Otherwise, you can use "encapsulation" to access the # object.

$characters = json_decode($json);
foreach ($characters as $character) {
    echo '<tr>';
        echo '<td>',(isset($character->{'#'})?$character->{'#'}:''),'</td>';
        // ...

From the manual:

Example #2 Accessing invalid object properties

Accessing elements within an object that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
Community
  • 1
  • 1
mickmackusa
  • 43,625
  • 12
  • 83
  • 136