doufu9947 2013-06-10 07:10
浏览 28

Foreach错误变量无法识别

i have this function

function c_del()
{
    $session_data = $this->session->userdata('logged_in');
    $uname  = $session_data['username'];
    $query  = $this->user_m->viewDetail($uname);
    foreach($query as $row)
        {
            $username=$row->username;
        }
    $id_calon_reg=$_GET['a'];
    $query1 = $this->candidate_m->del_calon($id_calon_reg);
    $query3 = $this->candidate_m->search_calon($id_calon_reg);
    foreach($query3 as $row)
        {
            $foto_calon=$row->foto_calon;
        }
    unlink($foto_calon);
    $query2 = $this->candidate_m->viewAll();
    $data=array(
        "query"=>$query2,
        "username"=>$username
    );
    $this->load->helper(array('form'));
    $this->load->view('candidate_view',$data);
}

i want to unlink the path stored in $foto_calon, but i get this error

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: foto_calon

Filename: controllers/candidate.php

Line Number: 67

line 67 is where i call unlink function.

but I already define the variable $foto_calon in foreach.

the first foreach when i want to store the username into $username its success but got error in the second foreach.

i can't find out what's the problem. can anybody tell me?

  • 写回答

2条回答 默认 最新

  • dongnao9525 2013-06-10 07:12
    关注

    Try to replace

    foreach($query3 as $row)
    {
       $foto_calon=$row->foto_calon;
    }
    

    with

    if(is_array($query3)) {
        foreach($query3 as $row)
        {
            $foto_calon=$row['foto_calon'];
        }
    }
    

    And if you want to unlink each row of foto_calon in the result array then put that unlink in this foreach loop like

    if(is_array($query3)) {
        foreach($query3 as $row)
        {
            $foto_calon=$row['foto_calon'];
            unlink($foto_calon);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源