I want to take the print of students reports with following PHP code. (the 4th column is Marks). I am unable to modify the code. Can any body help me . And i need the reports where the marks is more than 75. in this regard?
<?php
$row = 1;
if(($handle = fopen("xyz.csv", "r")) !== false) {
$table1 = '<table div id="kk">';
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$table1Add = false;
if($row >=1 && $row <= 53)
$table1Add = true;
$num = count($data);
if($row == 1) {
$table1 .= '<thead><tr>';
for($c = 0; $c <= 8; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
$table1 .= '<th>'.$value.'</th>';
}
$table1 .= '</tr></thead><tbody>';
} else {
if($table1Add) $table1 .= '<tr>';
for($c = 0; $c <= 8; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($table1Add) $table1 .= '<td>'.$value.'</td>';
}
if($table1Add) $table1 .= '</tr>';
}
$row++;
}
$table1 .= '</tbody></table>';
fclose($handle);
echo $table1;
}
?>
Try:
<?php
$row = 1;
if(($handle = fopen("xyz.csv", "r")) !== false) {
$table1 = '<table div id="kk">';
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$table1Add = false;
if($row >=1 && $row <= 53)
$table1Add = true;
$num = count($data);
if($row == 1) {
$table1 .= '<thead><tr>';
for($c = 0; $c <= 8; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
$table1 .= '<th>'.$value.'</th>';
}
$table1 .= '</tr></thead><tbody>';
} else {
if($table1Add) $table1 .= '<tr>';
/* this bit edited */
$temp_holder = '';
$flag = false;
for($c = 0; $c <= 8; $c++) {
if($c==3 && $data[$c]>75) { $flag = true; }
if($table1Add){
$temp_holder .= '<td>'.(empty($data[$c]) ? " " : $data[$c]).'</td>';
}
if($c==8 && flag==true) { $table1 .= $temp_holder; }
/* till here */
}
if($table1Add) $table1 .= '</tr>';
}
$row++;
}
$table1 .= '</tbody></table>';
fclose($handle);
echo $table1;
}
?>
What's done above is, we will use a temp variable to keep all the <td>
data, when you reach the 4th column (which is 3 starting from 0), check if the marks is greater than 75, if yes, set the flag
to true. If the flag
is true, that means that row has marks greater than 75, so append all the <td>
values from the temp variable to the table1
variable.