从数据库codeigniter中选择平均值

时间:2016-06-02 08:38:21

标签: php mysql codeigniter

我正在尝试使用codeigniter从我的数据库中的几列中选择平均值。但我只是设法显示我的数据库中最后一列的平均值。这有什么问题?

查看

<!--$noise is the field form database!-->
<div id = "conclusion">

<fieldset>
<h1>Tourism Rating</h1><br>
<table cellpadding = "0" cellspacing="0" border="0"><div id = "rating">

<?php foreach ($post as $row): ?>
<tr> 
    <td>Overall Rating</td>
    <td>:</td>
    <td><?php echo $row->rating;?></td>
 </tr>
 <?php endforeach; ?>

 <?php foreach ($post as $row): ?>
 <tr> 
    <td>% Annoying Noise</td>
    <td>:</td>
    <td><?php echo $row->noise;?></td>
 </tr>
 <?php endforeach; ?>

 <?php foreach ($post as $row): ?>
 <tr> 
    <td>Air Quality</td>
    <td>:</td>
    <td><?php echo $row->air;?></td>
 </tr>
 <?php endforeach; ?>

 <?php foreach ($post as $row): ?>
 <tr> 
    <td>Water Quality</td>
    <td>:</td>
    <td><?php echo $row->water;?></td>
 </tr>
 <?php endforeach; ?>

 <?php foreach ($post as $row): ?>
 <tr> 
    <td>Facilities</td>
    <td>:</td>
    <td><?php echo $row->facility;?></td>
 </tr>
 <?php endforeach; ?>

 <?php foreach ($post as $row): ?>
 <tr> 
    <td>Friendliness</td>
    <td>:</td>
    <td><?php echo $row->friend;?></td>
 </tr>
 <?php endforeach; ?>
 </table>
 </fieldset>
 </div>

控制器

<?php
class viewreview extends CI_Controller {

public function view($page = 'viewreview') //writereview page folder name
{
    $this->load->model('viewreview_model');
    $data['query'] = $this->viewreview_model->get_data();
    $this->load->vars($data);

    $data['post'] = $this->viewreview_model->tourismrating();
    $this->load->vars($data);

    $data['post'] = $this->viewreview_model->tourismnoise();
    $this->load->vars($data);

    $data['post'] = $this->viewreview_model->tourismair();
    $this->load->vars($data);

    $data['post'] = $this->viewreview_model->tourismwater();
    $this->load->vars($data);

    $data['post'] = $this->viewreview_model->tourismfacility();
    $this->load->vars($data);

    $data['post'] = $this->viewreview_model->tourismfriend();
    $this->load->vars($data);

    if ( ! file_exists('application/views/viewreview/'.$page.'.php')) //link
    {
        // Whoops, we don't have a page for that!
        show_404();
    }

    $data['title'] = 'View Review'; 
    //$data['title'] = ucfirst($page); // Capitalize the first letter
    $this->load->helper('html');
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->view('templates/header', $data);
    $this->load->view('viewreview/'.$page, $data);
    $this->load->view('templates/footer', $data);
   }
}
?>

模型

<?php
class viewreview_model extends CI_Model {

public function __construct()
{
    $this->load->database();
}

public function get_data()
{
    $match = $this->input->post('search');
    $this->db->like('sitename',$match);
    $this->db->or_like('titlereview',$match);
    $this->db->or_like('yourreview',$match);
    $this->db->or_like('suggestion',$match);

    $query = $this->db->get('tourism');     //pass data to query
    return $query->result();
}

public function tourismrating()
{
    $match = $this->input->post('search');
    $this->db->select('*');
    $this->db->from('tourism');
    $this->db->select_avg('rating');
    $this->db->where('sitename', $match);       
    $post = $this->db->get();
    return $post->result();
}

public function tourismnoise()
{
    $match = $this->input->post('search');
    $this->db->select('*');
    $this->db->from('tourism');
    $this->db->select_avg('noise');
    $this->db->where('noise', $match);      
    $post = $this->db->get();
    return $post->result();
}

public function tourismair()
{
    $match = $this->input->post('search');
    $this->db->select('*');
    $this->db->from('tourism');
    $this->db->select_avg('air');
    $this->db->where('sitename', $match);       
    $post = $this->db->get();
    return $post->result();
}

public function tourismwater()
{
    $match = $this->input->post('search');
    $this->db->select('*');
    $this->db->from('tourism');
    $this->db->select_avg('water');
    $this->db->where('sitename', $match);       
    $post = $this->db->get();
    return $post->result();
}

public function tourismfacility()
{
    $match = $this->input->post('search');
    $this->db->select('*');
    $this->db->from('tourism');
    $this->db->select_avg('facility');
    $this->db->where('sitename', $match);       
    $post = $this->db->get();
    return $post->result();
}

public function tourismfriend()
{
    $match = $this->input->post('search');
    $this->db->select('*');
    $this->db->from('tourism');
    $this->db->select_avg('friend');
    $this->db->where('sitename', $match);       
    $post = $this->db->get();
    return $post->result();
  }

}
?>

0 个答案:

没有答案