簡體   English   中英

php / mysql試圖在codeigniter中輸出單個結果?

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

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

我究竟做錯了什么? 我一直試圖讓它正確超過一個小時,我只想輸出數據庫查詢中的單個結果。 你能提供的任何幫助都會很棒..請尊重。 謝謝!

試試這段代碼,我更新了一些控制器和模型的代碼,並在模型查詢中使用student table的主鍵for 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');
    }


}
?>

希望這會幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM