How to compare two arrays and show matches in columns - php

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?

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>";
}

Iterate through Array1 ( foreach loop )
Check each value against Array2 ( in_array() )
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>";

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>

Related

PHP Nesting do-while loop

Im trying to teach myself php.... I have created a database with 4 elements... region, details, store_name, web_address.
I have created ado-while loop that displays all the records in a 'region'... it displays 'store_name', then 'web_address', then 'details' sorted so all the records with the same 'details' are grouped together;
<table width="600" align="center" border="0" cellspacing="4" >
<tr>
<td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
<td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
</tr>
<?php do { ?>
<tr>
<td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
<td colspan="2" rowspan="2" align="center"> <?php echo '' . $row_RegionList['web_address'] . ''; ?></td>
</tr>
<tr>
<td width="14">
<td width="313"><?php echo $row_RegionList['details']; ?> </tr>
<?php } while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>
I now want to make it so every record with the same 'details' value get listed under a 'details' sub heading.
This is my attempt;
<table width="600" align="center" border="0" cellspacing="4" >
<tr>
<td colspan="2"><h2>Web Stockists: <strong></strong></h2></td>
<td width="251" colspan="2" align="right"><h3>Region: <?php echo $region_text ?></h3></td>
</tr>
<?php $details = $row_RegionList['details'];
do { ?>
<tr>
<td width="313"> <h3><?php echo $row_RegionList['details']; ?> </h3></td>
</tr>
<?php do { ?>
<tr>
<td colspan="2"><strong><?php echo $row_RegionList['store_name']; ?></strong></td>
<td colspan="2" rowspan="2" align="center"> <?php echo '' . $row_RegionList['web_address'] . ''; ?></td>
</tr>
<?php } while ($row_RegionList['details'] == $details);
$details = $row_RegionList['details'];
} while ($row_RegionList = mysql_fetch_assoc($RegionList)); ?>
</table>
This makes a heading of 'region' with a sub heading of 'details' but then creates an eternal loop of the first record in the database. can someone show me what I have done wrong?

While Loop For Table PHP

