I am a newbie to php and have been searching tirelessly for a solution to this problem (i'll bet its a super simple solve too *sigh).
I am importing a .csv feed from a google doc. It is pulling in 2 columns, one for "name" and the other "location". I would like to remove duplicate "locations". since i am using fgetcsv, my understanding is that it is already sorting the data into an array. Ideally, it would omit the "location" duplicates so that the "names" look as though they are listed under the "location" they correspond to.
Here is what i have:
$url = "https://docs.google.com/spreadsheet/pub?key=0AsMT_AMlRR9TdE44QmlGd1FwTmhRRkFHMzFTeTZhS3c&output=csv";
$handle = fopen($url, "r");
while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
echo "<li>\n";
echo $data[1];
echo "<br/>\n";
echo $data[2];
echo "</li>\n";
}
fclose($handle);
ideally i would be able to use something like this:
$url = "https://docs.google.com/spreadsheet/pub?key=0AsMT_AMlRR9TdE44QmlGd1FwTmhRRkFHMzFTeTZhS3c&output=csv";
$handle = fopen($url, "r");
while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
echo "<li>\n";
echo array_unique($data[1]);
echo "<br/>\n";
echo $data[2];
echo "</li>\n";
}
fclose($handle);
Many thanks in advance for any help! :o)
This may work, assuming that the items in the array are grouped by location. It stores the last data item (location) and compares whether each item has that location. If it does, it prints it, otherwise it creates a new list item with the new location, and then prints the name underneath (I haven't tested it though):
$url = "the-url-to-my-csv-feed";
$handle = fopen($url, "r");
$lastdata = '';
while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
if ($lastdata == '') {
echo "<li><strong>" . $data[1] . "</strong>\n";
echo "<br/>\n";
$lastdata = $data[1];
}
if ($lastdata != $data[1]) {
echo "</li>\n";
echo "<li><strong>" . $data[1] . "</strong>\n";
echo "<br/>\n";
$lastdata == $data[1];
}
echo $data[2] . "<br/>\n";
}
fclose($handle);
<? //PHP 5.4+
$url = 'url to your csv feed';
//Group people by same location first,
//not assuming csv is already sorted.
$namesByLocations = [];
//Because we're using \SplFileObject, when the reference goes out
//of scope at the end of the loop, the file pointer is never
//left open. This is true even if an exception is thrown
//in the middle of looping.
foreach(
\call_user_function(static function() use ($url){
$file = new \SplFileObject($url);
$file->setFlags(\SplFileObject::READ_CSV);
return $file;
})
as $array
){
//$array[1] is assumed to be location string
//$array[2] is assumed to be a name that is there.
$namesByLocations[$array[1]][] = $array[2];
}
foreach($namesByLocations as $location => $names){
//Protect against injection flaws,
//escape to destination's context. (html this time)
echo '<strong>' . \htmlspecialchars($location) . '</strong>';
echo '<ul>';
foreach($names as $name){
echo '<li>' . \htmlspecialchars($name) . '</li>';
}
echo '</ul>';
}
?>
Related
I know this question has been asked before but my problem is different because I need to return the specific rows after a search.
Basically, this is the scenario:
I need to search a CSV file for a specific word/string in the name column and then IF the word/string is found I need to get the row[4] and row[5] and print them in the PHP.
The CSV looks like this:
"id","ident","type","name","latitude_deg","longitude_deg","elevation_ft"
6523,"00A","heliport","Heliport",40.07080078125,-74.93360137939453,11,
So basically, I need to search by name and if found, return the latitude_deg,longitude_deg.
This is what I have so far... However, this searches the ENTIRE CSV file which makes it slightly slower and it only returns whether the CSV file contains the string/word or not...
$search = "Heliport";
$lines = file('myCsv.csv');
$line_number = false;
while (list($key, $line) = each($lines) and !$line_number) {
$line_number = (strpos($line, $search) !== FALSE);
}
if($line_number){
echo "Results found for " .$search
} else {
echo "No results found for $search";
}
Could someone please advice on this issue? Thanks in advance.
fgetcsv is a good tool for this, it reads a CSV line and breaks it into an array.
$search = 'Heliport';
if (($fp = fopen("myCsv.csv", "r")) !== false) {
while (($row = fgetcsv($fp)) !== false) {
if($row[3] === $search) {
echo 'Found ' . $row[3] . ': ' . $row[4] . ', ' . $row[5] . "\n";
}
}
fclose($fp);
}
I am have retrieved all the file contents from a directory. It prints the file contents name from an array. However I want only a portion of the file content name. Any idea how can I achieve this? I have tried using the following:
The file contents from the directory has format: pdb101m.ent.gz , pdb102l.ent.gz
I want to retrieve only the 101m and 102l
<?php
$dir = "C:/Users/Desktop/EAD/PDB/";
$files = array();
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
foreach($files as $ex){
echo str_replace('pdb.ent.gz', ' ', $ex). '<br>';
}
?>
Please help. Grateful.
Use substr() function:
echo substr('pdb101m.ent.gz',3,4); // Outputs: 101m
echo substr('pdb102l.ent.gz',3,4); // Outputs: 102l
So:
foreach($files as $ex){
echo substr($ex, 3,4). '<br>';
}
Edit: Update my answer attending the OP new request:
So in your query you should use:
foreach ($files as $ex) {
$search = substr($ex, 3, 4);
$sql = 'SELECT DISTINCT `pdb_code` FROM pdb WHERE `pdb_code` <> "' . $search . '" LIMIT 6';
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$pdb[] = $row['pdb_code'];
}
}
If it's always pdb at the beggining and .ent.gz at the end you can do simply:
echo substr('pdb101m.ent.gz',3,-7);
Negative value of third parameter in substr() means
that many characters will be omitted from the end of string
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 am trying to import data from a source that is not a csv or txt but I am able to read it like a text / csv with my code.
The problem I am having is that some "data records" do not follow the same logic. I have approximately 70% of the document conforming, however, I think I may be missing something in the data that is throwing off the results.
I would appreciate it if you could please take a look at the code and the file and help me figure out why some of the data is not working like the rest of the document. I suspect it is because of odd number of characters (~ and/or >) in one of the fields or that the start/stop is slightly different for some of the records.
<?php
header("Content-Type:text/html");
$file = "data.txt";
if (($handle = fopen($file, "r")) !== FALSE)
{
fgetcsv($handle, 1000, ">~Yn");
$imports = array();
while (($data = fgetcsv($handle, 1000, ">")) !== FALSE)
{
if(strpos($data[4],'<') !== false)
{
echo "<br /><strong>Section:</strong> " . $data[5];
echo "<br /><strong>Row:</strong> " . $data[6];
echo "<br /><strong>Qty:</strong> " . $data[7];
echo "<br /><strong>Price:</strong> " . $data[8];
echo "<br /><strong>Notes:</strong> " . $data[10];
}
else
{
echo "error: ";
print_r($data);
}
echo "<br /><br /><br /><br />";
}
fclose($handle);
}
?>
The sample data can be found here: Sample Data
I have found a solution that works better than the method I originally attempted. I first determined that loading it as a CSV was not giving me the best results. I then realized that there are common delimiters between each record that I was missing. That being said, I split the contents into lines and then split the lines into pieces using split(). I also ignored the first and last match because of data mismatches.
$file = "data.txt";
$content = file_get_contents($file);
$lines = split(">~", $content);
foreach($lines as $line)
{
$data = split(">", $line);
if(strpos($data['5'],'.') !== false) //if the section is a price
{
//the first match is ignored
}
elseif(empty($data['7'])) //if Qty is empty
{
//the last match is ignored
}
else
{
echo "<br><br><br>";
echo $data['5'] . " (Section) <br>";
echo $data['6'] . " (Row) <br>";
echo $data['7'] . " (Qty) <br>";
echo $data['8'] . " (Price) <br>";
//use the data
}
}
This resulted in a much more accurate and thorough data collection!
I have the following code which outputs the contents of text files held in a directory.
Ive been looking at the sort command in PHP but cant get it to work with the following code, I usually get an error about the input being a string and not an array.
How can I sort the directory of file before they are output?
$directory = "polls/";
$dir = opendir($directory);
while (($file = readdir($dir)) !== false) {
$filename = $directory . $file;
$type = filetype($filename);
if ($type == 'file') {
$contents = file_get_contents($filename);
list($tag, $name, $description, $text1, $text2, $text3, $date) = explode('¬', $contents);
echo '<table width="500" border="1" cellpadding="4">';
echo "<tr><td>$tag</td></tr>\n";
echo "<tr><td>$name</td></tr>\n";
echo "<tr><td>$description</td></tr>\n";
echo "<tr><td>$text1</td></tr>\n";
echo "<tr><td>$text2</td></tr>\n";
echo "<tr><td>$text3</td></tr>\n";
echo "<tr><td>$date</td></tr>\n";
echo '</table>';
}
}
closedir($dir);
First collect the entries in an array, sort it and then put it out:
$directory = "polls/";
$dir = opendir($directory);
$files = array();
while (($file = readdir($dir)) !== false) {
$files[] = $file;
}
closedir($dir);
sort($files);
foreach ($files as $file) {
// content of your original while loop
}
Another possibility is fetching the file names with glob(). Its output is sorted by default.
<?php
foreach(glob('polls/*.txt') as $file){
// ...
}
?>
Don't print it in the while loop, but store it in an array. Sort the array and then print it.
(On php.net you'll find enough different sorting functions to get the sorting method you need.)