codeigniter,从模型中回显数据以查看PHP

时间:2017-04-10 05:15:52

标签: php codeigniter

我查看了本网站上的所有相关数据,试图找到一个适合我的解决方案但是,我还没找到。在用尽所有选择(尝试)之后,我已经在这几个小时了。我终于决定寻求帮助了。我需要将模型中的数据回显到视图中以查看我是否正确执行此操作(测试)。我正在尝试学习这个框架(Codeigniter)我自己的。我有以下设置...

控制器

<?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');

}
}
?>

模型

<?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
?>

查看

 <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>

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:2)

要查看视图中的数据,您需要通过控制器传入

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

答案 1 :(得分:1)

您需要在调用视图的函数中获取数据,然后将该数据传递给视图。例如在你的控制器

Option Explicit

Sub UniqueJohns()

Dim JohnsArr As Variant
Dim i As Long
Dim Dict As Object
Dim Result As Long

Set Dict = CreateObject("Scripting.Dictionary")

' read into array, to run faster
JohnsArr = Application.Transpose(Range("A1:A5"))

' loop through array elements
For i = 1 To UBound(JohnsArr)
    If JohnsArr(i) Like "John*" Then
        ' check if already exists in Dictionary
        If Not Dict.exists(JohnsArr(i)) Then
            Dict.Add JohnsArr(i), JohnsArr(i)
        End If
    End If
Next i
Result = UBound(Dict.keys) ' <-- this is the result, unique keys in Dictionary
MsgBox Result

End Sub

在您的模型中

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

在您的视图中

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

对于文件上传或表单发布,您可能需要查看mergeForm Validation

答案 2 :(得分:1)

好的,让它使用以下内容:

控制器

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

查看

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

看起来我们也很好,csv正在解析为JSON