在codeigniter模型中為foreach()提供的參數無效

[英]Invalid argument supplied for foreach() in codeigniter model


function mostViewedTracks() {
    $this->load->database();        
    $this->db->select("*");
    $this->db->from("file");
    $this->db->order_by("views", "desc");
    $this->db->limit(8);
    $query = $this->db->get();
    if($query->num_rows() > 0)
    {
        $results = $query->result();
    }
    return array();
}

when i run this model it gives error that invalid argument supplied for foreach()

當我運行這個模型時,它給出了為foreach()提供的無效參數的錯誤

this is my view code:

這是我的觀看代碼:

    <?php if( !empty($most_viewed) ) { ?>
    <?php foreach($most_viewed as $row): ?>
    <a href="<?php echo base_url('index.php/home/track');?>/<?php echo $row->id;?>" class="list-group-item">
    <img class="track-list-img" src="<?php echo base_url('assets/img/music');?>/<?php echo $row->id;?>.jpg">
    <div class="list-track-infoo">
    <h4 class="list-group-item-heading"><?php echo $row->title;?><span class="badge"><?php echo $row->views;?><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></span> </h4>
    <p class="list-group-item-text"><?php echo $row->singer;?></p>
    </div>
    </a>
    <?php endforeach; ?>
    <?php} ?>

Updated

Controller

function track() {
    $id = $this->uri->segment(3);
    $data1['track'] = $this->view_models->track($id);
    $data1['most_viewed'] = $this->view_models->mostViewedTracks();
    $this->load->view('includes/header');
    $this->load->view('track', $data1);
    $this->load->view('includes/footer');
}

I have tried my best to solve this problem but all in vain

我盡力解決這個問題,但一切都是徒勞的

3 个解决方案

#1


1  

You need to change two mistakes,

你需要改變兩個錯誤,

You should return the `$result` array in your model like,

$results = $query->result();
return $results;

And in foreach change $row1 to $row which produces the error. see,

並在foreach中將$ row1更改為$ row,從而產生錯誤。看到,

<?php foreach($most_viewed as $row): ?>

#2


1  

Instead of return array(); you have to return result set from models file

而不是返回array();你必須從模型文件返回結果集

  if($query->num_rows() > 0)
    {
         return $query->result();// return result set 
    }else{
          return FALSE;//
    }

IN controller you have to call your models function as

在控制器中,您必須將模型調用為

$data1['most_viewed'] = $this->view_models->getFeaturedTracks();// change mostViewedTracks to getFeaturedTracks

In view use it as

在視圖中使用它

 <?php foreach($most_viewed as $row): ?>// remove $row1 to $row

#3


0  

Function should like this....

功能應該像這樣....

function getFeaturedTracks() {
    $results = array();
    $this->load->database();        
    $this->db->select("*");
    $this->db->from("file");
    $this->db->order_by("views", "desc");
    $this->db->limit(8);
    $query = $this->db->get();
    if($query->num_rows() > 0)
    {
        $results = $query->result();
    }
    return $results;
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/07/29/72a3983dff92848e053497e30823f974.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com