dousi5501 2017-07-18 22:15
浏览 74
已采纳

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

I am trying to call the make_id from the model controller, but every time I just get illegal string, please help me with this, what I am doing wrong?

code from model :

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

  return $query->row;
}

code from controller :

$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)
        );
        }

code from view :

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

When don't have results I get the $text_no_results with no errors, but when I have records to show I get Illegal string offset 'model_id''name' 'mark_id' 'status' 'category,'from this morning I trying to get this fix, but I can't, this is open cart.

  • 写回答

1条回答 默认 最新

  • dongshi6844 2017-07-18 22:52
    关注

    Try this:

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

    And replace this:

    if ($models)
    

    to:

    if (count($models)>0)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