tomcc84
tomcc84

Reputation: 31

codeigniter, echo data from model to view PHP

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

Upvotes: 2

Views: 2878

Answers (3)

tomcc84
tomcc84

Reputation: 31

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

Upvotes: 1

Mudassar Khani
Mudassar Khani

Reputation: 1479

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

Upvotes: 1

Nishant Nair
Nishant Nair

Reputation: 2007

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

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

Upvotes: 2

Related Questions