2016-02-28 71 views
-1

如何從數據庫表中的每個主題盈他們的名字檢索學生的分數..PHP,笨,MySQL的

我的模型

public function getscore($class, $term, $session) 
{ 
    $query = $this->db->query("SELECT scores FROM allscores WHERE class ='$class' AND term ='$term' AND session ='$session' AND  scores !=0"); 
    return $query; 
} 

控制器

public function showscores() 
{ 
    $class=$this->input->post('classes'); 
    $term=$this->input->post('term'); 
    $session=$this->input->post('session'); 
    $data['query_scores'] = $this->model->getscore($class,$term, $session); 
    $this->load->view('scoresheet', $data); 
} 

視圖頁面

<table> 
    <tr> 
    <?php 
     foreach ($query_scores->result() as $row) 
     { 
    ?> 
      <td><?php echo $row->scores; ?></td> 
    <?php 
     } 
    ?> 
    </tr> 
</table> 

注意:stu凹痕名稱在d頁面的左側水平列出,主題名稱垂直顯示爲標題。 我的挑戰是在每個學生的d科目名稱下顯示每個科目的分數。 您會很樂意在這方面提供幫助。

+0

這是$ query_scores-> result()返回正確的數組, –

回答

0
public function getscore($class, $term, $session) 
{ 
    $query = $this->db->query("SELECT scores FROM allscores WHERE class ='$class' AND term ='$term' AND session ='$session' AND  scores !=0"); 
    return $query-result(); 
} 

使用result()格式化並從數據庫中顯示的東西。

0

使用下面的表格格式顯示科目名稱

<table> 
    <thead> 
    <tr> 
     <td> SUB1 </td> <td> SUB2 </td> <td> SUB3 </td> 
    </tr> 
    </thead> 

現在用你的代碼對應的內容。 顯示主題得分的確切PHP代碼將取決於您的數據庫結構。有關更多說明,請分享您的數據庫表結構。

0

試試這個。

型號:
注:改變字段名稱,查詢結構來匹配你的

normaly這將需要加入,但,這是一個基本的例子,你得到的想法

public function getscore($class, $term, $session){ 
     $query = $this->db->query("SELECT scores,student_id ,student_name, subject_id,subject_name , FROM allscores WHERE class ='$class' AND term ='$term' AND session ='$session' AND scores !=0"); 
     return $query; 
    } 

控制器:

public function showscores(){ 
     $class=$this->input->post('classes'); 
     $term=$this->input->post('term'); 
     $session=$this->input->post('session'); 
     $data['query_scores'] = $this->model->getscore($class,$term, $session); 

     // create array to hold the new organized data; 
     $list = array(); 
     // list['scores'] will hold the student scores; 
     // list['students'] = will hold the student names 
     // list['subjects'] = will hold the subjects names 

     // loop throught the result 
     foreach ($query_scores->result() as $row){ 
     $list['scores'][$row->student_id][$row->subject_id] = $row->scores; 
     $list['students'][$row->student_id]= $row->student_name; 
     $list['subjects'][$row->subject_id]= $row->subject_name; 
     } 

     $this->load->view('scoresheet', $list); 
    } 

查看頁面:

<table> 
     <tr> 
    <?php 
     foreach ($subjects as $subject){ 
     echo "<td>".$subject."</td>"; // will display subjects names on the first row 
     } 
    ?> </tr> <?php 
     $column = 0; 
     foreach ($scores as $key=>$element){ 
     echo "<tr>"; 
     echo "<td>".$students[$key]."</td>"; // will display student names on the first col 
     foreach ($element as $subKey=>$subelement){ 
      echo "<td>".$subelement."</td>"; 
     } 
     echo "<tr>"; 
    <?php 
     } ?> 
</table>