Illegal String offset error in view codeigniter - php

When I try to load the data from database in views it is showing error :
A PHP ERROR WAS ENCOUNTERED
SEVERITY: WARNING
MESSAGE: ILLEGAL STRING OFFSET 'NAME'
FILENAME: CLIENT/LISTVIEW.PHP
LINE NUMBER: 7
when I var_dump the variable, its showing
STRING(1) "N"
in the result
This is in my controller file (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);
}
This is in my model file (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;
}
View File (listview.php)
<h4><?php echo var_dump($detail['name']);?></h4>
<span class="bussadd"><?php var_dump($detail['detail']);?><span>

The following code could work here. Try it to the view page.
<h4><?php echo isset($detail['name']);?></h4>
<span class="bussadd"><?php isset($detail['detail']);?><span>
or
<h4><?php echo isset($detail['name']) ? isset($detail['name']) : ''; ?></h4>
<span class="bussadd"><?php isset($detail['detail']) ? isset($detail['detail']) : '' ;?><span>

I am not sure this is your answer. I think you will get some idea with my answer.
Change your files to
in your controller 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');
}
In Model page 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;
}
Your view file
<h4><?php var_dump($detail['name']);?></h4>
<span class="bussadd"><?php var_dump($detail['detail']);?><span>

Related

Codeigniter - controller can't send data to view

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 } ?>

Unable to Update and delete in CodeIgniter?

