Notice Message: Array to string conversion - php

I'm a starter in codeigniter and php, and im facing this problem
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: query
Filename: inc/header_main_view.php
Line Number: 175
A PHP Error was encountered
VIEW:
<ul class="dropdown-menu">
<li>
<!-- inner menu: contains the actual data -->
<ul class="menu">
<?php foreach($query as $row) :?>
<li><!-- Task item -->
<a href="#">
<h3>
<?php echo $row->id_task; ?><?php echo $row->name; ?>
<small class="pull-right">20%</small>
</h3>
<div class="progress xs">
<div class="progress-bar progress-bar-aqua" style="width: 20%" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100">
<span class="sr-only">20% Complete</span>
</div>
</div>
</a>
</li><!-- end task item -->
<?php endforeach; ?>
</ul>
</li>
<li class="footer">
View all tasks
</li>
</ul>
CONTROLLER
public function get_todo($id = null)
{
$this->_require_login();
$this->load->model('todo_model');
if ($id != null) {
$result = $this->todo_model->get([
'id' => $id,
'id_user' => $this->session->userdata('id_user')
]);
} else {
$result = $this->todo_model->get([
'id_user' => $this->session->userdata('id_user')
]);
}
return $result;
}
OTHER CONTROLLER
public function index()
{
require('api.php');
$api = new api();
$query = $api->get_todo();
$this->load->view('dashboard/inc/header_main_view', $query);
$this->load->view('dashboard/admin_pages/dashboard_view');
$this->load->view('dashboard/inc/footer_main_view');
}
MODEL
public function get($id = null, $order_by = null)
{
if (is_numeric($id)) {
$this->db->where($this->_primary_key, $id);
}
if (is_array($id)) {
foreach ($id as $_key => $_value) {
$this->db->where($_key, $_value);
}
}
$q = $this->db->get($this->_table);
return $q->result_array();
}
Can someone please tell me why I am getting the error.

It will be like this
$data['query'] = $api->get_todo();
$this->load->view('dashboard/inc/header_main_view', $data);

You did not passed variable to view right way.$query is not available at your view.CI converts array or object keys as variable which is sent from controller.See Adding Dynamic Data to the View From the doc.
You need to fix your controller like this
$data['query'] = $api->get_todo();
$this->load->view('dashboard/inc/header_main_view', $data);
//your view will get all key of $data as variable.
//your view will get $query from above code

Related

display announcements in user's dashboards per user type using codeigniter

I would like to display announcements in the different user's dashboards. What I am trying to do is to display it in the dashboard in which the user can close the announcement in the dashboard permanently, but what happens is that when a certain user close the announcement tab in the dashboard, all of the announcement in the same user type is being deleted. So can you please help me to figure out what approach should I use? Thanks!
VIEW
<div class="clearfix"></div>
<?php
if($announcements = $this->Bid_Model->fetchItems('announcements')):
foreach ($announcements as $announce):
if($announce->user_type == 6){?>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Announcement</h2>
<ul class="nav navbar-right panel_toolbox">
<li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
</li>
<li class="dropdown">
</li>
<li><i class="fa fa-close"></i>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="x_content">
<img src="<?php echo base_url().'uploads/';?>announcements/<?=$announce->image;?>" height="500" width="700">
<h4><?=$announce->title?></h4>
<h4><?=$announce->content?></h4>
<div class="ln_solid"></div>
<!-- modals -->
</div>
</div>
</div>
</div>
<?php } endforeach; endif; ?>
<!-- /modals -->
<br />
CONTROLLER
public function announceSubmit(){
$this->session->userdata('user_data');
$id = $this->session->userdata('user_id');
$title = $this->input->post('title', true);
$users = $this->input->post('users', true);
$content = $this->input->post('descr', true);
$config['upload_path'] = './uploads/announcements/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '25000';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('image-source')){
print_r($this->upload->display_errors());
}else{
$announce_image = $this->upload->data('file_name');
$data = array(
'title' => $title,
'user_type' => $users,
'content' => $content,
'image' => $announce_image,
'user_id' => $id
);
$this->Bid_Model->insertItem('announcements', $data);
redirect('dashboard');
}
}
public function deleteAnnouncement(){
$this->Bid_Model->updateItem('announcements',['user_type' => 0],['announcement_id'=>$this->uri->segment(3)]);
redirect('dashboard');
}
MODEL
function insertItem($table, $data = NULL){
$this->db->insert($table, $data);
}
function insertItems($table,$data = array()){
$this->db->insert_batch($table, $data);
}
function updateItem($table, $data, $var = NULL){
if($var != NULL){
$this->db->where($var);
}
$this->db->update($table, $data);
}
I assume that you are storing your user related data in session so
Controller :
public function deleteAnnouncement() {
$this->Bid_Model->updateItem('announcements', ['user_type' => 0], ['announcement_id' => $this->uri->segment(3), 'user_id' => $this->session->userdata('user_id')]);
redirect('dashboard');
}
First, You are passing 2nd and 3rd parameters to updateItem() in the model as an array.
So it is better to do like this $var = array() rather than $var = NULL
Second, you are deleting announcements for a particular user so use user id to delete for only one user
$id = $this->session->userdata('user_id')
function updateItem($table, $data, $var = array()){
$id = $this->session->userdata('user_id');
if(count($var) > 0){
$this->db->where($var);
}
$this->db->where('user_id', $id);
$this->db->update($table, $data);
}

