I have a simple json file that is outputting correctly when viewed directly.
I want to call in this data though and output on a php page so i’ve written this
<?php
$jsonitem = file_get_contents("conundrum.json");
$objitems = json_decode($jsonitem);
//print_r($objitems);
//output links to all the images for this beach
$row_set = count($objitems);
//lets check how many it is finding
echo $row_set.'<br>';
//loop through and print out
for ($row = 0; $row < $row_set; $row++)
{
if($row !==''){
echo 'Tip title ';
echo $objitems['tip'];
echo '<br>';
}
}
?>
It is correctly outputting ‘2’ as the number of results and echo’ing 2 lots of tip title so it is looping through. so it must be the variable echo $objitems[‘tip’];
What am i doing wrong? I’ve tried appending the variable with an ID number but that didn’t work either.
Json looks like this
[
{
"id":1,
"tip":"Make up",
"date":"11th June 2017",
"user":"anon1",
"description":"Apart from not using any (which I do mostly), are there ANY ways to go plastic free with make up?! Like powder, mascara and foundation?",
"category":"In the bathroom"
},
{
"id":2,
"tip":"Toothpaste?",
"date":"2nd June 2017",
"user":"Anon",
"description":"Anyone got any clever toothpaste ideas? It all seems to be in plastic tubes these days!",
"category":"In the bathroom"
}
]
ok next question. Might be better as a separate thread not sure but i’ll post here for now.
So i want to add a sub-array for comments associated with each tip. I’ve successfully done this but now i want to be able to filter the top level tips. Again i can get this to work but with one bit not quite working.
If no GET vaiable is set i want it to loop through like this (which works)
if($row !=='' ){
if a GET variable is set i need this (which also works)
if ($row != '' && (isset($_GET['filter'])? $objitems[$row]['category'] == $_GET['filter'] : true)) {
conditional return this if true otherwise this
If I’ve got the brackets correct, that would check to see whether your $_GET variable is set, and if it is, it will return the value of the comparison between the $_GET variable and the object category field. If it is not set, it will return TRUE anyway.
(Note: there are probably much neater ways to do this - probably just splitting that part into a separate line inside your for loop to create a true or false variable would make it easier to read).
What I don’t understand is this check
if ($row != ''
because (unless you’ve changed the code and not mentioned it) that is inside a loop
for ($row = 0; $row < $row_set; $row++)
I can’t see a circumstance when $row will ever be an empty string.
oh yeah i think the ‘if ($row != ‘’’ was probably left over from a piece of code i was using before. Now you point it out it is obvious it’s not needed. doh.
I’ll give the top bit of code you’ve put a go. It’s looks like what i was trying to achieve, I’d tried something similar but couldn’t get it to work as my knowledge was lacking.
I’ve just got to nip out for an hour so will give it a go when i’m back.
brilliant all working. I made a few amends as when i set a form up to filter it i realised it could send an empty variable for the ‘please select’ option which would return no results, so i just changed it to
I don’t know if this matters, but I rather use the word keywords since they are simplier to work with. Sometimes I confuse && with “or” and vice versa. So I often do if(... OR ...) or else if(... AND ...)
Weirdly i tend to prefer it as && etc in if/else type queries. although having said that i prefer words in sql queries. No idea why probably just the way i learnt it at some point and i haven’t broken the habit.
thanks though it was worth mentioning just in case as others may find it easier too.
One thing you may want to keep in mind is operator precedence. Probably not an issue in most cases (unless there is a mix of both being used), but “&&” has precedence over “and”
that’s certainly worth knowing. I assumed they were one and the same. I tend to only use one type and stick to it in a query but it’s always that odd time when for some reason i do something different and can’t work out why it isn’t working.