-1

I have table of invoices where each invoice has multiple expense row with different values. I store invoices and expenses in seperate db tables. I display all the invoices inside table on main page and I allow user to check checkboxes and each time they check it, correlated row of invoice gets pusshed into the array as an object. So I get somethnig like this

[{
    "id": 587,
    "name": "Ornek",
    "cariKodu": "129896",
    "adres": "HACI HALIL MAH. KIZILAY CAD. NO:29/B\nGEBZE / KOCAELI",
    "il": "ILYASBEY",
    "ilce": "Gebze"
}, {
    "id": 589,
    "name": "Ornek_1",
    "cariKodu": "34324",
    "adres": "Ataşehir Konakları",
    "il": "İstanbul",
    "ilce": "Araşehir"
}]

What I want is to make an array like this with expenses and push it into the array above. But I list invoices and expenses in two different pages so I don't know how to send data between those pages. I want to have one super array where each index of it is an object and inside that object I is another array of objects.

I know it's little complicated but I tried my best to explain it.

mitkosoft
  • 5,262
  • 1
  • 13
  • 31
mhajiyev
  • 25
  • 3
  • So you want to store a value, so that it “survives” navigation between different pages. Then you should either store it client-side in local- or sessionStorage, or server-side, in the session. (Which one might be more appropriate, depends on specifics - which this question so far doesn’t really provide.) – 04FS Jan 27 '20 at 13:53
  • `$_SESSION` looks like a valid option for that. – mitkosoft Jan 27 '20 at 13:55
  • @04FS Even if I store it in session variable, can I get the values from server side to client? And is it the effective way to do it? – mhajiyev Jan 27 '20 at 13:55

1 Answers1

0

You can simply "pass" the JSON via pages by using $_SESSION both as server and client-side variable:

page1.php (sender)

<?php
    session_start();
    $json = '[{
        "id": 587,
        "name": "Ornek",
        "cariKodu": "129896",
        "adres": "HACI HALIL MAH. KIZILAY CAD. NO:29/B\nGEBZE / KOCAELI",
        "il": "ILYASBEY",
        "ilce": "Gebze"
    }, {
        "id": 589,
        "name": "Ornek_1",
        "cariKodu": "34324",
        "adres": "Ataşehir Konakları",
        "il": "İstanbul",
        "ilce": "Araşehir"
    }]';

    //convert JSON object for further use if needed
    $phpArray = json_decode($json, true);

    //save the JSON as SESSION var
    $_SESSION['json_object'] = $json;
?>

page2.php (consumer)

<?php
    session_start();

    //get the JSON object from the session and convert it to array for further use
    $phpArray = json_decode($_SESSION['json_object'], true);
?>
<script>
    //push JSON object into your JS
    var jsonObject = <?php echo $_SESSION['json_object']; ?>;
    console.log(jsonObject);
</script>

Of course, you have to implement checks for this $_SESSION existence in your script in order to prevent any errors, this gives you just the basic idea.

mitkosoft
  • 5,262
  • 1
  • 13
  • 31