php/mysql尝试在codeigniter中输出单个结果?

时间:2022-05-06 21:53:55

I am new to codeigniter and trying to get a percentage result outputted into the view of codeigntier for my project. Once I fix this, things will be much smoother. He is the model function:

我是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; 
}

?>

Here is the controller.

这是控制器。

<?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>

What am I doing wrong? I been trying to get it right for over an hour and I just want to output the single result from the database query. Any help you can provide would be great.. please be respectful. Thanks!

我做错了什么?我试着让它正确运行一个多小时,我只想输出数据库查询中的单个结果。你能提供的任何帮助都是好的。请尊重。谢谢!

1 个解决方案

#1


0  

Try this code, I have updated some of code of controller and model and use primary key of student table for student_id in model query.

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

Update Model:

更新模型:

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

?>

Updated controller:

更新控制器:

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


}
?>

Hope this will help you.

希望这对你有帮助。

#1


0  

Try this code, I have updated some of code of controller and model and use primary key of student table for student_id in model query.

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

Update Model:

更新模型:

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

?>

Updated controller:

更新控制器:

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


}
?>

Hope this will help you.

希望这对你有帮助。