There is an error message when my view is being loaded. "Call to a member function result() on a non-object"
How do I fix this?
This is the loop in my View: discussion/view.php
<?php foreach ($query->result() as $result) : ?>
<tr>
<td>
<?php echo anchor('comments/index/'.$result->ds_id,$result->ds_title) . ' '
. $this->lang->line('comments_created_by') . $result->usr_name; ?>
<?php echo anchor('discussions/flag/'.$result->ds_id,
$this->lang->line('discussion_flag')) ; ?>
<br />
<?php echo $result->ds_body ; ?>
</td>
</tr>
<?php endforeach ; ?>
and this is the function index in my Comments Controller:
public function index() {
if ($this->input->post()) {
$ds_id = $this->input->post('ds_id');
} else {
$ds_id = $this->uri->segment(3);
}
$page_data['discussion_query'] = $this->Discussions_model->fetch_discussion($ds_id);
$page_data['comment_query'] = $this->Comments_model->fetch_comments($ds_id);
$page_data['ds_id'] = $ds_id;
$this->form_validation->set_rules('ds_id', $this->lang->line('comments_comment_hidden_id'), 'required|min_length[1]|max_length[11]');
$this->form_validation->set_rules('comment_name', $this->lang->line('comments_comment_name'), 'required|min_length[1]|max_length[25]');
$this->form_validation->set_rules('comment_email', $this->lang->line('comments_comment_email'), 'required|min_length[1]|max_length[255]');
$this->form_validation->set_rules('comment_body', $this->lang->line('comments_comment_body'), 'required|min_length[1]|max_length[5000]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('comments/view', $page_data);
$this->load->view('common/footer');
} else {
$data = array('cm_body' => $this->input->post('comment_body'),
'usr_email' => $this->input->post('comment_email'),
'usr_name' => $this->input->post('comment_name'),
'ds_id' => $this->input->post('ds_id')
);
if ($this->Comments_model->new_comment($data)) {
redirect('comments/index/'.$ds_id);
} else {
// error
$this->data['message'] = 'Error!';
}
}
}
Related
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$designation_id
Filename: dashboard/index.php
Line Number: 548
Backtrace:
File: /home/newtalent/public_html/demo/localhost/application/views/dashboard/index.php
Line: 548
Function: _error_handler
File: /home/newtalent/public_html/demo/localhost/application/third_party/MX/Loader.php
Line: 362
Function: include
File: /home/newtalent/public_html/demo/localhost/application/third_party/MX/Loader.php
Line: 304
Function: _ci_load
File: /home/newtalent/public_html/demo/localhost/application/controllers/Dashboard.php
Line: 86
Function: view
File: /home/newtalent/public_html/demo/localhost/index.php
Line: 292
Function: require_once
my controller
<?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();
}
}
model
<?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;
}
}
}
?>
View
<?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>
if anyone have solution please provide
thanks in advance
This error indicates that there is no property in stdClass.
For example:
<?php
$exampleObj = new stdClass();
$value = $exampleObj->designation_id; // Undefined property: stdClass::$designation_id
To avoid errors, you need to create this property:
<?php
$exampleObj = new stdClass();
$exampleObj->designation_id = 1;
$value = $exampleObj->designation_id;
I got an error like this :
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: tasks
controller
public function show() {
$data['tasks'] = array();
$data['tasks'] = $this->Tasks_model->show_task()->result();
$this->load->view('pages/all', $data);
}
view
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) ) {
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>
Model
public function show_task() {
return $this->db->get('task');
}
what's wrong with my code?
Controller
public function show() {
$this->$data['tasks'] = array();
$this->$data['tasks'] = $this->Tasks_model->show_task();
$this->load->view('pages/all', $data);
}
View
//Here, you can also use for loop to display data.
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) )
{
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>
EDIT =>
Model
public function show_task() {
$query = $this->db->get('task');
if( $query->num_rows() > 0 ) //Always check you are getting result or not.
{
return $query->result_array(); //result_array() returns the query result as a pure array
}
else
{
return array();
}
}
Here you are returning return $this->db->get('task'); directly. So you are not getting anythingin View.
First you have to assign $this->db->get('task') to variable e.g. $query and pass result array to controller return $query->result_array();.
Reference => https://www.codeigniter.com/userguide3/database/results.html#result-arrays
Controller:
public function show() {
$data['tasks'] = array();
$data['tasks'] = $this->Tasks_model->show_task();
$this->load->view('pages/all', $data);
}
Model:
public function show_task() {
return $this->db->get('task')->result();
}
View Page:
<?php
if( isset($tasks) && ( is_array($tasks) && count($tasks)>0 ) )
{
//echo"<pre>"; print_r($tasks); die();
for($i=0;$i<count($tasks);$i++)
{ ?>
<span><?php echo $task[0]['job']; ?></span><br />
<?php } ?>
<?php }
else { ?>
<span>No tasks records found.</span>
<?php } ?>
my problem is a want to insert multiple check box on my web application.
error Message
Array to string conversion
please help....
Controller :
public function add_overview_product($type)
{
if ($this->input->post('submit')) {
$type = $type;
$id_overview = $this->input->post('id_overview');
$records = array();
for ($i=0; $i < count($id_overview) ; $i++) {
$records[] = array(
'type' => $type,
'id_overview' => $id_overview[$i]
);
}
$check_idoverview = $this->Biostar->check_idoverview($type,$id_overview);
if ($check_idoverview > 0) {
$message = 'Sorry, The Product can not input twice with the same "TYPE" ';
} else {
foreach ($records as $key => $value) {
$datafield = array(
'type' => $type,
'id_overview' => $id_overview
);
$this->Biostar->saveoverviewproduct($datafield);
}
$data['type'] = $type;
$data['content'] = 'biostar/add_specification';
$this->load->view('dashboard/index', $data);
}
}
$data['diy'] = $diy;
$data['content'] = 'biostar/add_overview_product';
$this->load->view('dashboard/index', $data);
}
My : Model
public function saveoverviewproduct($datafield){
$sql = $this->db->insert('overview_biostar',$datafield);
return $sql;
}
public function check_idoverview($type,$id_overview){
$this->db->select('type');
$this->db->where('type',$type);
$this->db->where('id_overview',$id_overview);
$query = $this->db->get('overview_biostar')->rows();
return $query;
}
View:
<form method="post"action="<?php echo base_url(); ?>biostar/add_overview_product/<?php echo $type; ?>" >
<div class="box-body">
<?php foreach ($audio as $row){ ?>
<div class="checkbox">
<label>
<input type="checkbox" name="id_overview[]" value="<?php echo $row['title']; ?>"><?php echo $row['title']; ?>
</label>
</div>
<?php } ?>
</div>
$check_idoverview = $this->Biostar->check_idoverview($type,$id_overview);
$id_overview is an array. It should be a string.
How do I call the static method execute and use the $result array ?
i have this code:
<?php
$input_ = [
'object_id' => $id,
];
$results_wp_temp = ArticleLoadTagsData::execute($input_);
if (
is_array($results_wp_temp) &&
!empty($results_wp_temp)
) {
echo 'tags';
foreach ($results_wp_temp as $result_wp_temp) {
?>
<span><?php echo $result_wp_temp->name; ?></span>
<?php
}
} else {
echo 'no tags';
}
?>
and this class:
<?php
class ArticleLoadTagsData {
public static function execute($input = []) {
$result = [0=>1];
return $result;
}
}
I'm trying to build a easy booking system in codeIgniter and using a database with a table called conference_rooms. I'm calling for this in my Booking_Model.phplike this:
public function __construct()
{
$this->load->database();
}
public function get_room() {
$query = $this->db->get('conference_rooms');
return $query->result_array();
}
}
To display it I'm using my Booking.php class looking like this:
public function view()
{
$data['conference_rooms'] = $this->booking_model->get_room();
if (empty($data['conference_rooms'])) {
show_404();
}
$data['title'] = $data['conference_rooms']['title'];
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
And my view.php:
<h3><?php echo $conference_rooms['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms['seats']; ?>
</div>
It won't find $room. What am I doing wrong?
UPDATE:
Basically changed the whole code, my view class now looks like this (changed to index in my Booking controller)
public function index() {
$this->load->helper('form');
$this->load->view('bootstrap/header');
$this->load->model('Booking_Model');
$rooms = $this->Booking_Model->get();
$rooms_form_options = array();
foreach ($rooms as $id => $room) {
$rooms_form_options[$id] = $room->title;
}
$this->load->model('Package_Model');
$packages = $this->Package_Model->get();
$packages_form_options = array();
foreach ($packages as $id => $package) {
$packages_form_options[$id] = $package->package_name;
}
$this->load->view('booking', array(
'rooms_form_options' => $rooms_form_options,
'packages_form_options' => $packages_form_options,
));
$this->load->view('bootstrap/footer');
}
And my booking.php;
<div>
<?php echo form_label('Conference Room', 'id') ; ?>
<?php echo form_dropdown('id', $rooms_form_options, set_value('id')); ?>
</div>
<div>
<?php echo form_label('Package type', 'package_id') ; ?>
<?php echo form_dropdown('package_id', $packages_form_options, set_value('package_id')); ?>
</div>
<div>
<?php echo form_label('Antal deltagare', 'number_people') ; ?>
<?php echo form_input('number_people', set_value('number_people')) ; ?>
</div>
<div>
<?php echo form_submit('preview', 'Book'); ?>
</div>
Use this
In Model
<?
function __construct()
{
$this->load->database();
}
function get_room() {
$query = $this->db->get('conference_rooms');
$result = $query->result_array();
return $result;
}
?>
In controller
function view()
{
$data['conference_rooms'] = $this->booking_model->get_room();
if (empty($data['conference_rooms']))
{
show_404();
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
}
?>
in view
<h3><?php echo $conference_rooms[0]['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms[0]['seats']; ?>
</div>
cz of $conference_rooms[0] pointing 0 is we passing data with Objective array. so we need to point data. check with print_r
MODEL
You need to row_array(); to return an array
public function get_room() {
$query = $this->db->get('conference_rooms');
$rowcount = $query->num_rows();
if( $rowcount > 0 ){
return $row = $query->row_array();
} else {
return FALSE;
}
}
CONTROLLER
public function view()
{
$conference_rooms = $this->booking_model->get_room();// asing your array to variable
if (empty($conference_rooms)) {
show_404();
} else {
$data['conference_rooms']=$conference_rooms;// pass your variable to data array to pass into view
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
}
VIEW
<h3><?php echo $conference_rooms['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms['seats']; ?>
</div>