I'm new in CodeIgniter, just watched the tutorial on youtube and followed the steps.
I can't edit or delete records of my database. I can insert or fetch records, but update and delete functions always return error. Here is my code
Controller:
function __construct()
{
parent::__construct();
$this->load->model('Danhmuc_model');
}
function xoadm()
{
$id = $this->uri->segment(5);
$this->Danhmuc_model->delete($id);
$this->session->set_flashdata('mess',' Đã xóa thành công');
redirect(admin_url('danhmuc/xemdm'));
}
function suadm()
{
$data = array();
$id = $this->uri->segment(5);
$row = $this->Danhmuc_model->get_info($id);
if($this->input->post())
{
$this->form_validation->set_rules('ten_dm',"Tên danh mục",'required');
if($this->form_validation->run())
{
$tendm = $this->input->post('ten_dm');
$input = array('ten_dm'=>$tendm);
$this->Danhmuc_model->update($id,$input);
$this->session->set_flashdata('mess',' Đã sửa thành công');
}
}
$data['row'] = $row;
$data['temp'] = 'admin/danhmuc/suadm';
$this->load->view('admin/index',$data);
}
Model:
function update($id, $data)
{
if (!$id)
{
return FALSE;
}
$where = array();
$where[$this->key] = $id;
$this->update_rule($where, $data);
return TRUE;
}
/**
* Xoa row tu id
* $id : gia tri cua khoa chinh
*/
function delete($id)
{
if (!$id)
{
return FALSE;
}
//neu la so
if(is_numeric($id))
{
$where = array($this->key => $id);
}else
{
//xoa nhieu row
//$id = 1,2,3...
$where = $this->key . " IN (".$id.") ";
}
$this->del_rule($where);
return TRUE;
}
View:
Edit view:
<td>
<input name="title" class="form-control" type="text" value="<?php echo $row['ten_dm'] ?>">
</td>
If I use this value="<?php echo $row['ten_dm'] ?>", the text box doesn't appear the category's title (I'm inserting and editing categories)
https://imgur.com/lW7FeDA
https://imgur.com/Z6TjnhL
But if I use this value="<?php echo $row->ten_dm ?>", error with this message
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: danhmuc/suadm.php
Line Number: 13
I can't understand where is the error, please help me. Thanks in advance.

Can not run a foreach loop

i have a trouble with my foreach loop, it always shows warning that is: Invalid argument supplied for foreach(), can you have a look and help me solve this problem.
This is my controller:
function prod_detail($id_sp){
$this->load->model('product_model');
$data['prod_detail'] = $this->product_model->getProdDetailByProdId($id_sp);
$data['prod_errors'] = $this->product_model->getProdDataError($id_sp);
$data['error_repairing'] = $this->product_model->getProdDataErrorRepairing($id_sp);
$data['rows']= $this->membership_model->getUserData();
$data['main_content'] = 'backend/home/manproduct/prod_detail_view';
$this->load->view('includes/admin/template', $data);
}
This is my model:
function getProdDataError($id_sp){
$this->db->where('id_sp', $id_sp);
$this->db->where('status', 0);
$query = $this->db->get('loi');
if($query->num_rows()>0){
foreach ($query->result() as $row){
$data[]=$row;
}
return $data;
}
}
function getProdDataErrorRepairing($id_sp){
$this->db->where('id_sp', $id_sp);
$this->db->where('status', 1);
$query = $this->db->get('loi');
if($query->num_rows()>0){
foreach ($query->result() as $row){
$data[]=$row;
}
return $data;
}
}
And here is my view:
if($ud->status==0){
echo 'Hoạt động';
}else if($ud->status==1){
echo '<b>Lỗi<br><ul></b>';
foreach ($prod_errors as $err) {
echo '<li>'.$err->ten_loi.'</li>';
}
echo '</ul>';
}else if($ud->status==2){
echo '<b>Đang sửa lỗi<br><ul></b>';
foreach ($error_repairing as $err) {
echo '<li>'.$err->ten_loi.'</li>';
}
echo '</ul>';
}else if($ud->status==3){
echo 'Chưa lắp đặt';
}
It's ok when $ud->status==1 but it shows a warning message when $ud->status==2. btw, i use codeigniter to develop my web, can you help?
try this (adding checks on $prod_errors/$error_repairing before trying to iterate them)
if($ud->status==0)
{
echo 'Hoạt động';
}else if($ud->status==1)
{
echo '<b>Lỗi<br><ul></b>';
// first check $prod_errors exists and is not null before iterating it
if (isset($prod_errors))
{
foreach ($prod_errors as $err)
{
echo '<li>'.$err->ten_loi.'</li>';
}
}
else{
echo "No Data found";
}
echo '</ul>';
}else if($ud->status==2)
{
echo '<b>Đang sửa lỗi<br><ul></b>';
// first check $error_repairing exists and is not null before iterating it
if (isset($error_repairing))
{
foreach ($error_repairing as $err)
{
echo '<li>'.$err->ten_loi.'</li>';
}
}
else{
echo "No Data found";
}
echo '</ul>';
}else if($ud->status==3)
{
echo 'Chưa lắp đặt';
}

read more function with codeigniter

i am trying to create a readmore function in codeigniter where the readmore link will be linked to a controller which would show all the data about that particular id....i am kind of confused on how to go about it... i tried...
<?php
$research_detail_url = site_url()."/research/research_details";
//echo json_encode($research)
if($research)
{
foreach ($research as $_research) {
$author = $_research->author;
$content = $_research->content;
$dsubmitted = $_research->dsubmitted;
echo "<div class='menu-collapse'> <h5>$author</h5>";
echo "<p>";
echo "<span class='support_text'>$content <span><br />";
echo "<span class='support_text'>$dsubmitted <span><br />";
echo "<a href='$research_detail_url' target='_blank' style='text-decoration:underline; color:#0088cc;'>
read more » </a>";
echo "</p> </div>";
}
}
?>
but i seem not be getting any results...i need help...
and this is my controller function.....
public function research_details($id='')
{
if(!$id)
{
echo "Project Id required";
return;
}
$_result = $this->projects_model->get_project($id);
if($_result)
{// success in fetching data hurray
$result['projects'] = $_result;
$users_ids = $this->users_model->get_user_ids(); //return available user id's
$groups_ids = $this->groups_model->get_group_ids(); //return available group id's
//echo json_encode($users_ids);
//echo json_encode($groups_ids);
$group_record = $this->map_names_to_ids($users_ids , $groups_ids );
$result['group_record'] = $group_record;
//load the view
$this->load->view('__includes__/header');
$this->load->view('__includes__/boostrap_responsive');
$this->load->view('projects/project_panel', $result);
$this->load->view('__includes__/footer_scripts');
$this->load->view('__includes__/wijmo_file_jquery');
$this->load->view('__includes__/footer');
}
else
{
exit("An Error occured in fetching the requested project");
}
}
and this is my model.....
<?php
class research_model extends CI_Model {
function add()
{
$this->db->insert('research',$_POST);
if($this->db->_error_number())
{
return $this->db->_error_number();
}
}
function update($article_id, $data_fields = NULL){
if($data_fields == NULL)
{
$this->db->where("article_id =".$article_id);
$this->db->update('research',$_POST);
}
else
{
$this->db->where("article_id =".$article_id);
$this->db->update('research',$data_fields);
}
$is_error = $this->db->_error_number();
if($is_error){
echo $is_error;
}
return TRUE;
}
function delete($id){
$this->db->where("article_id =".$id);
return $this->db->delete('research');
}
//return the research with this id
function get_research($id){
$this->db->where("article_id =".$id);
$query = $this->db->get('research');
if ($query->num_rows() > 0){
return $query->row_array();
}
else
echo $this->db->_error_message();
return FALSE;
}
//return the available research in the table
function get_research_all(){
$query = $this->db->get('research');
if ($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
$result[] = $row;
}
return $result;
}
}
}
and my entire controller.....
<?php
class Research extends Public_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('research_model');
}
function index()
{
if($this->ion_auth->is_admin())
{
$result = $this->research_model->get_research_all();
$data = array(
'main_content' => 'research/index',
'research' => $result
);
$this->load->view("loader", $data);
}
else
{
redirect('home');
}
}//END INDEX
// public view
function current()
{
$result = $this->research_model->get_research_all();
$data = array('research' => $result);
$this->load->view('__includes__/header');
$this->load->view('__includes__/navbar');
$this->load->view('research/current', $data);
$this->load->view('__includes__/footer');
}
function add()
{
if($this->ion_auth->is_admin())
{
$this->load->view("loader",array('main_content'=>"research/add_research"));
}
}//END ADD
function edit($id='')
{
if(! $id)
{
echo "research Id required";
return;
}
$result = $this->research_model->get_research($id);
if( ! $result)
{
echo "Nothing to edit";
return;
}
$result['main_content'] = "research/add_research";
$this->load->view("loader",$result);
}//END EDIT
function delete($id='')
{
if(! $id)
{
echo "Id required";
return;
}
$this->research_model->delete($id);
$this->get_research();
}//END DELETE
function submit($id='')
{
//validate form [perform validation server-side to make sure of fields]
$this->load->library('form_validation');
$this->form_validation->set_rules('author', 'Author', 'trim|required|min_length[4]');
if ($this->form_validation->run() == FALSE){
//ajax data array
$data = array(
'server_validation' => validation_errors()
);
echo str_replace('\\/', '/', json_encode($data));
}
else{
if($id){
$result = $this->research_model->update($id);
$content = "article has been UPDATED successfully";
//$retArr["content"] = $content;
//echo json_encode($retArr);
}
else{
$result = $this->research_model->add();
$content = "article has been CREATED successfully";
//$retArr["content"] = $content;
//echo json_encode($retArr);
}
//if duplicate key
if($result == 1062){
//ajax data array
$data = array();
$data['is_valid'] = 0;
echo json_encode($data);
}else{
//ajax data array
$data = array(
'is_valid' => 1,
'content' => $content
);
echo json_encode($data);
}
}//end ELSE form valid
}//END SUBMIT
public function research_details($id='')
{
if(!$id)
{
echo "Project Id required";
return;
}
$_result = $this->research_model->get_research($id);
if($_result)
{// success in fetching data hurray
$result['article'] = $_result;
//load the view
$this->load->view('__includes__/header');
$this->load->view('__includes__/boostrap_responsive');
$this->load->view('research/research_details', $Aresult);
$this->load->view('__includes__/footer_scripts');
$this->load->view('__includes__/wijmo_file_jquery');
$this->load->view('__includes__/footer');
}
else
{
exit("An Error occured in fetching the requested project");
}
}//END EDIT
}
?>
my public controller
<?php
abstract class Public_Controller extends CI_Controller
{
public $about_data;
function __construct()
{
parent::__construct();
//Making This variable availale for the whole site
$this->load->model('about_model');
$this->load->model('captcha_model');
//get your data
$this->about_data = $this->about_model->get_abouts();
}
}
?>
I see a couple of problems:
In your view, you are not closing the span tags. You need
</span>
In your view, you are creating the link, but you are not
including an ID, which you need in your controller. What I mean by this is the following:
First,you create this $research_detail_url = site_url()."/research/research_details";.
Then echo "<a href='$research_detail_url' target='_blank' style=''>read more</a>";
As you can see, your URL does not have an ID when created. What you should do is something like this: $research_detail_url = site_url()."/research/research_details/31"; where 31 is a random ID. You need to come up with the right ID needed in order to pass it to your controller. Your controller is currently assigning a blank ID since none is being passed which is why you end up with a result of "Project Id required"

