'未定义的索引'使用foreach循环时出错

时间:2011-12-02 05:36:25

标签: php codeigniter foreach

我有2张名为preacher& amp;讲道
传道人的领域

    preacher_id 
    first_name 
    last_name 
    preacher_image 
    preacher_logo 
    preacher_bio_brief 
    category

布道领域

    sermon_id
    preacher_id 
    sermon_title 
    sermon_image 
    audio_file  
    sermon_description 
    sort_order

我想按照传道人first_name的顺序显示每位传道人的所有布道。我得到了正确但也得到了以下错误

A PHP Error was encountered

Severity: Notice

Message: Undefined index: 1

Filename: home/sermons_view.php

Line Number: 27

控制器中的方法

function index() {
    $res = $this->sermon_model->viewAllpreachers();
    $this->data['preachers'] = $res;
    $this->data['page'] = $this->config->item('APP_template_dir') . 'site/home/         sermons_view';
    $this->load->vars($this->data);
    $this->load->view($this->_container);
}

模型中的方法

   function viewAllpreachers() {

        $preacher = array();
        $this->db->select('*');
        $this->db->from('preacher');
        $this->db->order_by('first_name');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                 $preacher[$row->preacher_id]['preacher_id'] = $row->preacher_id;
                $preacher[$row->preacher_id]['preacher_name'] = $row->first_name . ' ' . $row->last_name;
                $preacher[$row->preacher_id]['preacher_image'] = $row->preacher_image;
                $preacher[$row->preacher_id]['preacher_bio_brief'] = $row->preacher_bio_brief;

        $this->db->select('*');
        $this->db->from('sermons');
        $this->db->where('preacher_id',$row->preacher_id);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row1) {
                $preacher[$row1->preacher_id][$row1->sermon_id]['sermon_id'] = $row1->sermon_id;
                $preacher[$row1->preacher_id][$row1->sermon_id]['preacher_id'] = $row1->preacher_id;
                $preacher[$row1->preacher_id][$row1->sermon_id]['sermon_image'] = $row1->sermon_image;
                $preacher[$row1->preacher_id][$row1->sermon_id]['sermon_title'] = $row1->sermon_title;
                $preacher[$row1->preacher_id][$row1->sermon_id]['audio_file'] = $row1->audio_file;
                $preacher[$row1->preacher_id][$row1->sermon_id]['sermon_description'] = $row1->sermon_description;
            }
                    }
            }
            return $preacher;
        }
        return false;
    }

视图中的代码

<?php
            if ($preachers) {
                foreach ($preachers as $val) {
            ?>
                    <tr>
                        <td><img src="<?= base_url(); ?>uploads/<?= $val['preacher_image']; ?>"/></td>
                        <td colspan="2"><?= $val['preacher_name']; ?></td>
                        <td colspan="2"><?= $val['preacher_bio_brief']; ?></td>
                    </tr>
            <?
                    echo '<pre>';
                    //print_r($val);
                    echo '</pre>';
                    foreach ($val as $val1) {
            ?>
                        <tr>
                            <td style="padding-left: 50px; "><img src="<?= base_url(); ?>uploads/<?= $val1['sermon_image']; ?>" style="width: 60px;height: 60px;"/></td>
                            <td><?= $val1['sermon_title']; ?></td>
                            <td><?= $val1['audio_file']; ?></td>
                            <td width="250px"><?php $res = explode('/', $val1['audio_file']);
                        if ($val1['audio_file']) {
 ?><a id="play" href="<?= site_url('sermons/playmp3/' . $val1['sermon_id']); ?>"><?= $res['1']; ?></a><?php } ?></td>
                    <td><?= $val1['audio_file']; ?></td>
                </tr>
            <?
                    }
                }
            }
            ?>

我认为结果数组存在问题 我得到了那个

Array
(
 [21] => Array
        (
            [preacher_id] => 21
            [preacher_name] => Vance Havner
            [preacher_image] => profile_image/havner.jpeg
            [preacher_bio_brief] => this is for testing 
            [42] => Array
                (
                    [sermon_id] => 42
                    [preacher_id] => 21
                    [sermon_image] => sermon_image/image_81322797345.jpg
                    [sermon_title] => 3 notes of the devil's tales
                    [audio_file] => audio_file/Niranja_Mizhiyum1.mp3
                    [sermon_description] => 3 notes of the devil's tales   
                )

            [41] => Array
                (
                    [sermon_id] => 41
                    [preacher_id] => 21
                    [sermon_image] => 
                    [sermon_title] => The Lordship of Christs
                    [audio_file] => audio_file/Naladhamayanthi_Kadhayile.mp3
                    [sermon_description] => the lordship of christ 
                )

        )

)

1 个答案:

答案 0 :(得分:1)

$res

中没有model

首先使用$res

调试print_r($res)

AFAIK必须有$val1['sermon_title']而不是$res['1']