dquoj04882 2017-07-25 07:14
浏览 46

即使数据在codeigniter中的数组中,也会出现未定义的索引问题

I am accessing an array object being transferred from model to view. When I print that array it shows complete info what I want, but when I implement foreach on that array and try to access an index it gives me an error of undefined index.

the model code is

public function fetch()
        {

$queryy=$this->db->select('*')
            ->from('selleritem')
            ->get();
            $data=$queryy->result();

            foreach($data as $datas)
            {
                $pid=$datas->pid ;
$query= $this->db->limit('1')
->select('*')
     ->where('pid', $pid)             
  ->get('selleritemimages'); 

    if($query->num_rows() > 0){
         foreach ($query->result() as $row)
         {
            $data[] = $row;
         }
        return $data;
        //print_r($data);
    }
}

the controller code is:

public function index()
    {

        $this->load->model('fetchindex');

        $fetch= array($this->fetchindex->fetch());  

        $this->load->view('welcome_message',['fetch'=>$fetch]);

    }
}

the view code is printing the array in view shows .

print_r($fetch);

    foreach ( $fetch as $ff) {

    echo $ff["pid"].'hh<br>';

    }   

printing the array in view shows .

Array ( 
    [0] => Array 
    ( 
        [0] => stdClass Object 
        ( 
            [id] => 2 
            [name] => fashionable kurta for men 
            [gender] => groom 
            [price] => 11213 
            [description] => Item Description 
            [rent_sale] => Rent 
            [features] => Key Features 
            [pid] => 4 
            [product] => kurta 
            [available] => 9 
            [uid] => 2 
            [ucid] => 7 
            [rating] => 0 
            [rating_users] => 0 
        ) 
        [1] => stdClass Object 
        ( 
            [id] => 1 
            [name] => suit fro groom 
            [gender] => groom 
            [price] => 12345 
            [description] => Item Description 
            [rent_sale] => Sale 
            [features] => Key Features 
            [pid] => 3 
            [product] => suits 
            [available] => 6 
            [uid] => 2 
            [ucid] => 11 
            [rating] => 22 
            [rating_users] => 5 
        ) 
        [2] => stdClass Object 
        ( 
            [id] => 3 
            [name] => sherwani 
            [gender] => groom 
            [price] => 12133 
            [description] => Item Description 
            [rent_sale] => Rent 
            [features] => Key Features 
            [pid] => 5 
            [product] => sherwani 
            [available] => 10 
            [uid] => 4 
            [ucid] => 10 
            [rating] => 22 
            [rating_users] => 5 
        ) 
        [3] => stdClass Object 
        ( 
            [id] => 4 
            [name] => branded shoes for groom 
            [gender] => groom 
            [price] => 3232 
            [description] => Item Description 
            [rent_sale] => sale 
            [features] => Key Features 
            [pid] => 6 
            [product] => foot wear 
            [available] => 9 
            [uid] => 4 
            [ucid] => 12 
            [rating] => 22 
            [rating_users] => 5 
        ) 
        [4] => stdClass Object 
        ( 
            [id] => 5 
            [name] => watches for groom 
            [gender] => groom 
            [price] => 33232 
            [description] => Item Description 
            [rent_sale] => Sale 
            [features] => Key Features 
            [pid] => 7 
            [product] => watches 
            [available] => 10 
            [uid] => 2 
            [ucid] => 18 
            [rating] => 24 
            [rating_users] => 6 
        ) 
        [5] => stdClass Object 
        ( 
            [id] => 6 
            [name] => watches for groom 
            [gender] => groom 
            [price] => 12133 
            [description] => Item Description 
            [rent_sale] => Sale 
            [features] => Key Features 
            [pid] => 8 
            [product] => watches 
            [available] => 10 
            [uid] => 1 
            [ucid] => 18 
            [rating] => 22 
            [rating_users] => 5 
        ) 
        [6] => stdClass Object 
        ( 
            [id] => 9 
            [name] => Branded bags 
            [gender] => bride 
            [price] => 22353 
            [description] => branded bags for sale 
            [rent_sale] => Sale 
            [features] => branded bags for sale 
            [pid] => 9 
            [product] => bags 
            [available] => 7 
            [uid] => 1 
            [ucid] => 3 
            [rating] => 53 
            [rating_users] => 20 
        ) 
        [7] => stdClass Object 
        ( 
            [id] => 10 
            [name] => Branded bags 
            [gender] => bride 
            [price] => 22353 
            [description] => Item Description 
            [rent_sale] => Sale 
            [features] => Key Features 
            [pid] => 10 
            [product] => bags 
            [available] => 8 
            [uid] => 1 
            [ucid] => 3 
            [rating] => 39 
            [rating_users] => 16 
        ) 
        [8] => stdClass Object 
        ( 
            [id] => 11 
            [image] => 1582863614kurta.jpg 
            [pid] => 4 
            [uid] => 2 
        ) 
    ) 
)

Below is the error what I'm getting:

Severity: Notice Message: Undefined index: pid Filename: views/welcome_message.php Line Number: 33

  • 写回答

5条回答 默认 最新

  • dourao3960 2017-07-25 07:36
    关注

    Try this:

    $objects = json_decode($fetch);
    $finalResult = (Array)$objects[0];
    foreach($finalResult as $result){
        echo $result->pid.'<br />';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号