check column values in php not working - php

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.

Related

How to create dynamic table from both directory and CSV files

I need to create a table whose first column is populated from subdirectory names inside a directory and rest are from a CSV file. This have to be a dynamic table and table headers have to be added from the code. What's wrong with my code?
I am an absolute beginner. So, please ignore my stupidity.
$dir = 'D:\workspace';
$dirlist = preg_grep('/^([^.])/', scandir($dir));
$row = 1;
if (($handle = fopen("D:\workspace\demo\database.csv", "r")) !== FALSE) {
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
foreach ($dirlist as $rowdirectory)
{
echo '<td>' . $rowdirectory . '</td>';
echo '<td>'.$value.'</td>';
}
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
<?php
#------------------------------------------Function for Reading Directory-------------------------------------------
function readdirectory($dir)
{
$dirlist = preg_grep('/^([^.])/', scandir($dir)); // for all(./../anything that
starts with .)
//$dirlist = preg_grep('/[^.]/', scandir($dir)); //only . & ..
//$dirlist =preg_grep('/^[(^.)]/', scandir($dir));//only files that starts with .
return $dirlist;
}
#------------------------------------------Function for Reading CSV file-------------------------------------------
function readcsvfile($source)
{
$handle = fopen($source, "r");
$filecontent = null;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE)
{
$filecontent[] = $data;
}
fclose($handle);
return $filecontent;
}
$filecontent= readcsvfile("D:\workspace\demo\database2.csv");
#-------------------Directory Function calling, header array cration and other declaration------------------------------------
$conf_prefix= "../";
$conf_suffix="index.php";
$headerarray= $filecontent[0];
#var_dump($headerarray);
$headersize= count($headerarray);
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
$dir = 'D:\workspace';
$dirlist= readdirectory($dir);
#------------------------------------------Creating 1st row/Header row of Table-------------------------------------------
echo '<tr>';
for($a=0; $a<$headersize;$a++)
{
echo '<td>'.$headerarray[$a].'</td>';
}
echo '</tr>';
#-----------------------------Creating Table elements by comparing both directory arrays and CSV file array-------------------------------------------
foreach ($dirlist as $rowdirectory)
{
foreach ($filecontent as $value) {
$num = count($value);
if ($rowdirectory== $value[0])
{
$link= $conf_prefix. $rowdirectory."/".$conf_suffix;
echo '<tr>';
echo '<td> ' . $rowdirectory . ' </td>';
for( $c=1; $c < $num; $c++) {// loop for csv file
{
echo '<td>'.$value[$c].'</td>';//else print "value"
}
}
echo '</tr>';
}
}
}
?>

php - CSV table with button to move between pages

I need my script will show at the bottom of the table a next and previous page button
any ideas?
The code:
<?php
$row = 1;
if (($handle = fopen("example.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
Any help will be appreciated!................................................

How to access values of row 1

i would like to place the values of row 1 from my csv file within the td as in the example:
<td data-label="Jaar">2014</td>
Where in this example the word "Jaar" is cel a1 in my csv file.
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
} else {
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
print_r($data[$c]);
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th scope="col">'.$value.'</th>';
}else{
echo '<td data-label=".In here is want to place the values of row 1 or 2 or 3.">'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
In the php code the second line of the csv file is parsed as td, but i want to place the value of a1 in the first td data-label and the value of b1 into the second td data-label. Can anyone give me a hint.
csv file example:
aaaa;bbbb;cccc
1111;2222;3333
11;22;33
Save row 1 in a variable:
if ($row == 1) {
echo '<thead><tr>';
$colnames = $data;
} else {
echo '<tr>';
}
then you can use it in the remaining rows:
if ($row == 1) {
echo '<th scope="col">'.$value.'</th>';
}else{
echo '<td data-label="'.$colnames[$c].'">'.$value.'</td>';
}

Merging arrays to a single array to find minimum value

I have a csv file and I am trying to get the minimum value for a particular column. However I am not getting an exact value even after exploding the values. After I explode and I do a var_dump on it, it produces a different array for each different row; so each value in the column is the first element of a different array. How do I merge this different arrays into one array in order to find the minimum value in the column, or is there any other way to go about it? Below is my code. Any help will be appreciated.
merging
while (($data = fgetcsv($handle, 0, ",")) !== false) {
$table2Add =false;
if($row >=$x && $row <=$y) {
$table2Add=true; }
if($row == 1) {
$table2 .= '<thead><tr>';
$table2 .= '<th></th>';
$table2 .= '<th>Min</th>';
$table2 .= '<th>Max</th>';
$table2 .= '<th>Average</th>';
$table2 .= '</tr></thead><tbody>';
}
else {
if($table2Add) $table2 .= '<tr><th>Voltage</th>';
for($c = 0; $c <= 10; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($c==1) {
var_dump((explode('STRING',$data[$c])));
//$ar=explode('STRING', $data[$c]);
//var_dump(min($ar));
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==2) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==3) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
}
if($table2Add) $table2 .= '</tr>';
if($table2Add) $table2 .= '<tr><th>Current</th>';
for($c = 0; $c <= 10; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($c==4) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==5) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==6) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
}
if($table2Add) $table2 .= '</tr>';
if($table2Add) $table2 .= '<tr><th>Power Factor</th>';
for($c = 0; $c <= 10; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($c==7) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==8) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
if($c==9) {
if ($table2Add) $table2 .='<td>'.round($value,2).'</td>';
}
}
if($table2Add) $table2 .= '</tr>';
}
$row++;
}

Import specific rows and columns form csv file into table using php

I'm very new to php but have been experimenting fairly successfully with using 'fgetcsv' to bring in a CSV file and convert it into an html table.
However I also have a large CSV file with 70 columns and 700 rows, but I only want to display columns 1 to 47 and rows 3 to 21 in one table, and then same columns but rows 22 to 44 in another table.
I'd appreciate some help with this, below is the code I am currently using:
<?php
$row = 1;
if (($handle = fopen("CSV/test.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<thead><tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
<?php
$row = 1;
if(($handle = fopen("CSV/test.csv", "r")) !== false) {
$table1 = $table2 = '<table border="1">';
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$table1Add = $table2Add = false;
if($row >=3 && $row <= 21)
$table1Add = true;
if($row >=22 && $row <= 44)
$table2Add = true;
$num = count($data);
if($row == 1) {
$table1 .= '<thead><tr>';
$table2 .= '<thead><tr>';
for($c = 1; $c <= 47; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
$table1 .= '<th>'.$value.'</th>';
$table2 .= '<th>'.$value.'</th>';
}
$table1 .= '</tr></thead><tbody>';
$table2 .= '</tr></thead><tbody>';
} else {
if($table1Add) $table1 .= '<tr>';
if($table2Add) $table2 .= '<tr>';
for($c = 1; $c <= 47; $c++) {
$value = empty($data[$c]) ? " " : $data[$c];
if($table1Add) $table1 .= '<td>'.$value.'</td>';
if($table2Add) $table2 .= '<td>'.$value.'</td>';
}
if($table1Add) $table1 .= '</tr>';
if($table2Add) $table2 .= '</tr>';
}
$row++;
}
$table1 .= '</tbody></table>';
$table2 .= '</tbody></table>';
fclose($handle);
echo $table1;
echo $table2;
}
?>
for the columns try this
for ($c=0; $c < 47; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
So you print cells between column 0 and column 47.
For the row you have to set a a counter, and, inside the while statement, use an if to print the rows in the range that you need. It's better that you set an exit condition in while to exit after row 44 if you dont need the others.

Categories