2

I've to display the td value as per th value matching. Here in the below table has th which has month dates. In the $monthdays I'm getting month days count and in the $listmonthdisplay I'm getting all the dates of the months.
In the $employeeDetail array I'm getting the month dates. In the table I'm doing foreach to display attendence_date if attendence_date is equal to $listmonthdisplay but when any dates is missing in the $employeeDetail array then loop is failing or not going forward. Here How can correctly I map the tb value?

$monthdays=28;
$listmonthdisplay=[
  "0"=>"01-02-2019",
  "1"=>"02-02-2019",
  "2"=>"03-02-2019",
  "3"=>"04-02-2019",
  "4"=>"05-02-2019", ............"28"=>"05-02-2019"
]

$employeeDetail=[
     "0"=>["attendence_date"=>"01-02-2019"],
     "1"=>["attendence_date"=>"02-02-2019"],
     "2"=>["attendence_date"=>"05-02-2019"],
     "3"=>["attendence_date"=>"08-02-2019"],
     "5"=>["attendence_date"=>"09-02-2019"]
  ]   
 <table>
  <tr>
    <?php for($i=0;$i<$monthdays;$i++){ ?>
    <th><?php echo $listmonthdisplay[$i]?></th>
    <?php }?>
 </tr>
<tr>
  <?php for($i=0;$i<$monthdays;$i++){ ?>
  <td>
 <?php
   if((isset($employeeDetail[$i]) && $listmonthdisplay[$i]==$employeeDetail[$i]['attendence_date']){
     echo $employeeDetail[$i]['attendence_date'];
   }?>
 </td>
  <?php }?>
 </tr>

 </table>


Current output:

 <table>
  <tr>
    <th>01-02-2019</th>
    <th>02-02-2019</th>
    <th>03-02-2019</th>
    <th>04-02-2019</th>
    <th>05-02-2019</th>
    <th>06-02-2019</th>
    <th>07-02-2019</th>
    <th>08-02-2019</th>
    <th>09-02-2019</th>........<th>28-02-2019</th>
 </tr>
<tr>
    <td>01-02-2019</td>
    <td>02-02-2019</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>.......<td></td>
 </tr>

 </table>


Expected output:

 <table>
  <tr>
    <th>01-02-2019</th>
    <th>02-02-2019</th>
    <th>03-02-2019</th>
    <th>04-02-2019</th>
    <th>05-02-2019</th>
    <th>06-02-2019</th>
    <th>07-02-2019</th>
    <th>08-02-2019</th>
    <th>09-02-2019</th>
 </tr>
<tr>
    <td>01-02-2019</td>
    <td>02-02-2019</td>
    <td></td>
    <td></td>
    <td>05-02-2019</td>
    <td></td>
    <td></td>
    <td>08-02-2019</td>
    <td>09-02-2019</td>.......<td></td>
 </tr>

 </table>
Devendra
  • 219
  • 2
  • 22

1 Answers1

1

I edited it and it's working right now.

<?php
$monthdays=28;
$listmonthdisplay=[
    "0"=>"01-02-2019",
    "1"=>"02-02-2019",
    "2"=>"03-02-2019",
    "3"=>"04-02-2019",
    "4"=>"05-02-2019",
    "5"=>"06-02-2019",
    "6"=>"07-02-2019",
    "7"=>"08-02-2019",
    "8"=>"09-02-2019",
    "9"=>"10-02-2019",
    "10"=>"11-02-2019",
    "11"=>"12-02-2019"
    ...
];
$employeeDetail=[
    "0"=>["attendence_date"=>"01-02-2019"],
    "1"=>["attendence_date"=>"02-02-2019"],
    "2"=>["attendence_date"=>"05-02-2019"],
    "3"=>["attendence_date"=>"08-02-2019"],
    "5"=>["attendence_date"=>"09-02-2019"]
];
$tmpArray = array_column($employeeDetail, 'attendence_date');
?>
<table>
    <tr>
        <?php 
        for($i=0;$i<$monthdays;$i++){ 
            echo "<th>".$listmonthdisplay[$i]."</th>";
        }
        ?>
    </tr>
    <tr>
        <?php 
        for($i=0;$i<$monthdays;$i++){
            echo "<td>";
            if(in_array($listmonthdisplay[$i], $tmpArray)){
                echo $listmonthdisplay[$i];
            }
            echo "</td>";
        }
        ?>
        </tr>
</table>

I've changed two things and it's working right now
First add this code:

$tmpArray = array_column($employeeDetail, 'attendence_date');

to get a one-dimensional array of dates to be printed
The second is to change the condition to search date in the $tmpArray

Sajad Mirzaei
  • 2,635
  • 1
  • 11
  • 13