I am trying to use fgetcsv() to grab data from a CSV file to a HTML table.
I am able to put the data into the HTML table, but I would like the table to have two columns, and separated by a "|" character.
I have the line :
fgetcsv($file, 999, "|");
However, the delimiter seems to be ignored and everything is input into one column?
this is my code:
<table>
<?php
$file = fopen("WSfile.csv", "r");
while (! feof($file)){
$lines = fgetcsv($file, 999, "|");
echo "<tr>";
foreach((array) $lines as $line){
echo "<td>" . $line . "</td>";
}
echo"</tr>";
}
fclose($file);
?>
</table>
Related
I have a CSV that is appended every 10 minutes with Data that looks similar to
07-01-2020 10:40 https://www.google.com OK 0.080382
07-01-2020 10:40 https://www.yahoo.com OK 0.120117
The first column being date shown as 07-01-2020 in this example,
time represented by the 10:40, Link represented by https://www.google.com, Status represented by OK and latency represented by 0.080382
I currently have all the results being displayed with the following bit of code:
<?php
$file = fopen("resultsTime.csv", "r");
//Output lines until EOF is reached
while(! feof($file)) {
$line = fgets($file);
echo $line. "<br>";
}
fclose($file);
?>
This works perfectly fine as it should. However I am looking to instead enter say https://www.google.com into a text box and get the history for that site. An additional piece for that would be I only want when the status is not OK.
Hopefully that is well enough explained. I have seen some examples on here but none that offer enough detail to be able to create my own.
Thank you for your help.
Indents are a little messy here but this is the solution I came up with that works great.
Should be easily modifiable if others are looking to do the same
<center>
<form action="" method="POST">
<label>Site</label><input type="text" name="search">
<input type="submit" name="submit" value="search">
</form>
</center>
<center>
<?php
if (isset($_POST['submit'])) {
$word = $_POST['search'];
print "<tr><td><u>Date</u><td><u>Time</u></td><td><u>Site</u></td><td><u>Status</u></td><td><u>Latencey</u></td></tr>";
if (($h = fopen("resultsTime.csv", "r")) !== FALSE) {
// Convert each line into the local $data variable
while (($data = fgetcsv($h, 1000, ",")) !== FALSE) {
foreach ($data as $str) {
$site = explode(" ", $str);
if ($site[3] != "OK" && $site[2] == $word) {
print "<tr><td>" . $site[0] . "</td>";
print "<td>" . $site[1] . "</td>";
print "<td>" . $site[2] . "</td>";
print "<td>" . $site[3] . "</td>";
print "<td>" . $site[4] . "</td></tr>";
}
}
}
// Close the file
fclose($h);
}
}
?>
I really need your help.
I´m using this code to write into text file from form:
$data = $_POST['jmeno'] . "\t" . $_POST['prijmeni'] . "\t" . $_POST['ulice'] . "\t" . $_POST['cislo_popisne'] . "\t" . $_POST['mesto'] . "\t" . $_POST['psc']. "\n";
$myfile = fopen("zakaznici.txt", "ab") or die("Unable to open file!");
fwrite($myfile, $data);
fclose($myfile);
This is my code to read from text file:
$filename = 'zakaznici.txt';
$handle = fopen($filename, 'r');
$datain = fread($handle, filesize($filename));
$array = explode("\t",$datain);
echo "<table class='styl'><th>Jméno</th><th>Příjmení</th><th>Ulice</th><th>Číslo popisné</th><th>Město</th><th>PSČ</th><tr>".implode("</tr><tr>",array_map(function($a) {return "<td>".implode("</td><td>",explode("\t",trim($a)))."</td>";},explode("\n",$datain)))."</tr></table>";
And in table it looks like this:
Table
Any advice how to sort it alphabetically? I mean for example, when I choose "Jmeno" in select option a hit "Odeslat", I will get sorted table by column "Jmeno" like this (ivana, stepan, tomas).
When using the explode function, it puts all the values in an array. What you can do before echoing the data, is sorting the array.
https://secure.php.net/manual/en/function.sort.php
$data = "atest\tctest\tptest\tqtest\tbtest\tltest";
$values = explode("\t", $data);
sort($values, SORT_STRING);
echo "<table class='styl'><th>Jméno</th><th>Příjmení</th><th>Ulice</th><th>Číslo popisné</th><th>Město</th><th>PSČ</th>";
echo "<tr>";
foreach($values as $value)
{
echo "<td>" . $value . "</td>";
}
echo "</tr></table>";
Result:
Jméno Příjmení Ulice Číslo popisné Město PSČ
atest btest ctest ltest ptest qtest
I´m getting this.
table
Really don´t know how to do it.
I am really a newbie in php. I have a problem in doing this..
I have sample.csv file contains 3 rows: inbound(1st row), outbound(2nd row), and date(3rd row).
sample.csv
**inbound** **outbound** **date**
IN/15#001234 OUT/000000163-000000as 1/12/2014
IN/15#004323 NOT/000000141-00000043 1/14/2014
IN/15#005555 OUT/000000164-000000jk 1/15/2014
is it possible to display the all columns where 2ndrow is start with "NOT" and a number before char "-" is 141???
output:
IN/15#004323 NOT/000000141-00000043 1/14/2014
i dont know if it is possible... please help me..
I have a code below. But it only open the csv file...
$file = fopen('Master.csv', 'r');
echo "<table style='border: 2px solid black; text-align:left'>";
while (($line = fgetcsv($file)) !== FALSE) {
list($inbound, $outbound, $date) = $line;
echo "<tr>";
echo "<td>$inbound</td>";
echo"<td>$outbound</td>";
echo "<td>$date</td>";
echo "</tr>";
}
echo "</table>";
is it possible to display the all columns where 2ndrow is start with "NOT" and a number before char "-" is 141???
Inserting
if (preg_match('/^NOT/', $outbound)) continue;
after the list()... statement should be sufficient.
But your data does not look like being comma-seperated, rather than tab-seperated. And perhaps you mean columns when talking about rows at the beginning?
You can use strpos()
if ( strpos($outbound, 'NOT') !== false ) {
// "NOT" WORD FOUND IN STRING
}
Try this out. This will work with comma separated csv file.
echo "<table border = 1><tr><td>first</td><td>second</td><td>third</td></tr>"; //creating table
$handle = fopen('fe.csv', "r"); //open csv file
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) //read csv file row by row
{
//check both NOT and 141- in the string
if ( (strpos($data[1], 'NOT') !== false ) && (strpos($data[1], '141-') !== false )) {
//add required field data to table
echo "<tr>";
echo "<td>".$data[0]."</td>";
echo"<td>".$data[1]."</td>";
echo "<td>".$data[2]."</td>";
echo "</tr>";
}
}
echo "</table>"; //close table
?>
I'm loading some columns from a csv file using php.
I am using divs to display the columns, but the issue is that the contents of the div for each heading is not perfectly aligned underneath the heading.
this is my php code:
<?php
$file_handle = fopen("myCSV.csv", "r");
while (!feof($file_handle) ) {
//print '<tr>';
$line_of_text = fgetcsv($file_handle, 1024);
print "<div style='float:left; margin-right:15px;'>".$line_of_text[2].' '.$line_of_text[3].' '.$line_of_text[4].' '.$line_of_text[5].' '.$line_of_text[6].' '.$line_of_text[7].' '.$line_of_text[8].' '.$line_of_text[9]."</div><br /> ";
}
fclose($file_handle);
?>
How can I align the columns under each related headings properly?
here is the output data:
http://jsfiddle.net/w9v5q8vp/
This is how I tried to use a table:
print "<table>\n";
print "<tr><td>".$line_of_text[2]."</td></tr><tr><td>".$line_of_text[3]."</td></tr>";
print '</table>';
When you output a table you should loop only over the rows:
$file_handle = fopen("myCSV.csv", "r");
print "<table>\n";
while (!feof($file_handle) ) {
//print '<tr>';
$line_of_text = fgetcsv($file_handle, 1024);
print "<tr><td>".$line_of_text[2].'</td><td>'.$line_of_text[3].'</td><td>'.$line_of_text[4].'</td><td>'.$line_of_text[5].'</td><td>'.$line_of_text[6].'</td><td>'.$line_of_text[7].'</td><td>'.$line_of_text[8].'</td><td>'.$line_of_text[9]."</td></tr>\n";
}
print '</table>';
fclose($file_handle);
The beginning/end table tags should be printed only once.
You can read more about table tr td tags at w3schools.
I am using file_get_contents() to import a text file.
In the text file, the format goes as below (example):
3434,83,8732722
834,93,4983293
9438,43933,34983
and so forth... basically it follows the pattern: integer, comma to split it, second integer, another comma to split it, third integer, then a new line begins. I need to get this into a table with the format following accordingly. So in other words, I would have a 3 column table and each new line in the text file would be a new row in the table.
This must be transcoded into a simple html table with <table> <tr> and <td>
I have never worked with multidimensional arrays and splitting text with that. This is why I'm seeking assistance. I really appreciate it! :)
You can do following :
$filename = 'abc.txt';
$content = file_get_contents($filename);
$explodedByBr = explode('<br/>', $content);
$table = "<table border='1'>";
foreach ($explodedByBr as $brExplode) {
$explodedByComma = explode(',', $brExplode);
$table .= "<tr>";
foreach ($explodedByComma as $commaExploded) {
$table .= "<td>" .$commaExploded. "</td>";
}
$table .= "<tr/>";
}
$table .= "</table>";
echo $table;
abc.txt has data in following format :
3434,83,8732722
834,93,4983293
9438,43933,34983
<?php
$file = 'path/to/file.txt';
echo '<table>';
while(!feof($file)) {
$line = fgets($file);
echo '<tr><td>' . implode('</td><td>',explode(',',$line)) . '</td></tr>';
}
echo '</table>';
?>
Try this:
Read the file into an array and then column'ize it by processing each line of the array by passing it through array_walk.
<?php
function addElements( &$v, $k ) {
$v1 = explode( ',', $v ); // break it into array
$v2 = '';
foreach( $v1 as $element ) {
$v2 .= '<td>'.$element.'</td>';
// convert each comma separated value into a column
}
$v = '<tr>'.$v2.'</tr>'; // add these columns to a row and return
}
// read the whole file into an array using php's file method.
$file = file( '1.txt' );
// now parse each line of the array so that we convert each line into 3 columns.
// For this, i use array_walk function which calls a function, addElements,
// in this case to process each element in the array.
array_walk( $file, 'addElements' );
?>
<html>
<head></head>
<body>
<table border="0">
<?php echo implode('',$file);?>
</table>
</body>
</html>
Hope it helps. See the php doc for file and array_walk. These are simple and convenient functions.