当多个模型函数从控制器

时间:2016-04-08 14:29:27

标签: php codeigniter

我有一个控制器它需要多个模型函数结果并将其传递给控制器​​我就像这样做了

adpreview_ctrl.php
        public function showBusinessReviews($vehicleid){


            $data=array();
            $data['details']=$this->ads_model->getBusinessReviews($vehicleid);
            $data['noOfReviews']=$this->ads_model->countReviews($vehicleid);
            $this->load->view('pages/templates/header');
            $this->load->view('pages/viewReviews',$data);
            $this->load->view('pages/templates/footer');
        }
    public function countBusinessReviews($vehicleid){
        $data['details']=$this->ads_model->countReviews($vehicleid);
        $this->load->view('pages/templates/header');
            $this->load->view('pages/viewReviews',$data);
            $this->load->view('pages/templates/footer');
        }
    }

viewReviews.php

    <?php 

    foreach($noOfReviews as $reviewAmount){
    echo $reviewAmount.'Reviews';

    }

    foreach($details as $review){
        $Breview=$review->rating;


    if($details==null)
    {?>



    <?php echo '<center><b><h3>No any reviews has been posted yet!</h3></b></center>';?>
            <a href="<?php echo base_url().'adpreview_ctrl/getad_preview'.'/'.$review->Vehicleid; ?>"><input type="submit" name="ok" class="btn btn-primary btn-lg" value="ok"></a>


         <?php }
        else{
            ?>

    Ads_model.php

    public function getBusinessReviews($Vehicleid){
            $status="Approved";
            $this->db->select("*");
            $query=$this->db->where('Vehicleid',$Vehicleid);
            $query=$this->db->where('Status',$status);  
            $query=$this->db->get('businessreviews');
            return $query->result();        
        }


    public function countReviews($Vehicleid){
        $status="Approved";
        $this->db->select("*");
            $query=$this->db->where('Vehicleid',$Vehicleid);
            $query=$this->db->where('Status',$status);  
            $query=$this->db->get('businessreviews');
        return $query->num_rows(); 
    }

我需要知道的是,它错误地说它无法识别$noOfReviews

  foreach($noOfReviews as $reviewAmount){
        echo $reviewAmount.'Reviews';

        }

我需要知道如何在视图中检索多个模型函数数据, 并且$noOfReviews仅给出用户给出的评论。所以他们在没有使用foreach循环的情况下如何获得它的值,这里不需要使用foreach循环。

1 个答案:

答案 0 :(得分:1)

试试这个:
模型:

public function countReviews($Vehicleid) {
    $status = "Approved";
    $this->db->select("*");
    $query = $this->db->where('Vehicleid', $Vehicleid);
    $query = $this->db->where('Status', $status);
    // it will return with only the number of data
    return $query->count_all_results('businessreviews');
}

viewReviews.php

<?php
    // show number of reviews
    echo $noOfReviews . 'Reviews';

    // show details if exists
    if(!empty($details)) {
        foreach($details as $review) {
            // echo what you want
        }
    } else { 
        echo 'No details.';
    }
?>