PHP CSV Output - How to add two rows? - php

Hellos!
Alessandro Minoccheri already helped me alot with this code, but I've got one more problem. The CSV file is a output file from powershell (get-aduser) - it is comma seperated.
As first this is the csv content:
#TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser
name,"officephone"
firstname, lastname,"+49 1234 555 134"
firstname, lastname,"+49 1234 555 242"
firstname, lastname,"+49 1234 555 338"
firstname, lastname,"+49 1234 555 149"
And this is the code for the moment:
$lineCount = 0;
while (($line = fgetcsv($f)) !== false) {
if ($lineCount > 1) {
echo "<tr class='departmenttext'>";
foreach ($line as $key => $cell) {
if ($key == 1) {
$cell = substr($cell, -3);
echo '';
}
echo "<td>" . htmlspecialchars($cell) . "</td>";
if ($key == 1) {
echo "</tr>\n";
}
}
echo "</tr>\n";
}
$lineCount = $lineCount + 1;
}
it displays a CSV file in one row. Means
Name1 Phone1
Name2 Phone2
Name3 Phone3
Name4 Phone4
But as it is sometimes very long I would be in need of the following:
Name1 Phone1 Name5 Phone5
Name2 Phone2 Name6 Phone6
Name3 Phone3 Name7 Phone7
Name4 Phone4
But as I just got one export function "htmlspecialchars($cell) I don't know how to separate it?

It's difficult to wrap the file like you want, but the alternative is to put entries side by side...
$lineCount = 0;
fgetcsv($f);
fgetcsv($f);
while (($line = fgetcsv($f)) !== false) {
if ( count($line)<2 ) {
break;
}
if ($lineCount % 2 == 0) {
echo "<tr class='departmenttext'>";
}
echo "<td>" . htmlspecialchars($line[0].','.$line[1]) . "</td>";
echo "<td>" . htmlspecialchars(substr($line[2],-3)) . "</td>";
if ($lineCount % 2 == 1) {
echo "</tr>\n";
}
$lineCount = $lineCount + 1;
}
if ($lineCount % 2 == 1) {
echo "</tr>\n";
}

First of all you have to decide that how much line you want in one bunch. and according to that number, You need to start a new raw in loop.
for example, if you want to show up to 10 line in one bunch, like
you need something like
if($key%10 == 0){
echo "</tr><tr class='departmenttext'>";
echo "<td>" . htmlspecialchars($cell) . "</td>";
}

