C-R-U-D CSV file

debtonu sarkar
debtonu sarkar used Ask the Experts™
on
I have a csv file. i need to read it which i already done but cannot edit it and also need to delete a particular line....  Can anybody help me for doing this......

the csv file is given bellow
https://drive.google.com/file/d/0B-9jQIuzx-JYYjlBZTFaX3JWN28/view?usp=sharing

and the read code is also given bellow with in table format:
<html>
    <head>
        <title>Reading Form Csv File Training</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    </head>
    <body>
        <div class="container">
            <div class="col-sm-3"><h1>Pages</h1></div>
            <div class="col-sm-3"><a href="index.php"><h2>Web1</h2></a></div>
            <div class="col-sm-3"><a href="p1.php"><h2>web1-fino-al-20170105</h2></a></div>
            <div class="col-sm-3"><a href="p2.php"><h2>web1-fino-al-20151118</h2></a></div>
            <table class="table table-striped">
                <thead>
                <th>No</th>
                <th class="text-center">Data 1</th>
                <th class="text-center">Data 2</th>
                <th class="text-center">Data 3</th>
                <th class="text-center">Data 4</th>
                <th class="text-center">Data 5</th>
                <th class="text-center">Data 6</th>
                <th class="text-center">Data 7</th>
                <th class="text-center">Data 8</th>
                <th class="text-center">Data 9</th>
                <th class="text-center">Data 10</th>
                <th class="text-center">Data 11</th>
                </thead>
                <?php
                $row = 1;
                if (($handle = fopen("csvfile/web1-fino-al-20151118.csv", "r")) !== FALSE) {
                    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                        $num = count($data);
                       
                        for ($c = 0; $c < $num; $c++) :
             
                            $va = explode(";", $data['0']);
         
                            $cx = 0;
                            ?>
                            <tr>
                                <td class="text-center"><?php echo $row; ?></td>
                                <?php
                                for ($cx = 0; $cx < count($va); $cx++) {
                                    ?>


                                    <td class="text-center"><?php echo $va[$cx]; ?></td>



                                    <?php
                                }
                                ?>
                                <td><button type="button" class="btn btn-warning">Edit</button></td>
                                <td><a href="deletecsvline.php?$data['0']=<?php echo $data['0'] ?>"<button type="button" class="btn btn-danger">X</button></a></td>
                            </tr> 
                            <?php
                        endfor;
                        $row++;
                    }
                    fclose($handle);
                }

                ?>

               
            </table>
        </div>
    </body>
</html>

Open in new window




waiting for help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
Greetings, Sarkar, and welcome to E-E.

Please upload the CSV file to the E-E site.  Google interferes with my ability to access the file.

Please use fully-qualified URLs (starting "http...") for links in the code snippets.  Relative URLs are fine for your server, but none of us have your server, so if we need to be able to access a file on your server (or anywhere else) we need the full URL.

And when you post code at E-E, please use the code snippet feature.  Example below.

I would also gently suggest that this design is often a recipe for confusion if it's not engaged to a confirmation script.  What would happen if a screen scraper or web spider read your web page?  The web spider will follow all of the links.  If the delete statement triggers deletion, without separate confirmation, the web spider will empty your CSV.  Probably not exactly what you want.  This is a bigger problem when it's mistakenly applied to a database, where data loss can be real and permanent.
<a href="deletecsvline.php?$data['0']=<?php echo $data['0'] ?>

Open in new window

