0

I have a JSON as shown below present in a file ptp-ess_landing.json.

ptp-ess_landing.json file:

{"toggle_multi_tiles":["1","2","3","4"]}

I have another php file (depth.php) in which I have some css codes.


Inside the depth.php, I have the following php code which makes depth.php and ptp-ess_landing.json to communicate with each other.

if (file_exists('feeds/ptp-ess_landing.json')) {
    $data = json_decode(file_get_contents('feeds/ptp-ess_landing.json'));
}

What I want to achieve in depth.php file is,

Case A, When JSON is this {"toggle_multi_tiles":["1","2","3","4"]}, following CSS should be called:

<style>
.hello-world {
    display:block;      
}
</style>

Case B, When JSON is this {"toggle_multi_tiles":["1","2","3"]}, following CSS should be called:

<style>
.hello-world {
    display:inline-block;       
}
</style>

This is what I have tried:

<style>
<?php if($data->toggle_multi_tiles["1","2","3","4"]) { ?>   // Line A
.hello-world {
    display:block;      
}
<?php } ?>
</style>

<style>
<?php if($data->toggle_multi_tiles["1","2","3"]) { ?>      // Line B
.hello-world {
    display:inline-block;       
}
<?php } ?>
</style>

Problem Statement:

I am getting error at Line A/Line B. I am wondering what changes I should make in the php code above at Line A/Line B so that it goes inside the if block with particular dataset of JSON.

Edit 1:

On doing var_dump($data); inside depth.php file, I am getting the following on the webpage ["toggle_multi_tiles"]=> array(4) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" }

On doing print_r($data); inside depth.php file I am getting [toggle_multi_tiles] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) on the webpage

flash
  • 1,455
  • 11
  • 61
  • 132
  • Please tell us what error you're encountering. We can't really do much to help you otherwise. – B. Fleming Aug 22 '19 at 20:49
  • 1
    It seems strange to dynamically render CSS. Does this situation get any easier if you have mutliple CSS classes (e.g. `.hello-world-block` and `.hello-world-inline`) and then change your depth.php code to put the correct class in the markup? – Cᴏʀʏ Aug 22 '19 at 20:50
  • @B.Fleming expected semicolon at **Line A/ Line B**. Probably thats not the right way. – flash Aug 22 '19 at 20:53
  • `$data->toggle_multi_tiles["1","2","3"]` is wrong, but its not clear what your trying to compare them 3 entries with i.e `toggle_multi_tiles)) { ?>` makes more sence if your toggling stuff – Lawrence Cherone Aug 22 '19 at 20:54
  • You're missing a semicolon in your PHP code, then. You need to terminate statements (not opening/closing blocks) with a semicolon `;` in order to tell PHP where the statement ends. Please review where PHP is telling you you're missing the semicolon and look around that line for where it's missing, then add it in place. – B. Fleming Aug 22 '19 at 20:56
  • More information. On doing `var_dump($data);` inside depth.php file, I am getting the following on the webpage `["toggle_multi_tiles"]=> array(4) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" }` – flash Aug 22 '19 at 21:05

1 Answers1

0

Did you try to use var_dump on $data? Is it really json? Also your if statements are not right. Try this. Let me know if it doesn't works. If you get error, please share it with us. They helps a lot to understand what's wrong.

<?php

   if (!file_exists('feeds/ptp-ess_landing.json')) {
       die('File not found');
       exit();
   }

   $data = json_decode(file_get_contents('feeds/ptp-ess_landing.json'));

   $display = (array)$data->toggle_multi_tiles == ["1", "2", "3"]
              ? 'inline-block'
              : 'block';
?>

   <style>
       .hello-world {
           display:<?php echo $display ?>;
       }
   </style>

Demo

Demo doesn't read data from file. Instead of it, i added data manually.