Reputation: 4047
I have two arrays.
Array 1 - List of all dates
Array 2 - list of all dates a person is present
I want to show a table that has all the dates in the first row and at each day that a person is present on the second row, the respective column should say present.
I tried nested loop but it just shows various rows and one result per row i.e. only one match per row.
I want to accomplish something like this
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td width="6%">160111</td>
<td width="6%">160113</td>
<td width="6%">160120</td>
<td width="6%">160127</td>
<td width="6%">160201</td>
<td width="6%">160203</td>
<td width="6%">160208</td>
<td width="6%">160210</td>
<td width="6%">160217</td>
<td width="6%">160224</td>
<td width="6%">160229</td>
<td width="6%">160302</td>
<td width="6%">160307</td>
<td width="6%">160309</td>
<td width="6%">160321</td>
<td width="5%">160323</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>Present</td>
<td> </td>
<td> </td>
<td> </td>
<td>Present</td>
<td> </td>
<td> </td>
<td> </td>
<td>Present</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
Suggestions? How to run loop or how can this be achieved?
Upvotes: 2
Views: 72
Reputation: 2058
You can try this if Array are same like I given in my answer
PHP Code:
<?php
$dates =array(
'160111','160113','160120','160127','160201','160203',
'160208','160210','160217','160224','160229','160302',
'160307','160309','160321','160323'
);
$person_present =array(
'','','','160127','','',
'','160210','','','','160302',
'','','',''
);
foreach ($dates as $pkey => $day)
{
if ( in_array($day, $person_present) ) {
?>
<td>Present</td>
<?php
} else {
?>
<td> </td>
<?php
}
}
?>
Full Code:
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td width="6%">160111</td>
<td width="6%">160113</td>
<td width="6%">160120</td>
<td width="6%">160127</td>
<td width="6%">160201</td>
<td width="6%">160203</td>
<td width="6%">160208</td>
<td width="6%">160210</td>
<td width="6%">160217</td>
<td width="6%">160224</td>
<td width="6%">160229</td>
<td width="6%">160302</td>
<td width="6%">160307</td>
<td width="6%">160309</td>
<td width="6%">160321</td>
<td width="5%">160323</td>
</tr>
<tr>
<?php
$dates =array(
'160111','160113','160120','160127','160201','160203',
'160208','160210','160217','160224','160229','160302',
'160307','160309','160321','160323'
);
$person_present =array(
'','','','160127','','',
'','160210','','','','160302',
'','','',''
);
foreach ($dates as $pkey => $day)
{
if ( in_array($day, $person_present) ) {
?>
<td>Present</td>
<?php
} else {
?>
<td> </td>
<?php
}
}
?>
</tr>
</table>
Upvotes: 0
Reputation: 144
Eg:-
echo "<br/><table border='1' style='width:100%'><tr><td>";
foreach($arr1 as $a1) {
echo "<table border='1' style='display:inline;border:0px solid'>";
echo "<tr><td>$a1</td></tr>";
if(in_array($a1,$arr2)) {
echo "<tr><td>Present</td></tr>";
} else {
echo "<tr><td> </td></tr>";
}
echo "</table>";
}
echo "</td></tr></table>";
Upvotes: 0
Reputation: 781370
Use in_array()
to test whether an item is in Array 2.
foreach ($array1 as $day) {
echo "<td>";
echo in_array($day, $array2) ? "Present" : " ";
echo "</td>";
}
Upvotes: 2