foreach返回警告:非法字符串偏移' model_id'

时间:2017-07-18 22:15:48

标签: php foreach opencart

我试图从模型控制器调用make_id,但每次我得到非法字符串时,请帮我解决这个问题,我做错了什么?

模型代码:

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

  return $query->row;
}

来自控制器的代码:

$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)
        );
        }
来自视图的

代码:

        <?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而没有错误,但是当我有记录显示我得到非法的字符串偏移&#39; model_id&#39;&#39; name&#39; &#39; mark_id&#39; &#39;状态&#39; &#39;类别,&#39;从今天早上开始我尝试修复此问题,但我不能,这是开放式购物车。

1 个答案:

答案 0 :(得分: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)