0

I'm sending my mysql result to an associative array and then encoding with JSON

$showDisplayResult = $mysqlConn->query($getDisplayPage);
while($row=mysqli_fetch_assoc($showDisplayResult))
    {
        $rows[] = $row;
    }
$showDisplays = json_encode($rows);

ANd in my main page I'm using javascript to grab this, parse it and append the correct variables into my URL. The functionality seems to work but it only shows undefined as the variables in the URL.

Here's the javascript:

<script type="text/javascript">

let obj = <?php echo $showDisplays; ?>;

//obj = JSON.parse(obj);

let params = new URL(document.location).searchParams;
params.set("pageID", obj.pageID);
params.set("display", obj.display_id);
let url = window.location.href.split('?')[0];
let nextURL = url + "?" + params.toString();
window.setTimeout(function () {
    window.location.href = nextURL;
}, obj.duration * 1000);

console.log(obj);
</script>

So my URL is now showDisplay.php?display=undefined&pageID=undefined

How can I get this to parse the JSON correctly?

UPDATE:

If I echo $showDisplays, this is the JSON that prints:

[{"pageID":"104","page_type_id":"1","display_id":"3","slide_order":null,"duration":"56","active":"1","background_img":null,"panel_id":"96","panel_type_id":"1","page_id":"104","cont_id":"148","contID":"148","content":"\r\n\r\n\r\n<\/head>\r\n\r\nThis is full content<\/p>\r\n<\/body>\r\n<\/html>"},{"pageID":"116","page_type_id":"1","display_id":"3","slide_order":null,"duration":"54","active":"1","background_img":"images\/BG_spring.svg","panel_id":"113","panel_type_id":"1","page_id":"116","cont_id":"165","contID":"165","content":"\r\n\r\n\r\n<\/head>\r\n\r\nThis background should be green<\/p>\r\n<\/body>\r\n<\/html>"}]

So on page load i want the url to have display=3&pageID=104 then after 56 seconds (its duration) it should refresh and th URL should be display=3&pageID=116 for 54 seconds, and then keep the loop

11
  • Why is JSON.parse(obj) commented out? Commented Aug 2, 2018 at 18:14
  • please add obj. Commented Aug 2, 2018 at 18:14
  • @MarkMeyer when I comment this line out, the functionality at least is there but when it's left in at no longer appends anything to the URL and the console says it can't read property 'pageID' of null Commented Aug 2, 2018 at 18:16
  • 1
    Why are you looping and adding a row to an array if you are only expecting one pageID etc??? Commented Aug 2, 2018 at 18:17
  • @abracadaver I'm expecting more than one, with this particular displayID I have 3 pages assigned to it. I'm trying to build a JSON of all pages assiged to the display so that it can loop trhough and append each one to the URL on refresh. It's just cycling slides after a duration basically Commented Aug 2, 2018 at 18:18

1 Answer 1

1

If I read the code correctly, $showDisplays is an array of row objects, not a single row object. The JavaScript, however, tries to access properties of a row object as if they were on their containing array, obj.

If you want to access properties of the first row object in the array, you can do this with:

params.set("pageID", obj[0].pageID);
params.set("display", obj[0].display_id);

The next row object would be at the next index, obj[1]. So, in order to get all of the row objects in sequence, you would loop over them.

Sign up to request clarification or add additional context in comments.

5 Comments

Can you give me an example?
This is making a bit more sense, but can you view my update where I added the JSON currently being printed and my example in the case of that object
@TomN. I cleaned up my answer. For a better answer regarding what you are trying to do, I suggest you ask a new question, as it is beyond the scope of this one.
Thank you, I think that answers my question. Basically if I don't know how many rows there may be ill just make it a JS loop
@TomN. Exactly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.