You can do something like this way
if($key%count($line) == 0){
echo "</tr><tr class='departmenttext'>";
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
Here I have divided it by total number of line so that it can be calculate 50% of your line.

Related

PHP CSV import, but without first two lines and a cut of infos

Hellos,
I would like to do a simple telephone list. The csv data I do get from a csv list from an active directory powershell export.
the csv is comma seperated and it's looking that way:
#TYPE Selected.Microsoft.ActiveDirectory.Management.ADUser
name,"officephone"
firstnameA, lastnameA,"+49 12345 555 123"
firstnameB, lastnameB,"+49 12345 555 124"
firstnameC, lastnameC,"+49 12345 555 125"
firstnameD, lastnameD,"+49 12345 555 126"
So, and I would like to display it like that way (firstnameA, lastnameA in one row and the three digit number in the second row:
firstnameA, lastnameA 123
firstnameB, lastnameB 124
firstnameC, lastnameC 125
firstnameD, lastnameD 126
till yet I have that code:
<?php
echo "<html><body><table>\n\n";
$phonebook = fopen("database/itlocal.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr class='departmenttext'>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
?>
How can I skip the first two lines and also cut the telephone number I'd like to have it?
thanks in advance
Try this:
$lineCount = 0;
while (($line = fgetcsv($f)) !== false) {
if ($lineCount > 1) {
echo "<tr class='departmenttext'>";
foreach ($line as $key => $cell) {
if ($key == 1) {
$cell = substr($cell, -3);
echo '';
}
echo "<td>" . htmlspecialchars($cell) . "</td>";
if ($key == 1) {
echo "</tr>\n";
}
}
echo "</tr>\n";
}
$lineCount = $lineCount + 1;
}

PHP read file match if statement

everyone I have the following file and I want to show lines that match on if condition and pass the other.
I have this TXT file:
Doc. number|Date|Price|Description|Name
100|11/11/2015|99|Test 1|Alex
101|11/11/2015|120|Test 2
102|11/11/2015|100|Test 3|John
102|11/11/2015|140||
103|11/11/2015|110|Test 4|
And this is my PHP code:
$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('|', $line_of_text);
if($i > 1) { // Pass the first line
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
$i++;
}
fclose($file_handle);
echo "</table>"
How I can check if there are no "Description" and/or "Name" in table and pass this line. I want to show(get) only line that match on if condition.
I will be very grateful if someone have idea. Thanks in advance.
As simple as
$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('|', $line_of_text);
if($i > 1 && !empty($parts[3]) && !empty($parts[4])) { // Pass the first line and lines without description / name
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
$i++;
}
fclose($file_handle);
echo "</table>"
Only print table row if we have name and description:
if($i > 1 && $parts[3] && $parts[4]) {
You can put condition before echo statement and if it will be false just skip "echo";
if (count($parts) === 5) {
$error = 0;
foreach ($parts as $part) {
if (empty($part)) error++;
}
if($i > 1 && $error === 0) {
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
}
I've a solution that can help you.
But why I think you need just scape the heading line. so I changed if($i > 1) to be if($i >0)
$file_handle = fopen("file.txt", "rb");
$i = 0;
echo "<table border='1'>";
echo "<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>";
while (!feof($file_handle)) {
$line_of_text = fgets($file_handle);
$parts = explode('|', $line_of_text);
if($i > 0) { // Pass the first line
if ( (!empty($parts[3])) && (!empty($parts[4])) ){
echo "<tr>";
echo "<td>" . $parts[0] . "</td>"; // Doc. number
echo "<td>" . $parts[1] . "</td>"; // Date
echo "<td>" . $parts[2] . "</td>"; // Price
echo "<td>" . $parts[3] . "</td>"; // Description
echo "<td>" . $parts[4] . "</td>"; // Name
echo "</tr>";
}
}
$i++;
}
fclose($file_handle);
echo "</table>"
Your file has a CSV structure, pipe delimited.
So parse it as a CSV.
Note the using of array_shift to get the header of the CSV and passing the delimiter parameter to fgetcsv.
Also consider using implode instead explicitly passing each member of the array between td tags.
Here's an example using two functions, one for parsing the CSV and returning the data,
and another one for displaying the data and doing the validation.
function getData($file){
$rows = array();
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, null, "|")) !== FALSE) {
$rows[] = $data;
}
fclose($handle);
}
return $rows;
}
function displayData($rows){
$header = array_shift($rows);
$output = '<table border="1">' . PHP_EOL;
$output .= '<tr><th>' . implode('</th><th>',$header) . '</th></tr>' . PHP_EOL;
foreach($rows as $row){
if (isset($row[3]) and isset($row[4]) and $row[3]!='' and $row[4]!=''){
$output .= '<tr><td>' . implode('</td><td>',$row) . '</td></tr>' . PHP_EOL;
}
}
$output .= '</table>';
return $output;
}
$rows = getData("pipe.txt");
print displayData($rows);
This will output the following
<table border="1">
<tr><th>Doc. number</th><th>Date</th><th>Price</th><th>Description</th><th>Name</th></tr>
<tr><td>100</td><td>11/11/2015</td><td>99</td><td>Test 1</td><td>Alex</td></tr>
<tr><td>102</td><td>11/11/2015</td><td>100</td><td>Test 3</td><td>John</td></tr>
</table>

PHP CSV dynamic table want to make all entries in fifth column links

I have a dynamic table created from my PHP script. The information is sourced from a CSV file. I would like everything in the fifth column to be a link to another page. So that would be every array index/key 4 is a link. Unsure on how i would modify the code below to achieve this?
echo "<html><body><table>\n\n";
echo "<tr><td>First</td><td>Last</td><td>Email<td>Address</td
<td>File</td></tr>";
$f = fopen("filelog.csv", "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
Add a counter to your loop and look for every 5th iteration.
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
$i = 0;
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
$i++;
if($i % 5 === 0) {
echo "<td>" . yourlinkstuff . "</td>";
}
}
echo "</tr>\n";
}
I am using modulus because it's unclear from your question whether or not there are more than 4 "cells" per "line". If the foreach($line as $cell) always iterates 4 times, you don't need a counter. You could just tag on the 5th column after that loop, like so:
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "<td>" . yourlinkstuff . "</td>";
echo "</tr>\n";
}
You could add a counter and when you reach the 4th element, output a link.
$i = 1;
foreach ($line as $cell) {
if ($i == 4) {
// Create the link.
echo "<td> </td>";
}
else {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
$i++;
}
Thank you all for your input, I really appreciate it. I have a table that has five columns, "First, Last, Email, Address, File". Everything in the "File" column is a link to that file. So yes in the array starting at index 0 the "File" data will be at index 4. I didn't want an extra column. This is code that ended up doing the job, thank you!!
echo "<html><body><table>\n\n";
echo "<tr><td>First</td><td>Last</td><td>Email<td>Address</td>
<td>File</td></tr>";
$fp = fopen("filelog.csv", "r");
while (($line = fgetcsv($fp)) !== false) {
echo "<tr>";
$i=1;
foreach ($line as $cell) {
if(($i % 5) == 0) {
echo "<td><a href=" . htmlspecialchars($cell) . ">"
.htmlspecialchars($cell) . "</a></td>";
}
else {
echo "<td>" . htmlspecialchars($cell) . "</td>";
$i++;
}
}
echo "</tr>\n";
}
fclose($fp);
echo "\n</table></body></html>";

How to get td data from a table in PHP

I am currently stuck at this problem right now where I don't seem to have any idea how to implement this. I have this table which is populated from a csv file in php and I want to get the data logged in order to calculate the daylight hours.
How can I get the data from the td "dateLogged", I did that in jquery before using children but I do not seem to be able to do that in php
//print out uploaded file.csv
echo "<html><body><table BORDER=1>\n\n";
echo " <tr><th>AvgCrown</th><th>MinCrown</th><th>MaxCrown</th><th>dateLogged</th><th>DaylightHours</th></tr>";
$f = fopen("./uploads/" . $_FILES["uploadedfile"]["name"], "r");
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "<td>" . calcDaylight() . "</td>";
echo "<tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
Here is a sample how the table looks like
Any help will be most welcome.
There's no need to parse the HTML if you can just grab the data while you're generating it.
Something like this should be all you need:
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
// Save the value before you output
$lastColValue = htmlspecialchars($cell);
echo "<td>" . $lastColValue . "</td>";
}
// If you want to store all log dates in an array:
$logDates[] = $lastColValue;
// Or if you want to do something with that value in calcDaylight:
echo "<td>" . calcDaylight( $lastColValue ) . "</td>";
echo "<tr>\n";
}
fclose($f);
Incidentally, if you do find you need to parse the HTML and are only familiar with jQuery, this library may strike your fancy:
http://code.google.com/p/phpquery/
this?
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo "<td>".htmlspecialchars($cell)."</td>";
}
echo "<td>" . calcDaylight( $cell[3] ) . "</td>";
echo "<tr>\n";
}
i might be wrong but 4th column is the field you want, no?

