【问题标题】:php/mysql trying to output a single result in codeigniter?php/mysql 试图在 codeigniter 中输出单个结果?
【发布时间】:2017-09-07 11:56:44
【问题描述】:

我是 codeigniter 的新手,并试图将百分比结果输出到我的项目的 codeigntier 视图中。一旦我解决了这个问题,事情就会顺利得多。 他是模型函数:

<?php

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

public function percent_specilation1($numstudent)
{
    $query = $this->db->query("SELECT * FROM Student WHERE Student.JRNOption ='1'");

    $numstudent = $query->num_rows()/25; 

    if($query->num_rows() > 0){
        return $numstudent;
    }
    return null; 
}

?>

这里是控制器。

<?php
class Statscontroller extends CI_Controller {
public function _contruct(){
    parent::_construct();
    $this->load->model('Stats_model');
}
public function index(){

    //echo "Various stats for the Journalism department:";
    $data['Test'] = $this->Stats_model->percent_specilation1($numstudent);
  }
   public function view($page = 'Statsview')
      {
            if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
            {
                    // Whoops, we don't have a page for that!
                    show_404();
            }

            //$data['title'] = ucfirst($page); 

           $this->load->view('templates/header',$data);
   $this->load->view('Statscontroller/index',$data);
   $this->load->view('templates/header');
    }
 ?>

  Here is the view
   <html>
   <h1> Various Statistics for the Journalism department</h1>

   <?php
  echo "Percent of students that are undecided:";   
  echo $Test; ?>
   </html>

我做错了什么?我试图让它正确一个多小时,我只想从数据库查询中输出单个结果。您可以提供的任何帮助都会很棒..请尊重。谢谢!

【问题讨论】:

标签: php html sql codeigniter view


【解决方案1】:

试试这段代码,我更新了控制器和模型的一些代码,并在模型查询中使用student表的主键student_id

更新模型:

<?php

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

public function percent_specilation1()
{
    $query = $this->db->query("SELECT (SELECT COUNT(s2.student_id) 
            FROM Student s2
            WHERE s2.JRNOption='1')*100/COUNT(s1.student_id) AS percentage
            FROM Student s1");

    $result = $query->row_array(); 

    return $result['percentage']; 
}

?>

更新的控制器:

<?php
class Statscontroller extends CI_Controller {
    public function _contruct(){
        parent::_construct();
        $this->load->model('Stats_model');
    }
    public function index(){

        //echo "Various stats for the Journalism department:";
        $data['Test'] = $this->Stats_model->percent_specilation1();
        $this->load->view('templates/header',$data);
        $this->load->view('Statscontroller/index',$data);
        $this->load->view('templates/header');
    }


}
?>

希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2012-07-04
    相关资源
    最近更新 更多