I am unable to get this loop to function properly. Whenever ($records[$row][2] == $prevRow2) I need it to recreate the class=field and only close the div=row that's part of the group where ($records[$row][2] == $prevRow2). Please help!!
if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {
$prevRow2 = false;
$row=0;
while (($data = fgetcsv($handle, 1000, ","))) {
$num = count($data);
$records[] = $data;
echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";
echo 'Is '. $prevRow2 . 'the same as ' .$records[$row][2];
if ($records[$row][2] == $prevRow2) {
for ($c=0; $c < $num; $c++) {
if ($c != 1) {
echo "<div class=\"field\">" . $data[$c] . "</div>";
}
}
$prevRow2 = $records[$row][2];
$row++;
echo "<div id=\"filler\"></div>";
}//if close
else {
for ($c=0; $c < $num; $c++) {
if ($c != 1) {
echo "<div class=\"field\">" . $data[$c] . "</div>";
}
}
$prevRow2 = $records[$row][2];
$row++;
}//close else
echo '</div>';
}//close while
fclose($handle);
}
$row seems to be null until the line you make $row++;
Some output and/or further description of your data would really help, but I notice a few things.
1 - Your closing if and else do exactly the same thing except for this filler div. I get the impression this is not what you desire, but I don't really understand enough to suggest what to do.
for ($c=0; $c < $num; $c++) {
if ($c != 1) {
echo "<div class=\"field\">" . $data[$c] . "</div>";
}
}
$prevRow2 = $records[$row][2];
$row++;
This code is common to both the if and else blocks, so it executes no matter what. Is that what you want? If so, you might as well move it out of the if and move only the filler div into the if, nothing in the else.
2 - What is the purpose of $records and $records[$row]. As far as I can see, $records[$row] === $data whenever its used.
3 - The div with id=row is closed at the end of every while loop. You seem to suggest you don't want this, put if you only do it when $records[$row][2] AKA $data[2] matches the previous iteration's, I don't think your HTML will be well formed.
4 - try using foreach instead of your foor loops
It looks like Company is actually index 3, not 2.
$prevData3 = false;
$row=0;
while (($data = fgetcsv($handle, 1000, ","))) {
if ($data[2] != $prevData2)
{
if ($prevData2) echo '</div>';
echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";
row++;
}
else echo "<div id=\"filler\"></div>";
foreach ($data as $d) {
if ($c != 1) {
echo "<div class=\"field\">" . $d . "</div>";
}
}
echo "<br />";
$prevData2 = $data[2];
}
echo CLOSE_DIV;
Related
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>';
}
}
}
?>
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!................................................
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.
I cannot get this to work properly, I need the program to list through the records within the file, if $records[$row][2]is the same as the previous, it should repeat class 'field' with a new $records[$row][2], otherwise it should start a new #row. Please help!
if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {
$prevRow2 = '';
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$records[] = $data;
echo 'Previous'. $prevRow2;
echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";
if ($records[$row][2] == $prevRow2) {
for ($c=0; $c < $num; $c++) {
if ($c != 1) {
echo "<div class=\"field\">" . $data[$c] . "</div>";
}
}
$prevRow2 = $records[$row][2];
$row++;
}
else {
echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";
for ($c=0; $c < $num; $c++) {
if ($c != 1) {
echo "<div class=\"field\">" . $data[$c] . "</div>";
}
}
$prevRow2 = $records[$row][2];
$row++;
echo "</div>";
}
echo "</div>";
}
fclose($handle);
}
First of all, you seem to assume that your CSV file is ordered by the column you want to group on. If this is not the case, then you should first read the file, sort it, and then print it. If your CSV file is not too big for the memory of your server, this should work.
Also, your code has repetitions and store the entire file into an array (actually, just like I suggested above, but I don't know if it is intentional). try something like this:
This is my demo.csv:
"Banana", "yellow"
"Lemon", "yellow"
"Orange", "orange"
"Strawberry", "red"
"Tomato", "red"
This is the PHP to loop through it and list it by color:
<?php
if (($handle = fopen('demo.csv', "r"))) {
$prev = false;
while (($data = fgetcsv($handle, 1000, ","))) {
if ($prev !== $data[1]) {
$prev = $data[1];
echo '<p><b>' . $prev . '</b></p>';
}
echo $data[0] . '<br>';
}
}
fclose($handle);
?>
This is the output in HTML (I added some linebreaks for proper display here)
<p><b>yellow</b></p>
Banana<br>
Lemon<br>
<p><b>orange</b></p>
Orange<br>
<p><b>red</b></p>
Strawberry<br>
Tomato<br>
(If the CSV was not grouped by color, you'd need a different approach.)
Is it possible for me to display the sum at the heading before the program runs through?
while (($data = fgetcsv($handle, 1000, ","))) {
if($data[2] != $prevRow2) {
echo '</div>';
if ($prevRow2 != '') {
$stringData .= '</Payment>';
}
echo "<div id=\"row\">";
echo $sum;
$row++;
$sum = 0;
}
else { echo "<div id=\"filler\"></div>";}
foreach ($data as $key => $d) {
if ($key != 1) {
echo "<div class=\"field\">" .$d . "</div>";
}
}
$sum +=$data[6];
echo "<br/>";
echo "<div id=\"filler\"></div>";
$prevRow2 = $data[2];
}
fclose($handle);
}
You could buffer the output, print out the heading with the sum after the loop ran through, and then output the buffer.
This could be accomplished simply by not echoing but assigning all the values to a variable and echoing that variable at the end - or by using the ob_start, ob_end_flush, etc functions.
So in your example, instead of:
while (true) {
echo "lots of code";
echo "some variable: " . $variable;
$sum = $sum + 1;
}
Write:
while (true) {
$output .= "lots of code";
$output .= "some variable: " . $variable;
$sum = $sum + 1;
}
echo $sum;
echo $output;