<html>
    <head>
        <title>Reading Form Csv File Training</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    </head>
    <body>
        <div class="container">
            <div class="col-sm-3"><h1>Pages</h1></div>
            <div class="col-sm-3"><a href="index.php"><h2>Web1</h2></a></div>
            <div class="col-sm-3"><a href="p1.php"><h2>web1-fino-al-20170105</h2></a></div>
            <div class="col-sm-3"><a href="p2.php"><h2>web1-fino-al-20151118</h2></a></div>
            <table class="table table-striped">
                <thead>
                <th>No</th>
                <th class="text-center">Data 1</th>
                <th class="text-center">Data 2</th>
                <th class="text-center">Data 3</th>
                <th class="text-center">Data 4</th>
                <th class="text-center">Data 5</th>
                <th class="text-center">Data 6</th>
                <th class="text-center">Data 7</th>
                <th class="text-center">Data 8</th>
                <th class="text-center">Data 9</th>
                <th class="text-center">Data 10</th>
                <th class="text-center">Data 11</th>
                </thead>
                <?php
                $row = 1;
                if (($handle = fopen("csvfile/web1-fino-al-20151118.csv", "r")) !== FALSE) {
                    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                        $num = count($data);
                       
                        for ($c = 0; $c < $num; $c++) :
             
                            $va = explode(";", $data['0']);
         
                            $cx = 0;
                            ?>
                            <tr>
                                <td class="text-center"><?php echo $row; ?></td>
                                <?php
                                for ($cx = 0; $cx < count($va); $cx++) {
                                    ?>


                                    <td class="text-center"><?php echo $va[$cx]; ?></td>



                                    <?php
                                }
                                ?>
                                <td><button type="button" class="btn btn-warning">Edit</button></td>
                                <td><a href="deletecsvline.php?$data['0']=<?php echo $data['0'] ?>"<button type="button" class="btn btn-danger">X</button></a></td>
                            </tr>
                            <?php
                        endfor;
                        $row++;
                    }
                    fclose($handle);
                }

                ?>

               
            </table>
        </div>
    </body>
</html>

Open in new window

You want to edit a spicific line or do you want to open the file for editting?
Most Valuable Expert 2011
Top Expert 2016

Commented:
Please verify that this file contains the CSV, thanks.  It looks like it's not really "comma" separated but actually "semicolon" separated, right?
temp_sarkar.csv
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Is there any reason why you want to work off the CSV file and not a database?
Most Valuable Expert 2011
Top Expert 2016
Commented:
Not sure what you want to do about editing or deleting, but this may get you a step closer to a solution.  

Each of the rows will have an id= attribute.  You can use a jQuery selector to target the rows by id and remove them.  You might attach this functionality to the "X" at the end of the display rows.

Each of the HTML links to the deletecsvline.php script will have a q= argument.  Your deletecsvline.php script will be able to find the q index in the $_GET array.

If you're new to PHP and want to learn the language, this article has links to well-vetted learning resources.  Just skip over the parts you already know from experience or academic work.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

Best of luck with your project! ~Ray
<?php // demo/temp_sarkar.php
/**
 * https://www.experts-exchange.com/questions/29009984/C-R-U-D-CSV-file.html
 */
error_reporting(E_ALL);
?>
<html>
    <head>
        <title>Reading Form Csv File Training</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    </head>
    <body>
        <div class="container">
            <div class="col-sm-3"><h1>Pages</h1></div>
            <div class="col-sm-3"><a href="index.php"><h2>Web1</h2></a></div>
            <div class="col-sm-3"><a href="p1.php"><h2>web1-fino-al-20170105</h2></a></div>
            <div class="col-sm-3"><a href="p2.php"><h2>web1-fino-al-20151118</h2></a></div>
            <table class="table table-striped">
                <thead>
                <th>No</th>
                <th class="text-center">Data 1</th>
                <th class="text-center">Data 2</th>
                <th class="text-center">Data 3</th>
                <th class="text-center">Data 4</th>
                <th class="text-center">Data 5</th>
                <th class="text-center">Data 6</th>
                <th class="text-center">Data 7</th>
                <th class="text-center">Data 8</th>
                <th class="text-center">Data 9</th>
                <th class="text-center">Data 10</th>
                <th class="text-center">Data 11</th>
                </thead>
                <?php
                $row = 1;
                if (($handle = fopen("https://filedb.experts-exchange.com/incoming/2017/03_w11/1151466/temp_sarkar.csv", "r")) !== FALSE) {
                    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                        $num = count($data);

                        for ($c = 0; $c < $num; $c++) :

                            $va = explode(";", $data['0']);

                            $cx = 0;
                            ?>
                            <tr id="<?php echo $row; ?>">
                                <td class="text-center"><?php echo $row; ?></td>
                                <?php
                                for ($cx = 0; $cx < count($va); $cx++) {
                                    ?>


                                    <td class="text-center"><?php echo $va[$cx]; ?></td>



                                    <?php
                                }
                                ?>
                                <td><button type="button" class="btn btn-warning">Edit</button></td>
                                <td><a href="deletecsvline.php?q=<?php echo $va['0']; ?>"<button type="button" class="btn btn-danger">X</button></a></td>
                            </tr>
                            <?php
                        endfor;
                        $row++;
                    }
                    fclose($handle);
                }

                ?>


            </table>
        </div>
    </body>
</html>

Open in new window

Commented:
Like Julian said, this is certainly a job for a database. If you can't use a full-blown database like MySQL (maybe you need portability), then you should consider a database that doesn't need a special server. Portable databases, like SQLite (support is built into PHP nowadays), can still offer great performance.

If you absolutely HAVE to use CSV, then it's going to get complicated, ESPECIALLY if the CSV is large.

If your CSV file is really small (< 10k or so), then you could consider just reading in the entire file into memory with fgetcsv(), then you make the edits to the specific record you want to change, and then write the CSV again.

Similarly, if you wanted to delete a record, read everything into memory, remove the unwanted element out of your resulting array, and rewrite the CSV.

fgetcsv() is fairly slow, so the larger the file, the slower the above will go, and the higher the danger of running out of memory. Reading a 30-megabyte CSV into memory with fgetcsv() will use about 750-800 megs of memory.

Part of the reason for doing it this way is because if you cannot add or remove bytes in the middle of a file. You can UPDATE existing bytes, though.

So if you wanted to delete a 20-byte record from the middle of the CSV without leaving a space behind, you would need to jump to the unwanted record's position, and then basically loop through the rest of the file from that point, rewriting the content from 20 bytes ahead, and then truncate the file 20 bytes from the end.

If you wanted to ADD a 20-byte record into the middle of the CSV, you'd have to do a similar jump-and-then-loop, re-reading and re-writing the rest of the file using a buffer and injecting the 20-byte record into that spot.

It's not exactly a trivial task, and you likely want to use exclusive locks while making these updates to avoid data corruption from a competing write activity.

Your toolkit here is:

File Mode:
fopen("file.csv","r+b") - Gives you read and write access on a file and uses binary mode to avoid the EOL translation.

File Navigation:
ftell() - Gives your current offset in the file
fseek() - Jumps immediately to a specified offset

Reading/Writing:
flock() - Lets you lock/unlock a file during reading or writing
fread() - Reads a series of bytes into the buffer (fastest way to read chunks of a large file without parsing them)
fwrite() - Writes a series of bytes to the file at the current position

If you want to go down the path of doing larger CSV files, you're going to have to master the above commands. They are good commands to know (and it helps to keep a separate file with the offsets of each record so you can quickly jump to any record in the file using fseek(), or maybe the offsets of every 100 records, for really large files)

There are a dozen ways of encoding the CSV file in a manner that would make all of this a lot easier to do (e.g. converting it to JSON after you read in the file would be far faster and easier), but at that point, you're beginning to build a database and there are lots of database choices that are far easier to implement and use.
Most Valuable Expert 2011
Top Expert 2016
Commented:
The standard design pattern for C-R-U-D database table maintenance is shown in this article, in the hope that we can convince you to use a database.
https://www.experts-exchange.com/articles/12335/PHP-and-MySQLi-Table-Maintenance.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial