I need some help converting the response of an XHR GET to a useable array (preferrably 2 arrays, but not strictly needed). I'm trying to get some data fra sql but I can't figure out how to decode/parse the response from my GET request into any form of useable array.
I'm sorry if this is a stupid question, but I'm new and trying to learn. I've tried both json_decode() in the php bit and JSON.parse() in the JS bit, but neither of those work (again, sorry, It's probably obvious why but this is tbh abit beyond my current capabilities)
Code snippets follow below:
GET response is a string and looks like example below (array.length of first section will always be static. array.length of second array will vary from 1->50ish)
array(1) {
[0]=>
array(10) {
["id"]=>
string(1) "1"
[0]=>
string(1) "1"
["project_name"]=>
string(10) "Test"
[1]=>
string(10) "Test"
["project_description"]=>
string(7) "new test"
[2]=>
string(7) "new test"
["created_by"]=>
string(14) "Testuser"
[3]=>
string(14) "Testuser"
["date"]=>
string(10) "27.05.2021"
[4]=>
string(10) "27.05.2021"
}
}
array(4) {
[0]=>
object(stdClass)#2 (5) {
["cell1"]=>
string(2) "K1"
["cell2"]=>
string(1) "3"
["cell3"]=>
string(5) "200.0"
["cell4"]=>
string(2) "12"
["cell5"]=>
string(4) "2400"
}
[1]=>
object(stdClass)#4 (5) {
["cell1"]=>
string(2) "K2"
["cell2"]=>
string(1) "8"
["cell3"]=>
string(5) "705.0"
["cell4"]=>
string(2) "21"
["cell5"]=>
string(5) "14805"
}
[2]=>
object(stdClass)#5 (5) {
["cell1"]=>
string(2) "K3"
["cell2"]=>
string(2) "15"
["cell3"]=>
string(5) "898.0"
["cell4"]=>
string(2) "31"
["cell5"]=>
string(5) "27838"
}
[3]=>
object(stdClass)#6 (5) {
["cell1"]=>
string(2) "F1"
["cell2"]=>
string(2) "19"
["cell3"]=>
string(5) "338.0"
["cell4"]=>
string(2) "24"
["cell5"]=>
string(4) "8112"
}
}
GET Request:
function loadData() {
let str = document.getElementById('loadSelected').value;
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let tempinfo = this.responseText;
console.log(tempinfo);
}
};
xhttp.open("GET", "includes/loaddata.php?q="+str, true);
xhttp.send();
}
and "loaddata.php"
<?php
$db = new PDO("sqlite:toolkit.db3");
$stmt = $db->prepare('SELECT * FROM projects WHERE id = :id');
$stmt->bindValue(':id', $_GET['q'], SQLITE3_INTEGER);
$stmt->execute();
$infoholder = $stmt->fetchAll();
$stmt = $db->prepare('SELECT * FROM project_data WHERE parent_id = :id');
$stmt->bindValue(':id', $_GET['q'], SQLITE3_INTEGER);
$stmt->execute();
$dataholder = $stmt->fetchAll();
$dataholder = json_decode($dataholder['0']['dataholder']);
var_dump($infoholder);
var_dump($dataholder);
?>