首页 > 解决方案 > 消息:未定义的属性:stdClass::$designation_id

问题描述

遇到 PHP 错误 严重性:通知

消息:未定义的属性:stdClass::$designation_id

文件名:仪表板/index.php

行号:548

回溯:

文件:/home/newtalent/public_html/demo/localhost/application/views/dashboard/index.php 行:548 功能:_error_handler

文件:/home/newtalent/public_html/demo/localhost/application/third_party/MX/Loader.php 行:362 功能:include

文件:/home/newtalent/public_html/demo/localhost/application/third_party/MX/Loader.php 行:304 功能:_ci_load

文件:/home/newtalent/public_html/demo/localhost/application/controllers/Dashboard.php 行:86 功能:查看

文件:/home/newtalent/public_html/demo/localhost/index.php 行:292 功能:require_once

我的控制器

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Announcement extends MY_Controller {

     public function __construct() {
        Parent::__construct();
        $this->load->library('session');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->database();
        $this->load->library('form_validation');
        //load the model
        $this->load->model("Announcement_model");
        $this->load->model("Xin_model");
        $this->load->model("Designation_model");
    }

    /*Function to set JSON output*/
    public function output($Return=array()){
        /*Set response header*/
        header("Access-Control-Allow-Origin: *");
        header("Content-Type: application/json; charset=UTF-8");
        /*Final JSON response*/
        exit(json_encode($Return));
    }

     public function index()
     {
        $data['title'] = $this->Xin_model->site_title();
        $data['all_designations'] = $this->Designation_model->all_designations();
        $session = $this->session->userdata('username');
        $data['breadcrumbs'] = 'Announcements';
        $data['path_url'] = 'user/user_announcement';
        if(!empty($session)){
            $data['subview'] = $this->load->view("user/announcement_list", $data, TRUE);
            $this->load->view('layout_main', $data); //page load
        } else {
            redirect('');
        }

     }

    public function announcement_list()
     {

        $data['title'] = $this->Xin_model->site_title();
        $session = $this->session->userdata('username');
        if(!empty($session)){ 
            $this->load->view("user/announcement_list", $data);
        } else {
            redirect('');
        }
        // Datatables Variables
        $draw = intval($this->input->get("draw"));
        $start = intval($this->input->get("start"));
        $length = intval($this->input->get("length"));


        $announcement = $this->Announcement_model->get_announcements();

        $data = array();

        foreach($announcement->result() as $r) {

        // get user > added by
        $user = $this->Xin_model->read_user_info($r->published_by);
        // user full name
        $full_name = $user[0]->first_name.' '.$user[0]->last_name;
        // get date
        $sdate = $this->Xin_model->set_date_format($r->start_date);
        $edate = $this->Xin_model->set_date_format($r->end_date);

        if($r->designation_id == '') {
            $ol = 'All Designations';
        } else {
            $ol = '<ol class="nl">';
            foreach(explode(',',$r->designation_id) as $desig_id) {
                $_des_name = $this->Designation_model->read_designation_information($desig_id);
                $ol .= '<li>'.$_des_name[0]->designation_name.'</li>';
             }
             $ol .= '</ol>';
        }

        $data[] = array('<span data-toggle="tooltip" data-placement="top" title="View"><button type="button" class="btn btn-secondary btn-sm m-b-0-0 waves-effect waves-light" data-toggle="modal" data-target=".view-modal-data" data-announcement_id="'. $r->announcement_id . '"><i class="fa fa-eye"></i></button></span>',
            $r->title,
            $r->summary,
            $ol,
            $sdate,
            $edate,
            $full_name
        );
      }

      $output = array(
           "draw" => $draw,
             "recordsTotal" => $announcement->num_rows(),
             "recordsFiltered" => $announcement->num_rows(),
             "data" => $data
        );
      echo json_encode($output);
      exit();
     }
}

模型

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class announcement_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function get_announcements()
    {
      return $this->db->get("xin_announcements");
    }

     public function read_announcement_information($id) {

        $condition = "announcement_id =" . "'" . $id . "'";
        $this->db->select('*');
        $this->db->from('xin_announcements');
        $this->db->where($condition);
        $this->db->limit(1);
        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            return $query->result();
        } else {
            return false;
        }
    }


    // Function to add record in table
    public function add($data){
        $this->db->insert('xin_announcements', $data);
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    // Function to Delete selected record from table
    public function delete_record($id){
        $this->db->where('announcement_id', $id);
        $this->db->delete('xin_announcements');

    }

    // Function to update record in table
    public function update_record($data, $id){
        $this->db->where('announcement_id', $id);
        if( $this->db->update('xin_announcements',$data)) {
            return true;
        } else {
            return false;
        }       
    }
}
?>

看法

<?php 
    $session = $this->session->userdata('username');?> 
<div class="box box-block bg-white">
    <h2><strong>List All</strong> Announcements </h2>
    <div class="table-responsive" data-pattern="priority-columns">
        <table class="table table-striped table-bordered dataTable" id="xin_table">
            <thead>
                <tr>
                    <th>Action</th>
                    <th>Title</th>
                    <th>Summary</th>
                    <th>Published For</th>
                    <th>Start Date</th>
                    <th>End Date</th>
                    <th>Published By</th>
                </tr>
            </thead>
        </table>
    </div>
</div>

如果有人有解决方案,请提前提供感谢

标签: phpcodeignitermodelcontrollerautoload

解决方案


此错误表示 stdClass 中没有属性。例如:

<?php
$exampleObj = new stdClass();
$value = $exampleObj->designation_id;  // Undefined property: stdClass::$designation_id

为避免错误,您需要创建此属性:

<?php
$exampleObj = new stdClass();
$exampleObj->designation_id = 1;
$value = $exampleObj->designation_id;

推荐阅读