2014-02-19 50 views
0

嗨:在選擇框的代碼點火器中我想顯示數據庫中的內容。我無法接收來自數據庫的內容。我如何使用助手進行顯示。如果我使用助手,我得到以下錯誤。如何得到相同的。如何從幫助程序中的數據庫獲取內容

Fatal error: Using $this when not in object context in G:\xampp\htdocs\lokalpickup_ci\admin\application\helpers\lib_helper.php on line 79 
+1

添加您的代碼,其中您正在收到此錯誤 –

回答

0

我認爲你的問題是使用$ this。實際上,在幫助器中CodeIgniter中不能使用$ this。您需要將實例獲取到某個變量,然後可以像$ this一樣使用它。

$CI = & get_instance(); 
    $CI->load->model('Dynamic_dropdown', 'dd_model'); 

下面的代碼是從PHP代碼生成器生成的動態選擇控件。你可以使用這樣的東西。

function form_dynamic_dropdown($tablename, $fieldDetails, $valueColumn, $displayColumn, $default='') { 
     if (!is_array($fieldDetails)) { 
      log_message('error', 'Field details should be an array for dynamic dropdown', TRUE); 
      return; 
     } 
     $field_properties = '<select'; 
     foreach ($fieldDetails as $key => $value) { 
      $field_properties .= ' ' . $key . ' = \'' . $value . '\''; 
     } 
     $field_properties .= '>'; 

     $CI = & get_instance(); 
     $CI->load->model('Dynamic_dropdown', 'dd_model'); 
     $field_properties .= '<option value=\'\'>'; 
     $field_properties .= 'Select'; 
     $field_properties .= '</option>'; 

     if(isset($fieldDetails['name']) && isset($_POST[$fieldDetails['name']])) 
      $default = $_POST[$fieldDetails['name']]; 

     $condition = array(); 
     $results = $CI->dd_model->search($condition, $tablename); 
     foreach ($results as $result) { 
      if ($result->$valueColumn == $default) 
       $field_properties .= '<option value=\'' . $result->$valueColumn . '\' selected>'; 
      else 
       $field_properties .= '<option value=\'' . $result->$valueColumn . '\'>'; 
      $field_properties .= $result->$displayColumn; 
      $field_properties .= '</option>'; 
     } 
     $field_properties .= '</select>'; 
     return $field_properties; 
    } 
相關問題