视图codeigniter中的非法字符串偏移错误

时间:2014-11-20 06:50:30

标签: php codeigniter

当我尝试从视图中加载数据库时,它显示错误:

A PHP ERROR WAS ENCOUNTERED

SEVERITY: WARNING

MESSAGE: ILLEGAL STRING OFFSET 'NAME'

FILENAME: CLIENT/LISTVIEW.PHP

LINE NUMBER: 7

当我var_dump变量时,显示

STRING(1) "N"

中的结果

这是在我的控制器文件(client.php)

public function viewlistdetail()
{

      $id=base64_decode($_GET['id']);
    $this->load->helper('client');
    $this->load->model('client/Client_model');
    $this->Client_model->viewlistdetail($id);  
}

这是我的模型文件(client_model.php)

 function viewlistdetail($st)
{
    //echo $st;die;
    $data['page_title'] = 'List View';
    $data['detail']=$this->getlistdetail($st);

    $this->load->view('client/general/head',$data);
    $this->load->view('client/general/header');
    $this->load->view('client/listview',$data);
    $this->load->view('client/general/footer');
}
      public function getlistdetail($id){

       // $this->db->reconnect();
        try{

            $query1 = $this->db->query("SELECT id,name,detail,facility,address,city FROM listing WHERE id='$id'");
            $str1=array();

        if ($query1->num_rows() > 0)
        {
            $j=0;
           foreach ($query1->result() as $row1)
               {
                $str1=array("id"=>$row1->id,"name"=>$row1->name,"detail"=>$row1->detail,"facility"=>$row1->facility,"address"=>$row1->address);

                $str1['photo']=$this->getphoto($id);
                $str1['price']=$this->getprice($id);
                $str1['tag']=$this->gettag($id); 
                $j++;
               }
        }

        else
        {
            $str1="No record Found";

        }
        }
        catch (Exception $exd){
        $str1= "error";
        }
        return $str1;
    }

查看文件(listview.php)

    <h4><?php echo var_dump($detail['name']);?></h4>
    <span class="bussadd"><?php var_dump($detail['detail']);?><span>

2 个答案:

答案 0 :(得分:0)

我不确定这是你的答案。我想你会对我的回答有所了解。

将文件更改为

控制器中的

client.php

public function viewlistdetail()
{

      $id=base64_decode($_GET['id']);
    $this->load->helper('client');
    $this->load->model('client/Client_model');

     $data['page_title'] = 'List View';
    $data['detail']=$this->Client_model->getlistdetail($id); 

    if(isset($data['detail']))
    {
    $this->load->view('client/general/head',$data);
    $this->load->view('client/general/header');
    $this->load->view('client/listview',$data);
    $this->load->view('client/general/footer');
    }
    else
    die('No list to display');

}

在模型页面client_model.php

 public function getlistdetail($id){

   // $this->db->reconnect();
    try{

        $query1 = $this->db->query("SELECT id,name,detail,facility,address,city FROM listing WHERE id='$id'");
        $str1=array();

    if ($query1->num_rows() > 0)
    {
        $j=0;
       foreach ($query1->result() as $row1)
           {
            $str1=array("id"=>$row1->id,"name"=>$row1->name,"detail"=>$row1->detail,"facility"=>$row1->facility,"address"=>$row1->address);

            $str1['photo']=$this->getphoto($id);
            $str1['price']=$this->getprice($id);
            $str1['tag']=$this->gettag($id); 
            $j++;
           }
    }

    else
    {
        $str1="No record Found";

    }
    }
    catch (Exception $exd){
    $str1= "error";
    }
    return $str1;
}

您的观看文件

 <h4><?php var_dump($detail['name']);?></h4>
    <span class="bussadd"><?php var_dump($detail['detail']);?><span>

答案 1 :(得分:0)

以下代码可以在这里工作。到查看页面试试。

<h4><?php echo isset($detail['name']);?></h4>
<span class="bussadd"><?php isset($detail['detail']);?><span>

<h4><?php echo isset($detail['name']) ? isset($detail['name']) : ''; ?></h4>
<span class="bussadd"><?php isset($detail['detail']) ? isset($detail['detail']) : '' ;?><span>