How to display json stored in a session?

How to display json stored in a session?

lyassalyassa Posts: 3Questions: 0Answers: 0
edited September 2010 in General
Thanks in advance for your help ...

I have the following json stored in $_SESSION['data']:
[{"Title":"title1", "Value":"valueA"},
{"Title":"title2", "Value":"valueB"},
{"Title":"title3", "Value":"valueC"}
]

How can I display this in a table with two columns Title and Value?

I thought I can do the following, but didn't work:

$(document).ready(function() {
$('#example').dataTable( {
"aData": <?php echo $_SESSION['data']; ?>
} );
} );

Here is how I have the html:




Title


Value









Title


Value

Replies

  • lyassalyassa Posts: 3Questions: 0Answers: 0
    Well ... I ended up writing a small function that converts json into JS array and then used the JS array as shown in the example. Here is the function:

    function toJSArray($json) {
    $a = json_decode($json);
    $jsArr = "[";
    $cma = "";
    foreach ($a as $i) {
    $jsArr .= "{$cma}["; //<
    $cma = "";
    foreach ($i as $k=>$v) {
    $jsArr .= "{$cma}\"$v\"";
    $cma = ",";
    }
    $jsArr .= "]";
    $cma = ","; //<
    }
    $jsArr .= "]";
    return $jsArr;
    }
This discussion has been closed.