2016-04-30 15 views
0

配列の値を表示したいが、配列のすべての値ではなく、配列の1つの値しか表示しない。Codeigniter配列表示

モデル:

function get_subject_position($exam_id , $class_id , $subject_id) { 

     $this->db->where('exam_id' , $exam_id); 
     $this->db->where('class_id' , $class_id); 
     $this->db->where('subject_id' , $subject_id); 
     $this->db->select('mark_obtained'); 
     $this->db->order_by('mark_obtained DESC'); 
     $subject_position = $this->db->get('mark')->result_array(); 
     foreach($subject_position as $row) { 

      return $row;  
     } 
     } 

ビュー:

今、それは最初の$行を返していますし、その後のforeachを停止するので、あなたは、あなたのビューに "foreachの" ループを移動する必要が
$subject_pos = $this->crud_model->get_subject_position($row2['exam_id'], $class_id, $row3['subject_id']); 

<td style="text-align: center;"><?php echo $subject_pos['mark_obtained'];?></td> 

答えて

1

ループ( "リターン" は停止してループ)

例:

ビュー

<?php 
$subject_pos = $this->crud_model->get_subject_position($row2['exam_id'], $class_id, $row3['subject_id']); 
foreach($subject_pos as $row) { 
?> 
<tr> 
    <td style="text-align: center;"><?=$row['mark_obtained']?></td> 
</tr> 
<?php } ?> 

あなたは結果配列のreturn文を送っdoes'n機能

function get_subject_position($exam_id , $class_id , $subject_id) { 

    $this->db->where('exam_id' , $exam_id); 
    $this->db->where('class_id' , $class_id); 
    $this->db->where('subject_id' , $subject_id); 
    $this->db->select('mark_obtained'); 
    $this->db->order_by('mark_obtained DESC'); 

    return $this->db->get('mark')->result_array(); // Get ALL rows 

    } 
+0

この戻りのようにする必要があります$ this-> DB->( 'マーク')を取得 - > result_array(); –

+0

@RanjeetSinghありがとう、コードを更新しました – prasoc

+0

ありがとうございました。ありがとうございました – 4Jean