简体   繁体   English

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

[英]php/mysql trying to output a single result in codeigniter?

I am new to codeigniter and trying to get a percentage result outputted into the view of codeigntier for my project. 我是codeigniter的新手,并尝试将百分比结果输出到我的项目的codeigntier视图中。 Once I fix this, things will be much smoother. 一旦我解决了这个问题,事情就会变得更顺畅。 He is the model function: 他是模特的职能:

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

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 table的主键for student_id

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. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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