2016-02-12 83 views
0

我一直在努力,並沒有得到一個合適的解決方案。我想要做的是我有一個控制器的功能,從模型中獲取數據。 這是我的控制器功能:如何在控制器和視圖之間建立連接?

function index() { 

    $data['results1'] = $this->RetrieveData_model->GetData(); 

    $this->load->view('ViewData',$data); 


    } 

我想訪問$數據[「結果1」]變量在我EditView中自動填補fields.this是我的看法:

<div id="container"> 
      <?php echo form_open('insert_ctrl/updateData/'); ?> 
      <h1>Update Data Into Database Using CodeIgniter</h1><hr/> 
      <?php if (isset($message)) { ?> 
       <CENTER><h3 style="color:green;">Data updated successfully</h3></CENTER><br> 
      <?php } ?> 
       <?php echo form_label('Student Name :'); ?> <?php echo form_error('dname'); ?><br /> 
      <?php echo form_input(array('id' => 'dname', 'name' => 'dname', 'value' => $results1[0]->Student_Name)); ?><br /> 

      <?php echo form_label('Student Email :'); ?> <?php echo form_error('demail'); ?><br /> 
      <?php echo form_input(array('id' => 'demail', 'name' => 'demail', 'value' => $results1[0]->Student_Email)); ?><br /> 

      <?php echo form_label('Student Mobile No. :'); ?> <?php echo form_error('dmobile'); ?><br /> 
      <?php echo form_input(array('id' => 'dmobile', 'name' => 'dmobile', 'placeholder' => '10 Digit Mobile No.', 'value' => $results[0]->Student_Mobile)); ?><br /> 

      <?php echo form_label('Student Address :'); ?> <?php echo form_error('daddress'); ?><br /> 
      <?php echo form_input(array('id' => 'daddress', 'name' => 'daddress','value' => $results1[0]->Student_Address)); ?><br /> 

     <?php echo form_hidden(array('id' => 'studentId', 'name' => 'studentId','value' => $results1[0]->Student_id)); ?><br /> 

      <?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?> 
      <?php echo form_close(); ?><br/> 
      <div id="fugo"> 

      </div> 
    <button type="button" onclick="window.location='<?php echo base_url();?>ViewData_ctrl/index'"> 
      View data Record 
    </button> 

     </div> 

當我訪問的「RESULT1」鑑於這是給錯誤:

A PHP Error was encountered 

Severity: Notice 

Message: Undefined variable: results1 

Filename: views/EditView.php 

Line Number: 16 

Backtrace: 

File: D:\dev\htdocs\CI2\application\views\EditView.php 
Line: 16 
Function: _error_handler 

File: D:\dev\htdocs\CI2\application\controllers\insert_ctrl.php 
Line: 104 
Function: view 

File: D:\dev\htdocs\CI2\index.php 
Line: 292 
Function: require_once 

請幫助我如何解決這個,因爲我是新來的Codeignitor ..

+0

可以請你告訴我,在你的控制器顯示值不? – vaibhav

+0

檢查這個var_dump($ data);在你的控制器中 –

回答

1

檢查文件名

$this->load->view('ViewData',$data); 
    // change to 
    $this->load->view('EditView',$data); 
0

存在其他錯誤在你EditView中的文件: 您在$結果1陣列跳過「1」,它可以幫助你

$results1[0]->Student_Mobile; 
0

試試這個答案西港島線。你得到的結果在控制器很容易

function index() { 

    $data['results1'] = $this->RetrieveData_model->GetData(); 

    $this->load->view('ViewData',$data); 


    } 

在視圖頁面

<?php 
foreach($results1 as $row) 
{ 
    echo $row->id; //echo your fields like this 
} 
?> 
2

試試這個在你的控制器,你給錯誤的觀點名

function index() { 

    $data['results1'] = $this->RetrieveData_model->GetData(); 

    $this->load->view('EditView',$data); 


    } 

和視圖的var_dump您的陣列。

<?php 
var_dump($results1); 
?> 
0

鑑於頁面,您可以使用

<?php echo form_open('insert_ctrl/updateData/'); ?> 
    <?php 
    foreach($results1 as $row) 
    { 

    //try this to get details 

    echo $row->Student_Address; 
    echo $row->Student_id; 


    //or try this   

    echo $row['Student_Address']; 
    echo $row['Student_id']; 
    } 
    ?> 
相關問題