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.
1 Answer 1
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.
$_SESSIONlooks like a valid option for that.