I want to show this CSV File the same way as the picture but then in PHP.
Right now I know how to show it if they were all under "A",
but I'm stuck now on how to show it with more then one cell.
<?php
echo "<table border='1'>";
if (($handle = fopen("top10.csv", "r")) !== FALSE) {
$data = fgetcsv($handle, 0, ",");
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
echo "<tr>";
$num = count($data);
for ($i=0; $i < $num; $i++) {
echo"<td>" . $data[$i] . "<br /> </td>";
}
echo "</tr>";
}
fclose($handle);
}
?>
This is how I did it with 2 rows.
Image:
Result right now:
You might want to use SplFileObject which can parse CSV files. For example:
$file = new SplFileObject("/path/to/your/csv/file.csv");
$file->setFlags(SplFileObject::READ_CSV);
$rows = "";
foreach ($file as $row) {
list ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l) = $row;
$rows .= <<<HTML
<tr>
<td>${a}</td>
<td>${b}</td>
<td>${c}</td>
<td>${d}</td>
<td>${e}</td>
<td>${f}</td>
<td>${g}</td>
<td>${h}</td>
<td>${i}</td>
<td>${j}</td>
<td>${k}</td>
<td>${l}</td>
</tr>
HTML;
}
echo "<table>".$rows."</table>";
PHP Documentation
you read file 2times in your code:
$data = fgetcsv($handle, 0, ",");// 1st (just remove this line)
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { // 2nd
and another solution
try this:
echo "<table border=\"1\">";
$row=#explode("\n",#file_get_contents('test.csv'));
if(count($row)>0) foreach($row as $r){
$col=#explode(",",$r);
echo "<tr>";
foreach($col as $c){
echo "<td>$c</td>";
}
echo "</tr>";
}
echo "</table>";
Related
I am new to php, and would like to pass a drop down menu value to another php file. Below is the code I have pieced together so far.
Drop down php (Lists all files in a directory, although i'd like to omit the folders and the "." & ".."):
<?php
$currentdir = 'data/';
$dir = opendir($currentdir);
echo 'Files are as follows:<br>';
echo '<form action="SelectedDate.php" method="POST">';
echo '<select name="selectedfile" onchange="this.form.submit()">';
while($file = readdir($dir))
{
echo '<option value="'.$file.'">'.$file.'</option>';
}
echo '</select>';
echo '</form>';
closedir($dir);
?>
Table generating php, which is where i'd like to pass the file name too so it can pull up the selected csv file,formatted to table. :
<?php
$var=$_REQUEST['selectedfile'];
$row = 1;
if (($handle = fopen('"data/"'$var'"', "r")) !== FALSE) {
echo '<head>';
echo '<script src="js/sorttable.js"></script>';
echo '</head>';
echo '<table class="sortable" 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);
}
?>
I cant seem to get the drop down value to pass into the fopen() function...
Any help and critiquing would be great!
For readability I would prefer $_POST instead of $_REQUEST, besides that you have an error:
(($handle = fopen('"data/"'$var'"', "r"))
//SHOULD BE:
(($handle = fopen("data/".$var.", "r"))
======
Updated answer:
Make sure your files are both saved with an .php extension. Besides that, add the following lines of code to the top of your second file:
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
As a finishing try, use this code in the if
(($handle = fopen("data/{$var}", "r"))
Change this line:
if (($handle = fopen('"data/"'$var'"', "r")) !== FALSE) {
Into
if (($handle = fopen('"data/"'.$var.'"', "r")) !== FALSE) {
Note the dots!
I have this csv file which has 2 rows.
The first one shows the city name and 2nd shows a number (statistic)
Example:
[City] [Count of dogs]
[Los Angeles] [100]
[New York] [-]
Now my problem is that I don't want to show the city's and the statistic from it that have [-] as [Count of dogs].
This is what I've tried right now:
<?php
echo"<table border='1'>";
if (($handle = fopen("table.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
echo "<tr>";
$num = count($data);
for ($i=0; $i < $num; $i++) {
$string = $data[$i];
$m = preg_match("#\w{2,}#", $string, $match);
if ($m) {
echo "<td>". $data[$i];
$m = preg_match("#\d{2}#", $string, $match);
if ($m) {
echo $data[$i] . "<br /> </td>";
}
}
}
echo"</tr>";
}
fclose($handle);
}
echo"</table>";
?>
It doesn't show the "-" right now but it does show the city, I was wondering if a "and-statement" exist for expressions, or is there a different way to do this?
Picture of how it looks right now: [IMG]http://i59.tinypic.com/14sl4xg.jpg[/IMG]
IS it just "-"? Then you can check for it...
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if ($data[1] != "-") {
echo "<tr>";
....
echo "</tr>";
}
}
If, count of dog field is the filtering field, then take that field first before display,
Here I have checked for numeric, because the count of dog is always in numeric, You may use any other logic as you have done using preg_match,
<?php
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if(is_numeric($data[1])) {
//proceed your logic here
}
else {
continue;//to go for the next row
}
?>
Assuming your CSV looks like your example, you can skip the rows where "Number of Dogs" is a dash.
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if( $data[1] == '-' ) {
continue;
}
echo "<tr>";
// ...
}
Or if the dash can also be any other non-number:
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if( !is_numeric($data[1]) ) {
continue;
}
echo "<tr>";
// ...
}
Also, I don't know what the other if checks are for, but it looks to me you want to just echo the data
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
if( $data[1] == '-' ) {
continue;
}
echo "<tr>";
$num = count($data);
for ($i=0; $i < $num; $i++) {
echo "<td>" . $data[$i] . "</td>";
}
echo"</tr>";
}
Check the occurrences of - in the second column using strpos function. If the second column doesn't contain any -, then print the row
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
$num = count($data);
if (strpos($data[$num-1],'-') === false) {
echo "<tr>";
for ($i=0; $i < $num; $i++) {
$string = $data[$i];
$m = preg_match("#\w{2,}#", $string, $match);
if ($m) {
echo "<td>". $data[$i];
$m = preg_match("#\d{2}#", $string, $match);
if ($m) {
echo $data[$i] . "<br /> </td>";
}
}
}
echo"</tr>";
}
}
I'm currently using this script:
<?php function jj_readcsv($filename, $header=false) { $handle = fopen($filename, "r"); echo '<table>'; //display header row if true if ($header) {
$csvcontents = fgetcsv($handle);
echo '<tr>';
foreach ($csvcontents as $headercolumn) {
echo "<th>$headercolumn</th>";
}
echo '</tr>'; } // displaying contents while ($csvcontents = fgetcsv($handle)) {
echo '<tr>';
foreach ($csvcontents as $column) {
echo "<td>$column</td>";
}
echo '</tr>'; } echo '</table>'; fclose($handle); } jj_readcsv('partitiontable.csv',true);
?>
I have a CSV table with 8 columns.
I would like to know if it possible to get only on element of the column when making a query. Here is what look like my CSV :
"HEMSI, Alberto",P_000001,P_1,Partition,169864,"Hemsi, Myriam","HEMSI, Alberto","Una matica de ruda",
"HEMSI, Alberto",P_000002,P_2,Partition,169865,"Hemsi, Myriam","HEMSI, Alberto","Ya salio de la mar",
the idea is :
query a value for example: P_1
and get in result: Una matica de ruda
For each line, when asking P_1 getting the value which is in the 8 column.
And the same for P_2
Yes, this is possible using two methods:
At the beginning create an associative array and insert as key the 4th column, and as value with 8th column. Then use this as a lookup table in all further code.
If you don't do 1. , you can make a for loop that goes through the CSV data and looks with IF that the 4th row value is the one you are looking for. When found, return the 8th column value.
Example of first approach with associative array
<?php
$array = array();
jj_readcsv("csv.txt");
echo $array["P_1"]."<br/>";
echo $array["P_2"]."<br/>";
function jj_readcsv($filename, $header = false)
{
global $array;
$row = 1;
if (($handle = fopen($filename, "r")) !== FALSE) {
echo '<table>'; //display header row if true if ($header) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//print_r($data);
$num = count($data);
echo '<tr>';
for ($c=0; $c < $num; $c++) {
if($row > 1)
echo "<td>$data[$c]</td>";
else
echo "<th>$data[$c]</th>";
if($c==2)
$array[$data[$c]]= $data[7];
}
echo '</tr>';
$row++;
}
fclose($handle);
}
}
?>
Try this:
$row = 1;
$handle = fopen ("teste.csv","r");
$head = Array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//register head rows with names
if($row == 1){
$num = count ($data);
for ($c=0; $c < $num; $c++) {
$colName = trim($data[$c]);
$head[$colName] = $c;
}
}else{
echo $data[$head['COL_NAME']];
}
$row++;
}
fclose ($handle);
This will make var $head an associative array where the key is the col name and value his position in csv, after that you can easy identify what col you want.
Even if you named your function jj_readcsv(), if you look more closely, this name is a liar, the function does much more: opening a file, reading the lines.
If you don't want to separate the concerns but also easily modify it, just make the actual reading method optional:
function jj_readcsv($filename, $header = FALSE, callable $fgetcsv = NULL)
########################
{
$fgetcsv || $fgetcsv = 'fgetcsv';
#################################
$fileHandle = fopen($filename, "r");
if (!$fileHandle) {
return;
}
$csvLine = $fgetcsv($fileHandle);
#####################
echo '<table>';
echo '<tr>';
foreach ($csvLine as $headercolumn) {
echo "<th>$headercolumn</th>";
}
echo '</tr>';
while ($csvLine = $fgetcsv($fileHandle)) {
#####################
echo '<tr>';
foreach ($csvLine as $column) {
echo "<td>$column</td>";
}
echo '</tr>';
}
echo '</table>';
fclose($fileHandle);
}
This little modification allows you to specify the reading function, therefore you can control what is read. So you can inject it as third parameter, for example to only return the eighth column:
jj_readcsv('test.csv', FALSE, function($fileHandle) {
if ($csvLine = fgetcsv($fileHandle))
{
$csvLine = [$csvLine[7]];
}
return $csvLine;
});
Output:
<table><tr><th>Una matica de ruda</th></tr><tr><td>Ya salio de la mar</td></tr></table>
I want to take a text file formatted like so:
*note I can change the format of the stored messages, basically there are delimiters and different lines
;68.229.164.10:4/5/2013:Hello
;71.73.174.13:4/6/2013:Oh Hey
(;IPADDRESS:TIMESTAMP:MESSAGE)
and put it in a table that looks like so:
IP Time Message
68.229.164.10 4/6/2013 Hello
71.73.174.13 4/6/2013 Oh Hey
I prefer to use something like the following:
http://php.net/manual/en/function.fgetcsv.php
And then format the output accordingly.
From Example 1 on the above referenced page:
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
so, you could do something like this...
<?php
$row = 1;
if (($handle = fopen("path_to_your_data_file", "r")) !== FALSE) {
echo '<table>';
echo '<tr><td></td>IP<td>Time</td><td>Message</td></tr>';
while (($data = fgetcsv($handle, 1000, ":")) !== FALSE) {
$num = count($data);
$row++;
if ($num > 2) {
echo '<tr>';
for ($c=0; $c < $num; $c++) {
echo '<td>'.$data[$c].'</td>';
}
echo '</tr>';
}
}
echo '</table>';
fclose($handle);
}
?>
$a=";68.229.164.10:4/5/2013:Hello
;71.73.174.13:4/6/2013:Oh Hey";
preg_match_all('{;(.*?):(.*?):(.*)}',$a,$d);
//set th
$d[0][0]='IP';
$d[0][1]='TIME';
$d[0][2]='Message';
$table = '<table>'.PHP_EOL;
foreach($d AS $tr){
$row = PHP_EOL;
foreach($tr AS $td){
$row .= "<td>{$td}</td>".PHP_EOL;
}
$table .= "<tr>{$row}</tr>".PHP_EOL;
}
$table .= "</table>".PHP_EOL;
echo $table;
Im trying to figure out how to take the data returned by fgetcsv, and format it into a readable/editable table, and then use fputcsv to save that table
so far i have this
<?php
$row = 1;
$handle = fopen("csv.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "\n";
}
}
fclose($handle);
?>
The following code will output the CSV in an html table. To make it editable wrap the echo ..$val.. with tags and add a php form handler that takes the result and reforms a CSV
<?php
$row = 1;
$handle = fopen("csv.csv", "r");
echo("<table>");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo("<tr>\r\n");
foreach ($data as $index=>$val) {
echo("\t<td>$val</td>\r\n");
}
echo("</tr>\r\n");
}
echo("</table>");
fclose($handle);
?>