3

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.

5
  • Didn't you post this earlier? Commented Jan 17, 2020 at 18:47
  • Yes, I posted that earlier. I thought I didn't post my question properly so I thought to re-post it. Commented Jan 17, 2020 at 18:48
  • 1
    Are you the person who controls what the JSON looks like? If so, just change it to something like house_sitting_dates: [{ "date":"2020-01-15", "did_sit": false}, {"date":"2020-01-16", "did_sit": true}]. Otherwise, when you loop through one of them, just keep a reference to the index and spit out the same indexed value from the other array. Commented Jan 17, 2020 at 19:21
  • I am not controlling the JSON. Values inside the JSON go through UI. Commented Jan 17, 2020 at 19:23
  • Some relation need to be made between Line B/Line C and JSON. Commented Jan 17, 2020 at 19:23

2 Answers 2

2
+50

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.

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

4 Comments

I have added one more case. It seems to be working` for Case 1 and Case 2 but for Case 3 it is printing Not a Sitting Day. It should display blank for Case 3
Will check it now!
Hi Ma'moun, I hope all is well. I have a similar question. I am wondering if you can have a look.
Thank you. Its related to unicode and array_filter.
0

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
}
?>

Comments

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.