Warning: Invalid argument supplied for foreach()

I have an error in the product detail, I do not know what else to change where again. when I change $detail into $categories succeed but is the same as function submenu. It was not according to my wishes
My View
<!-- navbar -->
<div class="row">
<?php foreach ($categories as $row) : ?>
<div class="col-sm-3">
<h5><?php echo $row->categoriesName; ?></h5>
<ul><li><a href="<?php echo base_url();?>member/detail">
<?php echo $row->productName; ?></a> </li></ul>
</div>
<?php endforeach; ?>
</div>
<!-- product detail -->
<?php foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; ?>
My Controller
public function home() {
$data=array('title'=>'Pasar Online | Ayo Belanja Sekarang',
'username'=> $this->session->userdata('username'),
'product' => $this->product_model->all(),
'categories' => $this->categories_model->get_main(),
'isi' =>'member/index');
$this->load->view('template_home/wrapper',$data);
}
public function detail() {
$data['username'] = $this->session->userdata('username');
$data['categories'] = $this->categories_model->get_main();
$data['detail'] = $this->categories_model->get_details();
$this->load->view('member/coba',$data);
}
My Model
public function get_main(){
$this->db->select('product.*,categories.*');
$this->db->from('product');
$this->db->join('categories','product.categoriesID=categories.categoriesID');
$this->db->limit(5);
$query = $this->db->get();
if($query->num_rows() > 0) {
$results = $query->result();
} return $results;
}
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
}
still can not display a single product
I do not know where my mistake... please help me, thanks
you are iterating details array in wrong format.
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
This will return only single row without any indexing like - 0,1,2...
and on view page you are using foreach loop and foreach loop use indexing to iterate value you can iterate like that --
<div class="box">
<h1 class="text-center"><?php echo $detail->productName; ?></h1>
<p class="price"><?php echo $detail->price; ?></p>
</div>
please use this way hope it will work..
In codeigniter row() selects only first row of your result in object format.So no need to use foreach loop.Just try like this...
<div class="box">
<h1 class="text-center"><?php echo $detail->productName;?></h1>
<p class="price"><?php echo $detail->price;?></p>
</div>
And
In model
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
it is because of $detail variable is empty, no data available in this variable, always check for data in variable before looping
<?php if(!empty($detail)){
foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; } ?>

error in my view file on calling a method from model

<div class="collapse navbar-collapse" id="navbar-collapse-1">
<ul class="nav navbar-nav">
<?php
$this->load->model('cms_model');
if (isset($article_data)) {
} else {
$article_data = $this->cms_model->get_end_user_cms_menu(); (this line gives error)
}
foreach ($article_data as $row) { ?>
<li class="dropdown"> <a href='<?php echo site_url('endusers/end_cms_controller/cms_type_selected?type_id=' . $row->type_id) ?>'>
<?php echo $row->type_name; ?>
</a> </li>
<?php } ?>
</ul>
<div class="nav-search-wrap">
<input class="txt-search" type="text" name="search" placeholder="Search">
</div>
</div><!-- /.navbar-collapse -->
This is the code from my view file it givs the error on line number i highlighted. The error is " Fatal error: Call to a member function get_end_user_cms_menu() on a non-object in E:\xampp\htdocs\Santulan\application\views\endusers\header.php on line 158"
About the other problem: The issue was inside your controller. I post the corrected version with explanatory comments:
public function index(){
$str=$this->input->post('search');
$search_id=$this->cms_model->search_tag_id($str);
//echo $id= $search_id; -> you cannot echo $search_id becouse it's an array: $query->result() from search_tag_id() model function.
//$query->result() is allways an array
//FOR DEBUGGING: when you want to see inside $query->result(), use: echo '<pre>'.print_r($query->result(), true).'</pre>';
// instead you use:
$id = $search_id->article_id;
$search_data['article']=$this->cms_model->search_article($id);// function for search article
if($search_data){
$this->load->view('endusers/cms_view', $search_data); // this is ok, if you call for $article and $article_image inside the view
}else{
echo "No result found";
}
}
First, you must call $this->load->model('cms_model'); in your controller, not in your view.
Then, in the controller also, not in your view, you could do:
if (!isset($article_data)) {
$article_data = $this->cms_model->get_end_user_cms_menu();
}
$data['list'] = '';
foreach ($article_data as $row){
$data['list'] .= "<li class=\"dropdown\"> $row->type_name </li>";
}
Then, also in your controller, when you load the view, add $data:
$this->load->view('your_viwe', $data);
And in your view:
<div class="collapse navbar-collapse" id="navbar-collapse-1">
<ul class="nav navbar-nav">
<?php echo $list;?>
</ul>
<div class="nav-search-wrap">
<input class="txt-search" type="text" name="search" placeholder="Search">
</div>
</div>
And your-re good to go.

Message: Undefined property: stdClass in codeigniter

I got this error:
A PHP Error was encountered Severity: Notice Message: Undefined property: stdClass::$positionsNum
when i run.
My model:
public function load_job($id){
$Where = array('jobs.id' => $id);
$this->db->select('jobs.id, positionsNum, companyName, contract_type.titleEN as contractType');
$this->db->from('jobs');
$this->db->join('contract_type', 'jobs.contractTypeId = contract_type.id', 'left');
$this->db->where($Where);
$Result = $this->db->get();
return $Result->first_row() ;
}
my view:
<?php if(isset($job) && count($job) > 0){ ?>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('contractType'); ?></div>
<div class="col-lg-6"><?php if(isset($job->contractType)) echo $job->contractType; ?></div>
</div>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('num_vacancies'); ?> </div>
<div class="col-lg-6"><?php echo $job->positionsNum; ?></div>
</div>
<?php } ?>
my Controller's function:
public function job() {
$job_id = $this->uri->segment(3);
if(isset($job_id) && !empty($job_id))
{
//one job
$job = $this->job_model->load_job($this->GetLang, $job_id);
$data['job'] = $job;
$this->load->view( 'front/singleJob', $data);
}
}
Also, i printed the returned object and it returns this:
stdClass Object(
[id] => 9
[positionsNum] => 2
[companyName] =>
[contractType] => type1
)
But, contractType won't be printed in page even if it is already has a value.
I can't figure out the problem. Any help?
The problem is here.
you have a function with one parameter.
public function load_job($id);
but in controller you are passing two values.
Try to pass correct values.
$job = $this->job_model->load_job($this->GetLang, $job_id);
it should be like this.
$job = $this->job_model->load_job( $job_id);
try like this
public function job() {
$job_id = $this->uri->segment(3);
if(isset($job_id) && !empty($job_id))
{
//one job
$data['job'] = $this->job_model->load_job($this->GetLang, $job_id);
$this->load->view( 'front/singleJob', $data);
}
}
So, it is solved now :)
This problem happened twice in two files:
First is which stated in my question, and I had an error in another query at the same page.
Second problem was in the HTML file. My complete view was like this:
<?php if(isset($service) && count($service) > 0){ ?>
<div class="row job-detail-row">
<div class="col-lg-6 job-detail-label"><?php echo lang('contractType'); ?></div>
<div class="col-lg-6"><?php if(isset($service->contractType)) echo $service->contractType; ?></div>
</div>
<?php } ?>
<ul>
<?php foreach ($similarServices as $service) { ?> <<========= NOTICE: $service here is the same as the above condition
<li>
<div class="title"><?php echo $service->title; ?>
</div>
</li>
<?php }//end foreach similarServices ?>
</ul>
It appeared because i used the same variable name $service, so i changed it and it works now. Hope this helps you.

error illegal string offset in codeigniter

i am getting error illegal sting offset in variable 'slug' and 'judul' in my view. can you tell me what the probelm.
controller
function index()
{
$slug = $this->uri->segment(2);
$this->data['halaman'] = $this->mhalaman->get_profil($slug);
if (empty($this->data['halaman'])) {
show_404();
}
$this->data['judul'] = $this->data['halaman']['judul'];
//var_dump($halaman_item['slug']);
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('page', $this->data, true);
$this->load->view('template/wrapper/mahasiswa/wrapper_content',$this->data);
}
view
<div class="section ui dropdown link item">
<span class="count">Profil</span>
<div class="menu">
<?php foreach ($halaman as $dt) : ?>
<div class="item">
<?php echo $dt['judul'] ?> //line error
</div>
<?php endforeach; ?>
</div>
</div>
modal
function get_profil($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get($this->tbl_halaman);
return $query->result_array();
}
$query = $this->db->get_where($this->tbl_halaman, array('slug'=>$slug));
return $query->row_array();
}
please help me what to do. thank you
It should be echo $dt instead of echo $dt['slug']

Categories