使用Codeigniter处理CSV文件中的数据

时间:2014-03-27 09:11:59

标签: php codeigniter csv

我是CODEIGNITER-PHP的初学者。我有一个可以在WEB中查看的CSV文件。我需要操作CSV文件中的数据,我需要为每行创建一个删除按钮和编辑按钮,当我edit我选择它的行的数据automatically save in CSV file而不下载文件与delete function相同。我已经被困在这里了。我很感激帮助。

以下是添加新数据EOF的代码。我现在需要做的是EDITDELETE

查看

 <div id="div1" class="row-fluid">
                    <div class="span12" id="dvData">
                        <?php
                            $row = 1;
                            $file_name = 'bin/pdw_table.csv';
                            $fname = 'bin/pdw_table_a.csv';
                            $table = file_get_contents($file_name);
                            $csv = explode(",", $table);
                            $newData = array();

                            foreach ($csv as $rowData)
                            {
                                $row = explode(",", $rowData);
                                $newData[] = implode(",", $row);
                            }
                            $newDatas = implode(",", $newData);
                            file_put_contents($file_name, $newDatas);

                                if (($handle = fopen("bin/pdw_table.csv", "c+")) !== FALSE) {
                                    $cols=array(
                                        array('title'=>'Field 1', 'name'=>'val1', 'type'=>'text'),
                                        array('title'=>'Field 2', 'name'=>'val2',),
                                        array('title'=>'Field 3', 'name'=>'val3',),
                                        array(),
                                        array('title'=>'Field 4', 'name'=>'val4',),
                                        array('title'=>'Field 5', 'name'=>'val5',),
                                        array(),
                                        array(),
                                        array('title'=>'Field 6', 'name'=>'val6',),
                                        array('title'=>'Field 7', 'name'=>'val7',),
                                        array('title'=>'Field 8', 'name'=>'val8',),
                                        array('title'=>'Field 9', 'name'=>'val9',),
                                        array('title'=>'Field 10', 'name'=>'val10',),
                                        array('title'=>'Field 11', 'name'=>'val11',),
                                        array('title'=>'Field 12', 'name'=>'val12',),
                                        array('title'=>'Field 13', 'name'=>'val13',),
                                        array(),
                                        array('title'=>'Field 14', 'name'=>'val14',),
                                        array('title'=>'Field 15', 'name'=>'val15',),
                                        array('title'=>'Field 16', 'name'=>'val16',),
                                        array(),
                                    );
                                    csv_table($handle, $cols);
                                } 
                        ?>

                        <?php
                            function csv_table($handle, $cols) {
                                echo "\n\t<style>\n".
                                    "\t\t.table-striped tr:nth-child(1) {background-color:#DDDDDD}\n".
                                    "\t\t.table-striped tr:nth-child(0) {font-weight:bold; background-color:#CCCCCC}\n".
                                    "\t</style>\n\n";

                                echo "\t<table class='table table-hover table-striped table-bordered' id=table-data>\n\t\t<tr>\n";
                                foreach($cols as $col) {
                                    if(!isset($col['title'])) echo "\t\t\t<th></th>";
                                    else echo "\t\t\t<th>${col['title']}</th>\n";
                                }
                                echo "\n\t\t</tr>\n";
                                $row=0; 

                                while(($data=fgetcsv($handle, 10000, ','))!==FALSE) {
                                    $num=count($data);
                                    $row++; 
                                    echo "\t\t<tr>";
                                    for($c=0; $c<$num; $c++) echo "<td>".$data[$c];
                                    echo "<td><a class='tcb' name='row_id' id=$row style='cursor:pointer;'>Delete</a>\n";
                                }
                                        while (($data = fgetcsv($handle, 10000, ',')) !== FALSE) {
                                            $num = count($data);

                                            $row++;

                        ?>
                                            <tr <?php if($row==0){echo "style='font-weight:bold; background-color:#CCCCCC'";} else {echo "style='background-color:#DDDDDD'";} ?> style="background-color:#DDDDDD">
                        <?php
                                                for ($c=0; $c < $num; $c++) {
                        ?>
                                                <td><?php echo $data[$c]; ?></td>

                        <?php
                            }
                            echo $row;

                            echo "\t<form method=post name=add1 id=add1 action='".base_url()."index.php/datacast_ctr/write_csv' autocomplete=off>\n".
                                 "\t\t<tr class=td1 id=td1>\n";

                                foreach($cols as $col) 
                                {
                                    if(!isset($col['name'])) 
                                    {
                                        echo "<td>";
                                        continue;
                                    }
                                    $type=!empty($col['type'])?$col['type']:'text';
                                    $id=!empty($col['id'])?$col['id']:$col['name'];
                                    echo "<td><input type=$type name=${col['name']} id=$id />";
                                }
                                echo "\t\t</form>\n\t</table>\n";
                            }
                        ?>
                                                <!--<td><a href="#" style="cursor: pointer;">Delete</a></td>-->
                                            <!--</tr>-->
                        <?php

                                        }

                                        fclose($handle);

                        ?>      
                                        <form method="post" name="add1" id="add1" action="<?php echo base_url();?>index.php/datacast_ctr/write_csv" autocomplete="off">
                                            <tr class="td1" id="td1" >  
                                                <td><input type="text" name="val1" id="val1"/></td>
                                                <td><input type="text" name="val2" id="val2"/></td>
                                                <td><input type="text" name="val3" id="val3"/></td>
                                                <td></td>
                                                <td><input type="text" name="val4" id="val4"/></td>
                                                <td><input type="text" name="val5" id="val5"/></td>
                                                <td></td>
                                                <td></td>
                                                <td><input type="text" name="val6" id="val6"/></td>
                                                <td><input type="text" name="val7" id="val7"/></td>
                                                <td><input type="text" name="val8" id="val8"/></td>
                                                <td><input type="text" name="val9" id="val9"/></td>
                                                <td><input type="text" name="val10" id="val10"/></td>
                                                <td><input type="text" name="val11" id="val11"/></td>
                                                <td><input type="text" name="val12" id="val12"/></td>
                                                <td><input type="text" name="val13" id="val13"/></td>
                                                <td></td>
                                                <td><input type="text" name="val14" id="val14"/></td>
                                                <td><input type="text" name="val15" id="val15"/> </td>
                                                <td><input type="text" name="val16" id="val16"/></td>
                                            </tr>
                                        </form>
                                    </table>

CONTROLLER

function delete_csv() 
{
    $filename = 'bin/pdw_table.csv';
    $row_number = trim($this->input->post('a'));

    $lines = file($filename);
    if(isset($lines[$row_number])) unset($lines[$row_number]);
    $liness = implode("\n", $lines);

    if ($h = fopen($filename, "a"))
    {
      fputcsv($h, $liness, '-');
      fclose($h);
    }

}

0 个答案:

没有答案