SomniusX
SomniusX

Reputation: 83

PHP Locate line to alter in CSV using Variables

Greetings fellow coders.

I have a csv file (includes/sbk.php, ignore the extension, it is a csv) that i read and delete lines from.

A sample of that csv file is ...

    serial,lastid,url,short,other
    01,without last ID,#,no scheduled search,no search criteria
    02,without last ID,#,no scheduled search,no search criteria
    03,without last ID,#,no scheduled search,no search criteria

It's got about 50 lines. The 1st line is used to define titles in an array and the rest are read into an array having it lifted from 0 starting point to 1.

Reading the CSV is not a problem. I also use the following code to delete lines, identified by the id (first csv col):

    if (isset($_POST['button'.$sID.'del'])) { 
            /* cmd */               //  $delLastID $delUrl $delShort $delOther cleans house for 
            $lines = file($_SERVER['DOCUMENT_ROOT'] . '/includes/sbk.php');
            $words =         ($sbk.','.$delLastID.','.$delUrl.','.$delShort.','.$delOther);
            $result = '';
            foreach($lines as $line) {
                if(substr($line, 0, 2) == $sbk) {
                    $result .= $words."\n";
                } else {
                    $result .= $line;
                }
            }
            file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/includes/sbk.php', $result);
            echo '<script type="text/javascript">parent.location="tabs.php";</script>';     // return the user to tabs
        }

Now to my problem..

In an other script i read thru the file and have a huge form for the user to choose stuff that with a "Save" button it tries to do the same as above to re-write the specific line with my variables.

//      $VarForSbkLastID it's $elID[1][0];
//      $VarForSbkUrl it's htmlspecialchars($_SERVER['QUERY_STRING'])
        $carVarVariant      = ($allCarVars['variant']);
        $VarForSbkShort     = $carVarMake.' '.$carVarModel.' '.$carVarVariant.' '.$carVarPriceFrom.' '.$carVarPriceTo.' '.$carVarMileageFrom.' '.$carVarMileageTo.' '.$carVarRegFrom.' '.$carVarRegTo.' '.$carVarEngineSizeFrom.' '.$carVarEngineSizeTo.' '.$carVarEnginePowerFrom.' '.$carVarEnginePowerTo.' '.$carVarFuel.' '.$carVarColor.' '.$carVarRegion.' ';
        $VarForSbkOther     = $carVarCategory.' '.$carVarMake.' '.$carVarModel.' '.$carVarVariant.' '.$carVarPriceFrom.' '.$carVarPriceTo.' '.$carVarMileageFrom.' '.$carVarMileageTo.' '.$carVarRegFrom.' '.$carVarRegTo.' '.$carVarEngineSizeFrom.' '.$carVarEngineSizeTo.' '.$carVarEnginePowerFrom.' '.$carVarEnginePowerTo.' '.$carVarFuel.' '.$carVarGearbox.' '.$carVarDoors.' '.$carVarDriveType.' '.$carVarColor.' '.$carVarAirbags.' '.$carVarDamage.' '.$carVarEuroclass.' '.$carVarRegion.' ';

if (isset($_POST['buttonadd'])) { 
        $lines = file($_SERVER['DOCUMENT_ROOT'] . '/includes/sbk.php');
        $words = ($refid2.','.$elID[1][0].','.htmlspecialchars($_SERVER['QUERY_STRING']).','.$VarForSbkShort.','.$VarForSbkOther);
        $result = '';
        foreach($lines as $line) {
            if(substr($line, 0, 2) == $refid2) {
                $result .= $words."\n";
            } else {
                $result .= $line;
            }
        }
        file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/includes/sbk.php', $result);
        echo '<script type="text/javascript">parent.location="tabs.php";</script>';     // return the user to tabs
    }
echo '<input type="submit" name="buttonadd" value="Save">';

Well.. i'm stuck, can't figure what i'm doing wrong there and it doesn't execute my commands after isset, like when cleaning a line..

Anybody?

P.S. for reading i use the following.. http://pastebin.com/69nfjKPM

Upvotes: 0

Views: 57

Answers (1)

SomniusX
SomniusX

Reputation: 83

I've implemented the parseCSV library and successfully manipulated specific lines of the csv file.

Thank you for everything fellow coders!

Upvotes: 1

Related Questions