在未找到数据时使用foreach循环获取错误

时间:2017-07-14 05:01:55

标签: php codeigniter

当没有结果集时,我正在调整此Arry:

Array
(
    [data] => Array
        (
            [res] => 
        )

)

当结果集出现时,我正在调整此Arry:

Array
(
    [data] => Array
        (
            [res] => 1
            [rows] => Array
                (
                    [0] => stdClass Object
                        (
                            [aprtid] => 11
                            [bldcode] => a
                            [buldname] => cd 
                            [aptno] => 901
                            [aptcore] => 2
                            [aptfloor] => 2
                            [buldsiteid] => 11
                            [rsdntname] => Gaurav
                            [rsdntemail] => Gaurav@gmail.com
                            [rsdntphone] => 9891110987
                            [rsdntpic] => 1498461013.jpg
                            [accessperson] => Ankit
                        )

After that i did this to recieved data :

  <?php
    $value=$data['rows'];

    ?>
its giving me that array 

    Array
    (
        [0] => stdClass Object

            (
                [aprtid] => 11
                [bldcode] => A_12
                [buldname] => BT Tower 
                [aptno] => 901
                [aptcore] => 2
                [aptfloor] => 2
                [buldsiteid] => 11
                [rsdntname] => Pankaj
                [rsdntemail] => pankaj@gmail.com
                [rsdntphone] => 9876543219
                [rsdntpic] => 
                [accessperson] => Ankit
            )


for accessing this data i used foreach cos there can be multiple records so i used that.

    <?php

    foreach ($value as $data) {
    ?>
    <tr>
     <td> <img src='<?=BASE_URL?>assets/images/<?= (!empty($data->rsdntpic)) ? $data->rsdntpic : '' ?>'> </td> 
    <td> <?= (!empty($data->name)) ? $data->name : 'No Records' ?> </td>
    <td> <?=(!empty($data->email)) ? $data->email : 'No Records' ?></td>
    <td> <?= (!empty($data->phone)) ? $data->phone : 'No Records' ?> </td>
    </tr>
    <?php } ?>
  

我的问题是,当我收到数据时,它正在工作的gud和   很好,但行不在数组中,因为我告诉你上面给我很多   像错误一样       严重性:注意       消息:未定义的索引:行

OR 

Severity: Warning
Message: Invalid argument supplied for foreach()

2 个答案:

答案 0 :(得分:2)

使用php isset()检查value is set and not null是否在将您的数据重复为isset($data['rows'])

之前
<?php

    if(isset($data['rows'])) {
        $value=$data['rows'];
        foreach ($value as $data) {
    ?>
    <tr>
        <td> <img src='<?=BASE_URL?>assets/images/<?= (!empty($data->rsdntpic)) ? $data->rsdntpic : '' ?>'> </td> 
        <td> <?= (!empty($data->name)) ? $data->name : 'No Records' ?> </td>
        <td> <?=(!empty($data->email)) ? $data->email : 'No Records' ?></td>
        <td> <?= (!empty($data->phone)) ? $data->phone : 'No Records' ?> </td>
    </tr>
<?php }} ?>

答案 1 :(得分:0)

使用空函数检查数组是否有值。

yourJpanel.setDropTarget(new DropTarget() {

    @Override
    public synchronized void drop(DropTargetDropEvent dtde)
    {
        this.changeToNormal();
        //handle the drop ....
    }

    @Override
    public synchronized void dragEnter(DropTargetDragEvent dtde){

        //Change JPANEL background...
        yourJpanel.setBackground(Color.RED);
    }

    @Override
        public synchronized void dragExit(DropTargetEvent dtde) {
        this.changeToNormal();
    }

    private void changeToNormal() {   
        //Set background to normal...
        yourJpanel.setBackground(Color.WHITE);
    }
});