Codeigniter view shows 0 record

i've insert 5 rows into ms_kategori_material table, and made some model here
function read()
{
$query = $this->db->get('ms_kategori_material');
if($query->num_rows()>0)
{
foreach ($query->result_array() as $value) {
echo $value['Kode_Kategori_Material_Jasa'];
echo $value['Nama_Material_Jasa'];
}
return $value;
}
else
{
return null;
}
}
and controllers
function index()
{
$data['kirim'] = $this->m_kategorimaterial->read();
echo $data; //returns KKMJ001batuKKMJ002batuKKMJ003batuKKMJ004batuKKMJ005batuArray
$this->load->view('v/vkategorimaterial',$data);
}
and the views
<?php
if ( !empty($kirim) ) {
$no = 1;
foreach ($kirim as $row) { ?>
<tr id="row">
<td id="no"><?php echo $no;?></td>
<td id="judul"><?php echo $row->Kode_Kategori_Material_Jasa;?></td>
<td id="kategori"><?php echo $row->Nama_Material_Jasa;?></td>
</tr> ?>
<?php
$no++;
}
} else { ?>
<tr id="row">
<td colspan="6" align="center">Data tidak ditemukan</td>
//the screen shows 'data tidak ditemukan' </tr>
<?php
}
?>
this is so confusing, since i can see the data in the controller, then pass them into the views, views said no data received
In your model
if($query->num_rows()>0)
{
foreach ($query->result_array() as $value) {
echo $value['Kode_Kategori_Material_Jasa'];
echo $value['Nama_Material_Jasa'];
}
return $value;
}
it's wrong, it should be
if($query->num_rows()>0)
{
return $query->result();
}
and in your controller you should have
function index()
{
$this->load->model('m_kategorimaterial'); // load it if it's not autoloaded
$data['kirim'] = $this->m_kategorimaterial->read();
$this->load->view('v/vkategorimaterial',$data);
}
Just reread your code. You're returning $value but never populating it. See below.
{
$query = $this->db->get('ms_kategori_material');
if($query->num_rows()>0)
{
$value = $query->result(); // or result_array() but you're using it as an object in the view not an array.
return $value;
}
else
{
return null;
}
}
You are actually not returning the results in your model,
replace
foreach ($query->result_array() as $value)
{
echo $value['Kode_Kategori_Material_Jasa'];
echo $value['Nama_Material_Jasa'];
}
with
if($query->num_rows()>0)
{
return $query->result();
}

Categories