0

Have struggled with this problem now few hours and got nothing which works..

I have a files called categories.json and index.php. In index.php i trying to get specified data from categories.json. I can get all data from there with this:

if( isset($_GET['categories_json']) ){ // TÄLLÄ SAA KAIKKI TIEDOT
    //json file as is
    header("Content-Type: application/json; charset=UTF-8");
    $f = file_get_contents('categories.json');
    echo $f;
}

And I have this kind of products in categories.json:

{
    "categories": [
        {
            "categoryName": "Nappaimistot",
            "products": [
                {
                    "name": "Logitech K270",
                    "productid": 1,
                    "productpic_url": "https://cdn.verk.net/576/images/33/2_145225-700x423.jpeg",
                    "category": "Nappaimistot",
                    "price": 18.9,
                    "weight": 0.2
                },
                {
                    "name": "Logitech super keyboard",
                    "productid": 2,
                    "productpic_url": "https://cdn.verk.net/576/images/33/2_145225-700x423.jpeg",
                    "category": "Nappaimistot",
                    "price": 228.9,
                    "weight": 12
                }
            ]
        },
        {
        "categoryName": "Hiiret",
            "products": [
                {
                    "name": "Logitech MX Master 2S",
                    "productid": 6,
                    "productpic_url":"https://cdn.verk.net/960/images/91/2_404981-1640x2500.jpeg",
                    "category":"Hiiret",
                    "price": 99.90, 
                    "weight": 0.1
                }
            ]
        },
        {
            "categoryName": "Naytot",
                "products": [
                    {
                        "name": "Samsung C27JG50 27",
                        "productid": 11,
                        "productpic_url":"https://cdn.verk.net/960/images/91/2_404981-1640x2500.jpeg",
                        "category":"Naytot",
                        "price": 299.90, 
                        "weight": 11
                    }
                ]
            }
    ]

}

Example if i want all products from categoryName Nappaimistot, which price are over 20. I would preciate if you give me some glue. I got headache of this couple hours ago.

masteroscar
  • 39
  • 1
  • 6
  • How about using a database instead of serving static content (which is kind of self-defeating)? – Martin Zeitler Oct 06 '20 at 16:49
  • @Martin Zeitler Using database in this practice is not accepted. – masteroscar Oct 06 '20 at 16:51
  • Are you using json_decode for the data from categories.json ? – Carlos Deseda Oct 06 '20 at 16:55
  • 1
    The only option you'd have would be to `json_decode()` the file, manipulate the resulting `object` and then `json_encode()` it to `string` again. Admittedly, NoSQL databases work in a similar way but having to mess around alike this goes too far. It's not that it would be impossible, but nobody does it. If this is a practice, you'd at least now know what to do. – Martin Zeitler Oct 06 '20 at 16:57
  • @MartinZeitler Thanks for your advices of decoding / encoding. Forgot that. How can i echo something from specified category? Example from Nappaimistot. – masteroscar Oct 06 '20 at 17:25
  • Same way you get details from any PHP object or array. Once you've decoded it, that's what you're left with. See the duplicate question for examples. – ADyson Oct 06 '20 at 17:43

0 Answers0