flash
flash

Reputation: 1519

scan dates from JSON and print specific content on the basis of date

I have a php code as shown below:

Php Code:

<?php
    the_title('<h1 class="position">', '</h1>');
    echo '<span class="data">' .date('yy-m-d').  '</span>'; // Line B
    echo '<span class="part">Sitting Day</span>';  // Line C
?>

The above php code (Line B and Line C) prints the following: I am using Sitting Day at Line C (just for fun) but it should print what is inside the JSON below:

JSON:

{
    "position_day": ["2020-01-15", "2020-01-16", "2020-01-17"],
    "proc_no": ["no", "yes", "no"]
}

Edit 1: I am not controlling the JSON. The values inside the JSON are passed via UI.

Problem Statement:

I am wondering what changes I should make in the php code above (specially at Line B and Line C) so that Line B looks/match/scan for a date inside the JSON above and print content at Line C on the basis of date in the JSON.

Case 1: If today's date is 2020-01-15 at Line B and its no for the corresponding date in the JSON, then it should say Not a Sitting Day at Line C.

Case 2: If today's date is 2020-01-16 at Line B and its yes for the corresponding date in the JSON, then it should say Sitting Day at Line C.

Case 3: If today's date is 2020-01-22 at Line B and nothing is present in the JSON for that particular date, then it should say display blank/nothing at Line C.

Upvotes: 3

Views: 195

Answers (2)

ROOT
ROOT

Reputation: 11622

Based on your description here is the change:

<?php
if (file_exists('feeds/ptp-ess_landing_house.json')) {
    $data_house = json_decode(file_get_contents('feeds/ptp-ess_landing_house.json'));
}

// $strdate = strtotime('2020-01-17');
// $date = date("Y-m-d", $strdate);

$date = date("Y-m-d");
$answer_prefix = "Not a ";

$sitting_day_str = "Sitting Day";
?>
<span class="current-date"><?php echo $date ?></span>
<?php foreach( $data_house->house_sitting_date as $key=>$val) {
  if($date === $val && $data_house->house_sitting_date_yes_no[$key] === "yes") {
    $answer_prefix = "";
    break;
  }
}
?>
<?php if(in_array($date, $data_house->house_sitting_date)) {?>
  <span class="current-date-answer"><?php echo $answer_prefix . $sitting_day_str ?></span>
<?php } ?>

Note: this can be cleaner by having it in a function, but I will leave to you since you provided little information about what you are using as framework/CMS.

Upvotes: 2

Jhecht
Jhecht

Reputation: 4435

This works by keeping track of the index we are looping through ($i) and using it for both arrays. The only problem here would be if one of those arrays were a different size than the other, in which case you'll run into errors.

The code is not guaranteed to run -- I don't have access to a PHP environment right now -- but I hope you can do whatever minor debugging would be necessary to get a working solution.

<?php
$data_house = json_decode(file_get_contents('feeds/ptp-ess_landing_house.json'));

for($i = 0; $i < count($data_house['house_sitting_date']; $i++) {
?>
  <span class="current-date"> <?php echo date('yy-m-d', $data_house['house_sitting_data'][$i]).</span>
  <span class="current-date-answer"><?php $data_house['house_sitting_date_yes_no'][$i] == 'yes' ? 'Sitting Day' : 'Not a Sitting Day' ?></span>
<?php
}
?>

Upvotes: 0

Related Questions