简体   繁体   中英

how to get the data from array when multiple model functions pass to the view from the controller

I have a controller it take multiple model function results and pass it to controller I did it like this

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

what I need to know is, it gives error saying it cannot identify $noOfReviews .

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

        }

I need to know how to retrieve multiple model function data in view, and $noOfReviews only gives the no of reviews, a user has given. So their, without using a foreach loop how can i get the value of that, using a foreach loop is not necessary here.

Try this:
model:

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.';
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM