2017-07-18 121 views
0

我想從模型控制器調用make_id,但每次我只是得到非法字符串,請幫助我這個,我做錯了什麼?從模型foreach返回警告:非法字符串偏移'model_id'

代碼:

$this->load->model('catalog/models'); 
$data['models'] = array(); 
$results = $this->model_catalog_models->getTotalModelsByMark($mark_info['mark_id']); 
foreach ($results as $result) { 
    $data['models'][] = array(
     'model_id' => $result['model_id'], 
     'name'   => $result['name'], 
     'mark_id'   => $result['mark_id'], 
     'status'   => $result['status'], 
     'category'   => $result['category'], 
     'edit'   => $this->url->link('catalog/models/edit', 'token=' . $this->session->data['token'] . '&model_id=' . $result['model_id'] . $url, true) 
     ); 
     } 

從視圖代碼:

public function getTotalModelsByMark($mark_id) { 
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "models WHERE mark_id = '" . (int)$mark_id . "'"); 

    return $query->row; 
} 

從控制器代碼

 <?php if ($models) { ?> 
     <?php foreach ($models as $model) { ?> 
     <tr> 
      <td class="text-center"><?php if (in_array($model['model_id'], $selected)) { ?> 
      <input type="checkbox" name="selected[]" value="<?php echo $model['model_id']; ?>" checked="checked" /> 
      <?php } else { ?> 
      <input type="checkbox" name="selected[]" value="<?php echo $model['model_id']; ?>" /> 
      <?php } ?></td> 
      <td class="text-left"><?php echo $model['name']; ?></td> 
      <td class="text-left"><?php echo $model['mark_id']; ?></td> 
      <td class="text-right"><?php echo $model['status']; ?></td> 
      <td class="text-right"><?php echo $model['category']; ?></td> 
      <td class="text-right"><a href="<?php echo $model['edit']; ?>" data-toggle="tooltip" title="<?php echo $button_edit; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a></td> 
     </tr> 
     <?php } ?> 
     <?php } else { ?> 
     <tr> 
      <td class="text-center" colspan="4"><?php echo $text_no_results; ?></td> 
     </tr> 
     <?php } ?> 

如果沒有結果我得到沒有$ text_no_results錯誤,但是當我有記錄顯示我得到非法字符串偏移量'model_id''name''mark_id''status''category,'fro米今天早上我試圖得到這個修復,但我不能,這是開放購物車。

+0

什麼是你的表名? – Rashid

+0

在db_models我有model_id,mark_id,名稱,狀態,類別,圖像和我得到非法字符串的所有錯誤 – rpV

+0

如果我嘗試獲取所有模型,它工作正常,但如果我想調用模型從一個特定的mark_id ,我得到非法的字符串錯誤 – rpV

回答

1

試試這個:

public function getTotalModelsByMark($mark_id) { 
    $sql = "SELECT * FROM ". DB_PREFIX."models WHERE mark_id={$mark_id}"; 
    return $this->db->query($sql)->rows(); 
} 

而且替換此:

if ($models) 

到:

if (count($models)>0) 
+0

致命的錯誤:調用未定義的方法DB :: select(),我使用opencart 2.3.0 – rpV

+0

現在我已經改變了函數getTotalModelsByMark(),PLZ再試一次。 – Rashid

+0

非常感謝Rashid的幫助,即使這樣也行不通,但是我讓它工作,問題在於:'return $ query-> row;'我改變了'return $ query-> rows;''再次感謝你 – rpV

相關問題