-1

I get data array make me confuse like this:

{  
   "error":0,
   "productcount":"1",
   "page":1,
   "totalpage":1,
   "result":[sorry hidden cust request]
}

I want to convert it to html table, how ? please help me...

Hengky ST
  • 185
  • 1
  • 2
  • 15
  • Thanks Mr @Jordan S – Hengky ST May 19 '17 at 16:37
  • Please be more precise concerning what you want to display and how. From your tags we can assume you are using php. So far so good: now you want to display the "stok" param or your whole "result" array, which would result in a table in a table ;) – ju_ May 19 '17 at 16:45
  • Like this : https://storage.googleapis.com/openscreenshot/-/e/3/SkWSWj3e-.png – Hengky ST May 19 '17 at 16:54

1 Answers1

2

Just iterate over your result like so, note that the data you show is a JSON object, which you need to convert to an array first:

<?php

// this has to be in JSON format
$dataArray = json_decode($yourJSON);

$dataArray = $dataArray["result"][0]["stok"];
?>
<table>
  <tr>

    <td><strong>Ukuran</strong></td>
    <?php foreach($dataArray as $item){?>
      <td> <?= $item["ukuran"] ?> </td>
    <?php } ?>

  </tr>

  <tr>
    <td><strong>Stok</strong></td>
    <?php foreach(dataArray  as $item){?>
      <td> <?= $item["stok"] ?> </td>
    <?php } ?>

  </tr>
</table>
ju_
  • 569
  • 1
  • 4
  • 17
  • Thanks Mr @ju_ for helping me, i got this error "Fatal error: Cannot use object of type stdClass as array" what this mean ? – Hengky ST May 19 '17 at 17:17
  • have a look at http://stackoverflow.com/questions/18576762/php-stdclass-to-array so you first convert your std class into an array – ju_ May 19 '17 at 17:18
  • Ok, thanks Mr ju, i have tried, good but have another error Warning: Invalid argument supplied for foreach(), i try used is_array() but return null – Hengky ST May 19 '17 at 17:26
  • try to var_dump($your_data); – ju_ May 19 '17 at 17:28
  • return NULL, sorry if i to much ask – Hengky ST May 19 '17 at 17:30
  • before that i used $dataArray = json_decode(json_encode($myJson),true); is this true ? – Hengky ST May 19 '17 at 17:31
  • no, as your data is not a JSON but a apparently a dump of an stdclass its not correct. just follow the instructions in the link i commented earlier – ju_ May 19 '17 at 17:35
  • To assist you further, please post a working phpFiddle of your code. – ju_ May 19 '17 at 17:57
  • for your code just remove the json_encode() and change $dataArray = $dataArray["result"]["stok"] to $dataArray = $dataArray["result"][0]["stok"]; – ju_ May 19 '17 at 18:57
  • Thank you very much Mr ju, its work fine... maybe i have learn from you a master PHP :D – Hengky ST May 19 '17 at 19:09
  • My pleasure. Keep going ;) – ju_ May 19 '17 at 19:10