2

I have looked through all the relative data on this site to try and find a resolution that works for me but, I have not found one. I have been at this for hours, after exhausting all my options (trying). I have finally decided to ask for help. I need to echo the data from the model to the view to see if I am doing this right (testing). I am trying to learn this framework (Codeigniter) my own. I have the following setup...

Controller:

<?php
class csv extends CI_Controller
{
public $data;
public function __construct()
{
    parent::__construct();
    $this->load->model('csvToJson');
    $this->load->helper('url');
}
function index()
{
    $this->load->view('uploadCsvForm');

}
function uploadData()
{
    $this->csvToJson->uploadData();
    redirect('csv');

}
}
?>

model:

<?php
// php class to convert csv to json format
class csvToJson extends CI_Model{

function __construct()
{
    parent::__construct();
}

function uploadData()
{
    $fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");

    //read csv headers
    $key = fgetcsv($fp,"1024",",");

    // parse csv rows into array
    $json = array();
    while ($row = fgetcsv($fp,"1024",",")) 
    {
        $json[] = array_combine($key, $row);
    }

    fclose($fp) or die("can't close file");
    return json_encode($json);

}
}// end class
?>

View:

 <form action="<?php echo site_url();?>csv/uploadData" method="post" 
 enctype="multipart/form-data" name="form1" id="form1"> 
<table>
    <tr>
        <td> Choose a CSV file: </td>
        <td>
            <input type="file" class="form-control" name="userfile" 
              id="userfile"  align="center"/>
        </td>
        <td>
            <div class="col-lg-offset-3 col-lg-9">
                <button type="submit" name="submit" class="btn btn-
                 info">upload</button>
            </div>
        </td>
    </tr>
</table> 

</form>

Any help would be appreciated

2
  • what exactly do you want to show on view? Commented Apr 10, 2017 at 5:21
  • codeigniter.com/user_guide/general/… Commented Apr 10, 2017 at 5:23

3 Answers 3

2

To view the data on view you need to pass in through controller

function uploadData()
{
$data = $this->csvToJson->uploadData();
$this->load->view('uploadCsvForm',$data);
}
Sign up to request clarification or add additional context in comments.

3 Comments

just need to echo out my json data in the view so i can test and make sure its working.
updated to: function uploadData() { $data = $this->csvToJson->uploadData(); //$this->csvToJson->uploadData(); $this->load->view('uploadCsvForm',json_encode($data)); redirect('csv'); } <?php echo json_encode($data, JSON_PRETTY_PRINT); ?> and getting an error : A PHP Error was encountered Message: Undefined variable: data Filename: views/uploadCsvForm.php Line: 19 uploadCsvForm.php Line: 19
change it to $data['anyName']=$this->csvToJson->uploadData() and access $anyName in your view
1

You need to get the Data in your function in which you are calling the view, and then pass that Data to the view. For example in your Controller

public function index()
{
  $data['files']=$this->yourModel->getDataFunction();
  $this->load->view('path/to/your/view',$data);
}

In your Model

public function getDataFunction()
{
  return $this->db->query('your query')->result();
}

In your View

<?php foreach($files as $file){?>
      <h2><?php echo $files['title']?></h2>
<?php endforeach?>

For File uploading or Form Posting your might wanna look at Form Validation and File Uploading Class

Comments

1

ok, got it working with the following :

In Controller:

function uploadData()
{
    $data['json'] = $this->csvToJson->uploadData();
    $this->load->view('uploadCsvForm',$data);
}

In View:

<?php
$data = json_encode($json, JSON_PRETTY_PRINT);
echo $data;
?>

looks like we are good too, csv is parsing to JSON

1 Comment

Can you not do <code>$this->load->view('uploadCsvForm', json_encode($data, JSON_PRETTY_PRINT));</code> in your controller and not have to mess around in the view?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.