Welcome Guest, Not a member yet? Register   Sign In
how to read csv file saved in webserver using codeigniter.
#1

[eluser]Unknown[/eluser]
good day everyone, I start using codeigniter for 3 weeks. I’m developing a system that save csv file, the problem is i don’t know how to view the file that i save in my web server.

this is how i save/find csv file together with the file name

controller
Code:
function SaveOrFind()
{

  $this->load->helper(array('form', 'url'));
  $this->load->model('dtr_model');
                $this->load->library('csvimport');
  
  if(isset($_POST["btnSaveAndSubmit"])){
  
   $config['upload_path'] = './uploads/';
   $config['allowed_types'] = 'csv';
  
   $this->load->library('upload', $config);
    
   $this->load->library('form_validation');
   $this->form_validation->set_rules('selTimerecordMonth', 'Month', 'required');
   $this->form_validation->set_rules('selTimerecordPeriod', 'Period','required');
  
  
    if ( !$this->upload->do_upload() || $this->form_validation->run() == FALSE)
    {
     $error = array('error' => $this->upload->display_errors());
    
     $this->load->view('header');
     $this->load->view('dtr_nav');
     $this->load->view('dtr_timerecord_nav');
     $this->load->view('dtr_timerecord_main', $error);
     $this->load->view('footer');

    }
    else
    {
    
     $image_data = $this->upload->data();          
     $insert_data = array(
      'FILENAME' => $image_data["file_name"],
      'MONTH'=>$this->input->post('selTimerecordMonth'),
      'PERIOD'=>$this->input->post('selTimerecordPeriod'));
     $query = $this->db->insert('dtr_timerecord',$insert_data);
     $id = $this->db->insert_id();
        
    
     $file_data = $this->upload->data();
              $file_path =  './uploads/'.$file_data['file_name'];
    
     echo "[removed]alert('Upload Successful: ". $file_data['file_name']."');[removed]";
     $this->timerecord();
      
            }

          }
    if(isset($_POST["btnFind"])){
     $this->load->view('header');
     $this->load->view('dtr_nav');
     $this->load->view('dtr_timerecord_nav');
      
     $this->load->library('form_validation');
     $this->form_validation->set_rules('selTimerecordMonth', 'Month', 'required');
     $this->form_validation->set_rules('selTimerecordPeriod', 'Period','required');
  
     if ($this->form_validation->run() == FALSE)
     {
      $this->load->view('dtr_timerecord_main',array('error'=>''));
      
     }
     else
     {
      $this->load->model('dtr_model');
      $data=array('result'=>$this->dtr_model->finddtr());    
      $this->load->view('success_dtr_find',$data);
      }
     $this->load->view('footer');
    }
}

controller selecting csv file by primary key
Code:
function importcsv() {    

  $sql = "SELECT * FROM dtr_timerecord WHERE TIME_RECORD_ID='".$this->input->post('txtTimeRecordID')."'";
  ?>
  [removed]alert("<?php echo $sql;?>");[removed]
  <?php
  return $this->db->query($sql);

model in choosing csv file by primary key
Code:
function FindDtr(){
$getInfo = "SELECT * FROM dtr_timerecord WHERE MONTH ='".$this->input->post('selTimerecordMonth')."' AND PERIOD ='".$this->input->post('selTimerecordPeriod')."'";
        $getResult = $this->db->query($getInfo);
  return $getResult;                                      
}

view
Code:
<html>
<head>
</head>

<div id="content">
&lt;?php
echo"<table border='1' >
<tr>
  <th></th>
  <th>MONTH</th>
  <th>PERIOD</th>
  <th>FILE NAME</th>
  <th>ACTION</th>
</tr>";
foreach ($result->result_array() as $row) {
  echo "<tr>";
  echo form_open("dtr/importcsv");
  
  echo"<td>&lt;input type='hidden' name='txtTimeRecordID' value='".$row["TIME_RECORD_ID"]."'&gt;&lt;/td>".
  "<td align='center'>".$row["MONTH"]."</td>".
  "<td align='center'>".$row["PERIOD"]."</td>".
  "<td align='center'>".$row["FILENAME"]."</td>".
  "<td>&lt;input type='submit' value='VIEW'&gt;&lt;/td>";
  echo form_close();
  "</tr>";
}
echo "</table>";
echo "<br>";
echo anchor('dtr/timerecord', 'BACK');
?&gt;
</div>
&lt;/html&gt;

i want that when i click the view button the content of the csv file ill be display in the screen
#2

[eluser]EyeAmN8[/eluser]
So it looks like you are headed in the right direction.
Code:
$file_data = $this->upload->data();
              $file_path =  './uploads/'.$file_data['file_name'];

This above code is the way you find a file. Just like an image, but when rendering an image in the browser, the browser knows how to deal with its contents.
With a csv or txt or any file, the browser does not know what to do with it.

So, what you need to do is parse the file for its contents, save them in a collection (array will work), then pass it to the view.

I've never used php for file parsing, I always use perl because I think it handles files much better.

I think in php you could use
Code:
file_get_contents()
and it will return a string of the file's contents. Just google parsing csv files in php and you'll figure it out
#3

[eluser]CroNiX[/eluser]
It looks like you're storing them in the db, so after you retrieve the raw CSV from the db you could use str_getcsv() to convert it to an array which you can then loop through to build your view.




Theme © iAndrew 2016 - Forum software by © MyBB