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