foreach plus for in php

I would like make:
aaa | bbb | ccc | ddd etc
1 | 1 | 1 | 1
2 | 2 | 2 | 2
3 | 3 | 3 | 3
etc
for aaa, bbb etc i use FOREACH
<table><tr>
foreach ($data as $d){
echo "<td>" . $d . "</td>";
}
</tr>
for ($i = 0; $i < 20; $i++){
echo "<tr><td>" . $i . "</td></tr>";
}
but this working not ok. how can i use loop FOR for all data from foreach?
I think you can generate your table like this:
$columns = array('aaa','bbb','ccc','ddd');
$num_cols = count($columns);
echo "<table>";
echo "<tr>";
foreach($columns as $col)
{
echo "<td>$col</td>";
}
echo "</tr>";
for($i=1;$i<20;$i++)
{
echo "<tr>";
for($j=0;$j<$num_cols;$j++)
{
echo "<td>$i</td>";
}
echo "</tr>";
}
echo "</table>";
In general depends on what $data looks like.
<?php
$data = array('aaa', 'bbb', 'ccc', 'ddd'); // Assuming that $data is a columns storage
$rows = 10; // $rows = count($data); if you wish to have same number of columns and rows
echo '<table>';
echo '<tr>';
foreach ($data AS $item)
{
echo '<td>' . $item . '</td>';
}
echo '</tr>';
for ($idx = 0; $idx < $rows; $idx++)
{
echo '<tr>';
for ($col = 1, $col_num = count($data); $col <= $col_num; $col++)
{
echo '<td>' . $idx . '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
P.S. haven't tested the code.

Categories