I'm trying to do this in while loop PHP, number of assignments are unlimited so rowspan should also adjust itself with the number of rows, is there any proper way to do it with minimum numbers of line?
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Assignment No</th>
<th>Student Name</th>
<th>Assignment Marks</th>
<th>Overall Result</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="center">S1</td>
<td align="center">5</td>
<td rowspan="3" align="center">B Grade</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">S1</td>
<td align="center">8</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">S1</td>
<td align="center">7</td>
</tr>
</tbody>
</table>
Try This:
<? foreach($dataarray as $data)
{?>
<tbody>
<tr>
<td align="center"><? echo $data[0]; ?></td>
<td align="center"><? echo $data[1]; ?></td>
<td align="center"><? echo $data[2]; ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="3" align="center"><? echo $data[3]; ?></td>
<? } ?>
<? } ?>
and put if condition for last column
Assuming that data comes in a linear fashion (ie a plain one dimensional array), and that the grade is not in the array, I'd do something like this:
<?
$sizeofArray = count($data);
$rowspan = floor($sizeofArray/3);
for($arrCnt = 0; $arrCnt < $sizeofArray; $arrCnt +=3)
{?>
<tbody>
<tr>
<td align="center"><?=$data[$arrCnt]; ?></td>
<td align="center"><?=(($arrCnt+1 < $sizeofArray)?$data[$arrCnt+1]:" "); ?></td>
<td align="center"><?=(($arrCnt+2 < $sizeofArray)?$data[$arrCnt+2]:" "); ?></td>
</tr>
<? if($data[3]!=Null) { ?>
<td rowspan="<?=$rowspan?>" align="center"><?=$grade; ?></td>
<? } ?>
<? } ?>
EDIT: added "padding" code, to avoid index out of bounds error
Based on your case, rowspan is different from one student to another. What you would want to do, is group data by student. The number of assignments per student is what will give you rowspan value.
While ... {
$data['student'][] = ['name' => $row -> name]; // all student inf
$data['student][]['assignments'][] = ['id' => $row -> assignement_id];//student assigments
}
Hope this help or put in the right direction to resolve your problem.
use foreach to loop
foreach ($data as $student){
$rowspan = count($student['assignments'];
foreach ($student['assigments'] as $assignment ){
// html table
}
}

store part of the page in variable

i have a page contains a table and this table contains a lot of coding like this
<table class="table">
<tbody>
<tr >
<td width="20" class="tabletop">م</td>
<td class="tabletop" >name</td>
<td class="tabletop" style="width:120px">date</td>
<td class="tabletop" style="width:120px">note1</td>
<td class="tabletop" style="width:100px">note2</td>
<td class="tabletop" style="width:90px">note3</td>
</tr>
<? $res=mysql_query($sql);
while($resArr=mysql_fetch_array($res)){ ?>
<tr style="width:700px">
<td class="tabletext"><?= ++$serial;?></td>
<td class="tabletext" ><?= $resArr[stName];?></td>
<td class="tabletext"><?= $resArr['date'];?></td>
<td class="tabletext" ><?= $resArr[matName];?></td>
<td class="tabletext" ><? if($resArr[exam]==1) echo "work";else echo "final";?></td>
<td class="tabletext" ><? if($resArr[exam_type]==1) echo "prac";else echo "test";?></td>
</tr>
<? }?>
</tbody>
</table>
as you see the table has php coding
now i want to store the whole table in variable so i can send it to pdf printing library tcpdf
You can use heredoc syntax, but you need to move the conditionals outside:
<?php
if($resArr['exam']==1) $exam = "work"; else $exam = "final";
if($resArr["exam_type"]==1) $examtype = "prac";else $examtype = "test";
$var = <<<EOT
<table class="table">
<tbody>
<tr >
<td width="20" class="tabletop">م</td>
<td class="tabletop" >name</td>
<td class="tabletop" style="width:120px">date</td>
<td class="tabletop" style="width:120px">note1</td>
<td class="tabletop" style="width:100px">note2</td>
<td class="tabletop" style="width:90px">note3</td>
</tr>
$res=mysql_query($sql);
while($resArr=mysql_fetch_array($res)){
<tr style="width:700px">
<td class="tabletext">{++$serial}</td>
<td class="tabletext" >{$resArr["stName"]}</td>
<td class="tabletext">{$resArr["date"]}</td>
<td class="tabletext" >{$resArr["matName"]}</td>
<td class="tabletext" >{$exam}</td>
<td class="tabletext" >{$examtype}</td>
</tr>
<? }?>
</tbody>
</table>
EOT;
echo $var;
Another way, if you already have all of this code and want to save it:
Before the table:
<?php ob_start(); ?>
After the table:
<?php $output = ob_get_contents(); ?>
The table will still be displayed and you can use $output to send to the PDF.

Pulling last years data from a table

Hello I'm trying to update this scholarship application box. However when the year changed to 2013 it only displays scholarship applicant info from 2013. I'd like it to display info from 2012. I tried messing around with the date but I cant seem to figure it out. Any help would be greatly appreciated!
<?php
$appYear = date("Y").'-'.(date("Y")+1);
$sql = 'select * from sApplication where studentID = "'.$database->iPrep($_SESSION['ID']).'" AND appYear = "'.$appYear.'" LIMIT 1';
$appID = Scholarship::iFindSQL($sql);
$total = count($appID);
if ($total > 0)
{
$app = array_shift($appID);
}
else
{
$app = 0;
}
?>
<li id="item-2">
<div id="appStatus">
<h3>Application Status</h3>
<blockquote>
<?php if ($app->submitted == ('0000-00-00') || !isset($app->submitted)) { ?>
<table style="border:1px solid #000;" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="50%"><strong>Scholarship<br /> 2013-2014</strong></td>
<td width="50" align="right"> <a style="font-size:16px;" href="welcome.php? app=Scholar">Apply Now</a></td>
</tr>
<tr>
<td><strong>Date Submitted</strong></td>
<td align="right"> </td>
</tr>
<tr>
<td><strong>References</strong></td>
<td align="right"> </td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td> </td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<tr>
<td><strong>Scholarship 2012-2013</strong></td>
<td> </td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td> </td>
</tr>
</table>
<?php } else { ?>
<table style="border:1px solid #000;" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr>
<td width="90%"><strong>Scholarship 2013-2014</strong></td>
<td width="10%" align="right"> </td>
</tr>
<tr>
<td><strong>Date Submitted</strong></td>
<td align="right"><?=dbOutDate($app->submitted)?></td>
</tr>
<tr>
<td><strong>References</strong> </td>
<td align="right"></td>
</tr>
<tr>
<td colspan="2">
<?php
$refs = Reference::iFindSQL("Select * from reference where appID = '".$app->ID."'");?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php foreach($refs as $ref) { ?>
<tr> <td> <small><?php if($ref->rType == 'Academic Reference'){ echo 'Academic/Artistic/Professional'; } else { echo 'Community Service'; } ?></small></td> <td align="right"><?=$ref->status?></td></tr>
<?php } ?>
</table>
</td>
</tr>
<tr>
<td><strong>Decision</strong></td>
<td align="right">
<?php
if ($app->complete == 'Approved') { echo 'Approved'; }
if ($app->complete == 'Declined') { echo 'Declined'; }
if ($app->complete == 'Pending') { echo 'Pending'; }
if ($app->complete == 'Incomplete') { echo 'Incomplete'; }
Remove the WHERE clause that limits the year. Use ORDER BY to sort by year, descending.
$sql = 'select * from sApplication
where studentID = "'.$database->iPrep($_SESSION['ID']).
'" ORDER BY appYear DESC LIMIT 1';

Multidimensional Arrays two loops

MSSQL query link is this;
click here for picture
and my output shows three different pictures for the same product as shown below. What I want is, if the product is the same, keep just one picture and then get the colors and sizes for that product.
Means;
My output ıs the picture below,
click here for picture
as you see there are three product in the picture but they are the same product with different colors and sizes, instead of seeing the same product every time, I want my output like in the picture below.
<table width="376" cellspacing="0" class="stats" width:100%>
<tr>
<td colspan="9" align="center"><?php echo $secim ?></td>
</tr>
<?php
while(odbc_fetch_into($sql_result, &$row)) {
$unit1 = floor($row[3]);
$unit2 = floor($row[4]);
$unit3 = floor($row[5]);
$unit4 = floor($row[6]);
$unit5 = floor($row[7]);
?>
<tr>
<td colspan="2" align="left" valign="top"><?php echo"$row[0]";?></td>
<td>36</td>
<td>38</td>
<td>40</td>
<td>42</td>
<td>44</td>
</tr>
<tr>
<td width="114" align="right" valign="top">
<img src= <?php echo"images/Resize/$row[2]"?>></td>
<td width="25" valign="top"><?php echo"$row[1]";?></td>
<td width="25"valign="top"><?php echo"$unit1";?></td>
<td width="25"valign="top"><?php echo"$unit2";?></td>
<td width="25"valign="top"><?php echo"$unit3";?></td>
<td width="25"valign="top"><?php echo"$unit4";?></td>
<td width="25"valign="top"><?php echo"$unit5";?></td>
</tr>
<?php } }?>
<?php
odbc_free_result($sql_result);
odbc_close($connection);
?>
</table>
I think this is what you are looking for, http://jsfiddle.net/sv8ZS/
while(odbc_fetch_into($sql_result, &$row)) {
$unit1 = floor($row[3]);
$unit2 = floor($row[4]);
$unit3 = floor($row[5]);
$unit4 = floor($row[6]);
$unit5 = floor($row[7]);
?>
//you can check with the index to see if its a first row or not
//This will avoid printing the same header for each row.
if (this-is-the-first row) {
<tr>
<td colspan="2" align="left" valign="top"><?php echo"$row[0]";?></td>
<td>36</td>
<td>38</td>
<td>40</td>
<td>42</td>
<td>44</td>
</tr>
}
//above if ends
<tr>
//If its not a first row then do not show the image again and merge all the next rows
if (this-is-the-first-row) {
<td width="114" align="right" valign="top" rowspan='3'>
<img src= <?php echo"images/Resize/$row[2]"?>
</td>
}
//above if ends
<td width="25" valign="top"><?php echo"$row[1]";?></td>
<td width="25"valign="top"><?php echo"$unit1";?></td>
<td width="25"valign="top"><?php echo"$unit2";?></td>
<td width="25"valign="top"><?php echo"$unit3";?></td>
<td width="25"valign="top"><?php echo"$unit4";?></td>
<td width="25"valign="top"><?php echo"$unit5";?></td>
</tr>
<?php } }?>
enter code here
<?php
echo "<table border='1'>
<tr>
<th>Model</th>
<th>Color</th>
<th>Unit</th>
</tr>";
?>
<?php
while($row =odbc_fetch_array($result)){
//if $row['sAciklama']=$row['sAciklama'] bla bla ???
if (this-is-the-first row) {
echo "<tr>";
echo "<td>" . $row['sAciklama'] . "</td>"; //Model First Element
}
if (this-is-the-first-row) {
echo "<td>" . $row['sRenkAdi'] . "</td>"; //color
echo "<td>" . $row['Kalan'] . "</td>"; //unit
echo "</tr>";
}}
echo "</table>";
?>

Categories