-1

This is my php file from where i want to get json file data and show it in the table form in php.I am getting error on browser while getting values of expense table from json file ?

The error i am getting is : Notice: Undefined index: amounts in C:\xampp\htdocs\jsontophptable\table.php on line 20

   <!DOCTYPE html>
    <html>
    <body>
    <?php

    $url = 'accountbook.json'; // path to your JSON file
    $data = file_get_contents($url); // put the contents of the file into a variable
    $characters = json_decode($data,true); // decode the JSON feed

    ?>
    <table>
        <tbody>
            <tr>
                <th>Amount </th>
                <th>Detail</th>
                <th>Email</th>
            </tr>
            <?php foreach ($characters as $character) : ?>
            <tr>
                <td> <?php echo $character['amounts']."\n" ?> </td>
                <td> <?php echo $character['details']."\n"; ?> </td>
                <td> <?php echo $character['email']."\n;"?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    </body>
    </html>

This is my json file from where i want to get nested data: I want to get expense table data in json as seperate table in php and sales table seperate.How to do that?

 {
  "Expense_Details_DataBase" : {
    "24-10-2018--02:15:46 PM" : {
      "amounts" : "36",
      "details" : "tree",
      "email" : "a@gmail.com"
    }
  },
  "Sales_Details_DataBase" : {
    "24-10-2018--03:23:08 PM" : {
      "email" : "b@hotmail.com",
      "sale" : 1040
    },
    "24-10-2018--03:23:58 PM" : {
      "email" : "b@hotmail.com",
      "sale" : 5010
    },
    "24-10-2018--12:41:30 PM" : {
      "email" : "a@gmail.com",
      "sale" : 120
    }
  }
}

This is my new php file after editing: The table after this edit is not showing any data it is empty. how to fix that? .And also I want to get date and time also in table how to do that?

<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
    border: 1px solid black;
}
table {
    border-collapse: collapse; width: 30%;
    text-align: center;
}

th {
    background-color: #4CAF86;
    color: white;
    height: 70px;
    text-align: center;
    padding: 6px;
}
td{
    padding: 6px;
    border-bottom: 1px solid #ddd;
}
tr:hover {background-color: #f5f5f5;}
</style>
</head>
<body>
<?php

$url = 'accountbook.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data,true); // decode the JSON feed

?>
<table>
<table align="center">
    <tbody>
        <tr>
            <th>Amount </th>
            <th>Detail</th>
            <th>Email</th>
        </tr>
        <?php
      if(isset($characters['Expense_Details_DataBase'])){
      foreach ($characters['Expense_Details_DataBase'] as $character) : 
      ?>

        <tr>

            <td> <?php echo $character['amounts']."\n" ?> </td>
            <td> <?php echo $character['details']."\n"; ?> </td>
            <td> <?php echo $character['email']."\n;"?></td>
            <?php print_r($character); ?>
        </tr>
        <?php endforeach;
          }  ?>
    </tbody>
</table>
</body>
</html> 
  • 1
    `print_r($character)` and see what you actual have. The error indicates the index doesn't exist. I'm surprised there aren't `details` and `email` notices as well. – user3783243 Nov 07 '18 at 05:38
  • Please read my comment and address any points you have questions about. Just commenting `please correct my code` multiple times isn't going to help your question – user3783243 Nov 07 '18 at 05:41
  • I am getting the array of json file after implementing your suggestionArray ( [24-10-2018--02:15:46 PM] => Array ( [amounts] => 36 [details] => tree [email] => a@gmail.com ) ) Array ( [24-10-2018--03:23:08 PM] => Array ( [email] => b@hotmail.com [sale] => 1040 ) [24-10-2018--03:23:58 PM] => Array ( [email] => b@hotmail.com [sale] => 5010 ) [24-10-2018--12:41:30 PM] => Array ( [email] => a@gmail.com [sale] => 120 ) ) – admin account Nov 07 '18 at 05:46
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Nov 07 '18 at 06:06
  • That's a garbage data format. Date and time as an object key? – miken32 Nov 07 '18 at 06:08

1 Answers1

0

Try this one.

 <!DOCTYPE html>
<html>
<body>
<?php

$url = 'accountbook.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data,true); // decode the JSON feed

?>
<table>
    <tbody>
        <tr>
            <th>Amount </th>
            <th>Detail</th>
            <th>Email</th>
        </tr>
        <?php
      if(isset($characters['Expense_Details_DataBase'])){
      foreach ($characters['Expense_Details_DataBase'] as $character) : 
      ?>

        <tr>
            <td> <?php echo $character['amounts']."\n" ?> </td>
            <td> <?php echo $character['details']."\n"; ?> </td>
            <td> <?php echo $character['email']."\n;"?></td>
        </tr>
        <?php endforeach;
          }  ?>
    </tbody>
</table>
</body>
</html>
j3thamz
  • 66
  • 4