Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7718623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:12:38+00:00 2026-06-01T03:12:38+00:00

I have a .csv file with code and description. I want to search the

  • 0

I have a .csv file with “code” and “description”. I want to search the .csv and if it finds a duplicate “code”, replace the whole row.

ie. of csv file:

code          description
STA-101       A great product
STA-102       Terrible Product

If it were to find “STA-102” replace description with something, ie:

code          description
STA-101       A great product
STA-102       OK Product

These are dynamic so I need an if statement. Can this be done using fgetcsv()?
The below doesn’t work but its all I know right now:

$row = 1;
if (($handle = fopen("formTest.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            if ($data[$c] = "STA-102") {
          $data[$c] = "OK Product"; }
            echo $data[$c] . "<br />\n";
        }

    }
    fclose($handle);
}

In order to write to my file to begin with, I have:

$code = $_POST['CODE'];
$description = $_POST['DESC']; 

if ($_POST) {
    $csvData = $code . "," . $description. "\n";
    $fp = fopen("formTest.csv","ab"); 

    if($fp){
    fwrite($fp,$csvData);
    fclose($fp); }
}

How can I incorporate what I have above with the if statement to overwrite the code if it needs be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:12:39+00:00Added an answer on June 1, 2026 at 3:12 am

    In your example the separator is [tab] (not ‘,’).

     while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
    

    At the beggining put:

    $res = "";
    

    Also instead of

    for ($c=0; $c < $num; $c++) {
        if ($data[$c] = "STA-102") {
            $data[$c] = "OK Product"; }
        }
        echo $data[$c];
    

    I think you want

    if ($data[0] == "STA-102") {
        $data[1] = "OK Product";
    }
    $res .= implode('\t', $data) . "\n";
    

    and after fclose($handle);

    $handle = fopen("formTest.csv", "w"));
    fwrite($handle, $res);
    fclose($handle);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that returns a .CSV file when a link is
I have a code where in I first convert a .csv file into multiple
I have a simple java code that reads text csv file that contains sentences
I have the following piece of Java code that reads strings from CSV file.
I have the following code: ifstream initFile; initFile.open(D:\\InitTLM.csv); if(initFile.is_open()) { // Process file }
I have CSV file and Macro in VBA. I want to open CSV file
Little Background: I have csv file which has lots of rows and each row
I have 46 rows of information, 2 columns each row (Code Number, Description). These
I have a CSV file which the code below uses to put the contents
I have a csv file where each row is a different type of record.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.