Getting every 7th element from array while echoing a table - php

I'm getting the data I need from fgetcsv and store them into $data. It contains a table with a header row and lot's of info. Every 7th column is the path to where the file is stored.
I've already searched for what my problem is but I can't seem the find the solution.
My Code yet:
echo '<table border="0" cellspacing="0" cellpadding="5" class="csvTable" width="auto">';
$handle = fopen("index.csv", "r");
$start = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
echo '<tr>' . "\n";
for ( $x = 0; $x < count($data); $x++) {
if ($start == 0 && $hasTitle == true)
echo '<th>'.$data[$x].'</th>' . "\n";
else
echo '<td>'.$data[$x].'</td>' . "\n";
}
$start++;
echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';
I want to add a hyperlink via <a href=?> on every 7th column but I don't know how. How can I do it and is that the right way?

You check every column is a 7th column or divisible by 7 you can just check if the variable is divided by 7 like this.
echo '<table border="0" cellspacing="0" cellpadding="5" class="csvTable" width="auto">';
$handle = fopen("index.csv", "r");
$start = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
echo '<tr>' . "\n";
for ( $x = 0; $x < count($data); $x++) {
if ($start == 0 && $hasTitle == true)
echo '<th>'.$data[$x].'</th>' . "\n";
else
echo '<td>'.$data[$x].'</td>' . "\n";
if( $x && !($x % 7) ){
echo '<a href=?>'
}
}
$start++;
echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';

Check if the remainder by 7 of your counter is 0. If it is, then it is a multiple of seven and you can echo your desired string.
if($start % 7 === 0){
echo '<a href=?>'
}

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>';
}
}
}
?>

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

How to find / replace text in CSV and pull it into an HTML table with PHP?

Thanks to some help I got here, I managed to pull specific rows and columns of data from a CSV into an HTML table using PHP.
The working table is here: http://digitaldemo.net/table/table.php
Here is the data in the CSV (screenshot)
https://www.dropbox.com/s/5pa0ruxxnydwhr4/Screenshot%202014-11-17%2012.19.26.png?dl=0
and my code:
<?php
echo "<table cellspacing=\"5\" cellpadding=\"5\">";
echo "<thead><tr align=\"center\">";
echo "<td>Cumulative<br />Total Returns (%) as of [10]</td>";
echo "<td>Quarter</td>";
echo "<td>YTD</td>";
echo "<td>1YR</td>";
echo "<td>3YR</td>";
echo "<td>5YR</td>";
echo "<td>10YR</td>";
echo "<td>Since<br>Inception<sup>1</sup></td>";
echo "</tr></thead>";
$filter = array(
'2' => '1,2,3,4,5,6,7,8',
'3' => '1,2,3,4,5,6,7,8',
'5' => '1,2,3,4,5,6,7,9'
);
if (($handle = fopen("source.csv", "r")) !== FALSE) {
$row = 1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(array_key_exists($row, $filter))
{
$columns = explode(",", $filter[$row]);
echo "<tr align=\"center\">";
for($i = 0; $i <= count($data); $i++)
{
if(in_array($i, $columns))
{
echo "<td>" . $data[$i - 1] . "</td>";
}
}
echo "</tr>";
}
$row++;
}
}
echo "</table>";
?>
My question now is how do I find / replace text in the CSV so that it shows up the way that I want in the table (e.g. changing Row_1 in the table above to Apple)?
Also, how can I auto format any numbers (including those w/ leading zeros) to two decimal places?
Thanks in advance!
Cynthia

list through file using a while loop

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.)

loop not functioning properly

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;

Categories