【问题标题】:Codeigniter Array DisplayCodeigniter 数组显示
【发布时间】:2016-08-25 10:21:30
【问题描述】:

我想显示一个数组的值,但它只显示数组的一个值,而不是数组的所有值。

型号:

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

查看:

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

【问题讨论】:

    标签: codeigniter co


    【解决方案1】:

    您需要将“foreach”循环移动到您的视图中,因为现在它正在返回第一个 $row,然后停止 foreach 循环(“return”停止循环)

    例子:

    查看

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

    功能

    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
    
        }
    

    【讨论】:

    • 你不发送结果数组返回语句应该是这样的 return $this->db->get('mark')->result_array();
    • 非常感谢非常感谢
    • 我想使用 array_search() 来查找每个科目的分数位置,它不起作用我会问一个新问题
    猜你喜欢
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 2016-05-17
    相关资源
    最近更新 更多