I have a simple CSV table
Name,Year of birth,City
Yeremy,1980,New York
Louis,1982,Washington
Marta,1987,Chicago
David,1985,Los Angeles
Also, I have this code to can get cell value by number of row and number of column:
$trow = 3;
$tcolumn = 3;
$output .= '<span>';
$row = 1;
$mycsvfile = array();
if (($handle = fopen($abspath, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
$mycsvfile[] = $data;.
}
fclose($handle);
}
$output .= $mycsvfile[$trowN][$tcolumnN];
$output .= '</span>';
I want to define that $trow=Marta and $tcolumn=City, so, in this case to get value "Chicago"...
The code could be optimized a lot, but with the current code:
$output .= array_column($mycsvfile, null, 0)['Marta'][2];
Re-index the array on the values in the Name (column 0)
Access the one with index Marta, getting the value in the City (column 2)
You can access by column name if you combine the column names:
if (($handle = fopen($abspath, "r")) !== FALSE) {
$columns = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$mycsvfile[] = array_combine($columns, $data);
}
fclose($handle);
}
$output .= array_column($mycsvfile, null, 'Name')['Marta']['City'];
Keep in mind that if Marta occurs more than once in the Name column then the LAST one will be returned.
Related
Am reading a CSV file using PHP, i get all the values for each row but some columns having long data displays something like this 9.21008E+15
I don't know how to get the complete value which is suppose to be this 9210080000000000
if (($handle = fopen("Test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
$tb_row = "";
for ($c=0; $c < $num; $c++) {
$tb_row .= $data[$c].",";
}
$new_row = substr($tb_row, 0, -1);//removing the comma at the end of line...
//explode and assign values then insert
$each_col = explode(',', $new_row);
$Device_state = $each_col[0];
$udid = $each_col[8];
$imsi = $each_col[9];
$imei = $each_col[10];
echo "$imsi || $imei"."<br />";
}
}
Adding a prefix to the cells having this issue solved the issue (')
You need just convert it back into integer value
if (($handle = fopen("Test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
$tb_row = "";
for ($c=0; $c < $num; $c++) {
if (strpos($data[$c], 'E+'))
$data[$c] = intval((float)$data[$c]);
$tb_row .= $data[$c].",";
}
$new_row = substr($tb_row, 0, -1);//removing the comma at the end of line...
//explode and assign values then insert
$each_col = explode(',', $new_row);
$Device_state = $each_col[0];
$udid = $each_col[8];
$imsi = $each_col[9];
$imei = $each_col[10];
echo "$imsi || $imei"."<br />";
}
}
Pretty new to PHP. I have a csv feed that i am trying to display every line of in the right place. The feed is loaded into a multidimentional array and I can echo out specific places like this.
echo $readcsv[0][2]
But I am trying to use the value of the variable $row in stead of the first number while doing a while loop.
Something like:
echo $readcsv[$row][2]
I have tried next(), str_replace() and strtr() but none of them seems to work while the loop is running.
$row = 1;
$readcsv = array();
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
echo $readcsv[$row][2];
$row++;
for ($c=0; $c < $num; $c++) {
}
$mycsvfile[] = $data;
}
fclose($handle);
}
CSV file:
title;image_link;link;empty;price;
1;back.gif;http://link.com;;19.95;
2;back.gif;http://link.com;;19.95;
3;back.gif;http://link.com;;19.95;
4;back.gif;http://link.com;;19.95;
5;back.gif;http://link.com;;19.95;
I am not quite sure from your code and question exactly what you are trying to do, but maybe its something like this
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
// you load the line into $data and its a one dimentional array
// occurance 2 is the `link` (http://link.com) if that what you want to echo
echo $data[2];
// as you have a loop, maybe you were trying to
// echo each fields from the csv so you can do that like this
foreach ($data as $idx => $value) {
echo "Column $idx = $value";
}
$mycsvfile[] = $data;
}
fclose($handle);
}
How do I display lines in reverse order?
CSV file:
column1;column2;column3
cell1;cell1;cell1
cell2;cell2;cell2
cell3;cell3;cell3
It should appear like this:
column1;column2;column3
cell3;cell3;cell3
cell2;cell2;cell2
cell1;cell1;cell1
Code:
if (($handle = fopen($path, 'r')) !== FALSE)
{
echo '<table class="table table-striped table-bordered"><thead>';
// Get headers
if (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
}
echo '</thead><tbody>';
// Get the rest
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
echo '<tr><td>'.implode('</td><td>', $data).'</td></tr>';
}
fclose($handle);
echo '</tbody></table>';
}
Thanks in advance.
Collect first
$collect = array();
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
$collect[]= '<tr><td>'.implode('</td><td>', $data).'</td></tr>';
}
echo implode(PHP_EOL,array_reverse($collect));
Reverse the array at the end.
Here what you need to do: instead of direct echoing row - store it to a variable:
// Get the rest
$rest = '';
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
// main trick here - add every new row BEFORE old ones
$rest = '<tr><td>'.implode('</td><td>', $data).'</td></tr>' . $rest;
}
// echo gathered data
echo $rest;
I'm trying to take a file of id numbers and find the names associated with those numbers from a different file and print a csv in the form id,name.
The Def.csv file is in the form
1;128;34;64;Uppland,
2;0;36;128;Östergötland,
3;128;38;192;Småland,
WIth the first number being the ID and the last field the name.
The IDs.csv is just IDs in no specific order with a comma and new line. ie.
12, \n
Here's the (updated, bad) script:
<?php
$ID;
$names;
$both = array();
$row = 0;
echo ("1\n");
if (($handle = fopen("IDs.csv", "r+")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$ID[$row] = $data[0];
$row++;
//print_r($ID);
}
}
print_r($ID);
fclose($handle);
if (($handle = fopen("Defs.csv", "r+")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$Names = $data[4];
// DEBUG, works// echo "$Names";
}
}
fclose($handle);
for ($c = 0; $c < $row; $c++)
{
$Both[$ID[$c]] = $Names[$ID[$c]];
}
foreach ($Both as $key => $value)
{
echo "$key,$value\n";
}
?>
This is my current output
Array
(
[0] => 2
[1] => 3
[2] => 1
)
2,t
3,l
1,u
for the small test case here it should be 2,Östergötland 3,Småland and 1,Uppland
I have a CSV defined like the data this.
"PID", "FName", "LName", "Email"
2425751712402934017,
1037862, "Jason", "Van Hooser", "jvanhooser#example.com"
961741, "Alana", "Traxler", "atraxler#example.com"
1100854, "Emily", "Walcheck", "ewalcheck#example.com"
1166892, "Mary", "Thomas", "mthomas#example.com"
8853065679823467777,
1179079, "Donna", "Thimm", "dthimm#example.com"
927671, "Lillian", "Wasson", "lwasson#example.com"
1175139, "Barry", "Tollison", "btollison#example.com"
1058086, "Christina", "Viktorin", "cviktorin#example.com"
What I need to do is iterate through it and when it comes to the lines where there is only the PID field with the long number, I need to store that in a variable ($wkey) and then use it in an insert statement. I know we could put the value on each row but the process that outputs the file cannot do that.
Hwere is my code:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($flag) {
$flag = false;
continue;
}
$import = "INSERT into exp_wb_bulk_reg(`WKey`,`PID`,`FName`,`LName`, `Email`,`status`) "
. "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
// Use the sql to insert into the table
}
fclose($handle);
How would I modify this to do what I need?
Here's working code:
// skip header line
$data = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// get wkey for lines that has empty second column
if(trim($data[1]) == "") {
$wkey = $data[0];
}
if(trim($data[1]) != "") {
$import = "INSERT into exp_wb_bulk_registrations(`WebinarKey`, `PID`,`FName`,`LName`, `Email`,`status`) "
. "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
echo $import."<br />";
}
}
fclose($handle);
You could just check the size of $data array. Something like:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(count($data) == 4){
$import = "INSERT into exp_wb_bulk_reg(`WKey`,`PID`,`FName`,`LName`, `Email`,`status`) "
. "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
// Use the sql to insert into the table
} else if(count($data) == ?) {
//DO STUFF
}
}
fclose($handle);
Here's my variant:
$wkey = ''; // just for sure
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
if(count($data)==1) { // hey, we found a wKey! let's remember it
$wkey = $data[0]; continue;
}
$import = "INSERT into exp_wb_bulk_reg(`WKey`,`PID`,`FName`,`LName`, `Email`,`status`) "
. "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
}
fclose($handle);