Pass php drop down value to another php site - php

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!

Related

PHP CSV showing in table

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>";

Read and load two CSV files

I have two very small CSV files that I want to load into array and compare each row and their individual cells, and if there is a change in any cell highlight it.
At the moment I am writing a script to load files and display these onto the browser as they appear on the files.
CSV structure:
My PHP Code:
<?php
$row = 1;
$row2 = 1;
if (($file = fopen("Workbook1.csv", "r")) !== FALSE && ($file2 = fopen("Workbook2.csv", "r")) !== FALSE ) {
while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row Orginal File: <br /></p>\n";
$row++;
for ($i=0; $i < $num; $i++) {
echo $data[$i] . "<br />\n";
}
while(($data2 = fgetcsv($file2, 1000 , ",")) !== FALSE){
$num2 = count($data2);
echo "<p> $num2 fields in line $row2 Updated File: <br /></p>\n";
$row2++;
for ($x=0; $x < $num2; $x++) {
echo $data2[$x] . "<br />\n";
}
}
}
fclose($file);
fclose($file2);
}
?>
Browser Result:
Not to sure why the Array is structured like above image as i understand fgetcsv() read line by line.
Any one can see my my stake in code..?
You are not closing the first while loop in the right place.
Try this
<?php
$row = 1;
$row2 = 1;
if (($file = fopen("Workbook1.csv", "r")) !== FALSE && ($file2 = fopen("Workbook2.csv", "r")) !== FALSE ) {
while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row Orginal File: <br /></p>\n";
$row++;
for ($i=0; $i < $num; $i++) {
echo $data[$i] . "<br />\n";
}
} // end first while loop
while(($data2 = fgetcsv($file2, 1000 , ",")) !== FALSE){
$num2 = count($data2);
echo "<p> $num2 fields in line $row2 Updated File: <br /></p>\n";
$row2++;
for ($x=0; $x < $num2; $x++) {
echo $data2[$x] . "<br />\n";
}
}
fclose($file);
fclose($file2);
}
?>
After reading this enter link description here
I managed to fix it by adding this line of code at the top of the file before fopen()
ini_set('auto_detect_line_endings',TRUE);
If you need to set auto_detect_line_endings to deal with Mac line
endings, it may seem obvious but remember it should be set before
fopen, not after:
Having this fixers how can i Compare the values between these two files..?

Read CSV file in php

PHP file
$teste = fopen("teste.csv", "r");
print "<table border=\"2\">";
while (!feof($teste) ) {
$line_of_text = fgetcsv($teste);
print "<tr>
<td>$line_of_text[0]</td>
<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>
<td>$line_of_text[10]</td>
<td>$line_of_text[11]</td>
</tr>";
}
print "</table>";
fclose($teste);
CSV file
,912,Gold,Sat Bodegas Noroeste de La Palma,Vega Norte Albillo Criollo Seco,2013,Dry White,13,5,97,D.O.La Palma - Islas Canarias,
,922,Gold,Sat Bodegas Noroeste de La Palma,Acertijo Blanco Seco,2013,Dry White,14,95,D.O.La Palma - Islas Canarias,
im not an PHP expert but i need to create a simple system to read data form a CSV and create a table with the result
and i tried the code above, but i only get the 1st line
can anyone tell me whats wrong and how i can fix it?
Read and print the entire contents of a CSV file
<?php
echo "<table>";
$row = 1;
if (($handle = fopen("teste.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
//echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
echo "<tr>";
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
echo "<td>".$data[$c]."</td>";
}
echo "</tr>";
}
fclose($handle);
}
echo "</table>";
?>

how to query a csv value in php?

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>

Create HTML table from multiple lines of strings

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;

Categories