ignoring multiple entries from a CSV file.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Monotoko
Forum Commoner
Posts: 64
Joined: Fri Oct 26, 2007 4:24 pm

ignoring multiple entries from a CSV file.

Post by Monotoko »

Hiya :)

I have a search function on a webpage, and when someone searches for something, i want it too appear in a section of the admin panel....i have achieved this using a .csv file and the following script:

Code: Select all

                       <?php
 $row = 1; // a counter for line numbers
 $handle = fopen("test.csv", "r"); // open the test.csv file for reading
 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { // while there are still lines to read
     $num = count($data); // the number of columns
    $row++; // next row
    for ($c=0; $c < $num; $c++) {
         echo $data[$c] . "<br />\n"; // print everything in the row
     }
 }
 fclose($handle); // close the file
?>
Now,what i need it to do, is if the timestamp column is the same (which would be stored as $data[3] in each case...) skip that entry and move to the next...what modifications should i make?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: ignoring multiple entries from a CSV file.

Post by Christopher »

You need to add something like:

Code: Select all

if ($data[3] == $timestamp) {
(#10850)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: ignoring multiple entries from a CSV file.

Post by pickle »

Pseudo code:

Code: Select all

$timestamp = some impossible value;
while(iterating through each line)
{
  if(current timestamp != $timestamp)
    echo the line
  
  $timestamp = current timestamp
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply