error illegal string offset in codeigniter - php

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']

Related

Retrieve data from two tables in PHP Codeigniter

I want to retrive count of datas from one table and some datas from another table , also i want to know how to display these in a single foreach loop.
Model
public function counter(){
$this->db->where('package_id', $id);
$this->db->from('gt_package');
$cnt = $this->db->count_all_results();
}
Controller
public function package_category_listing()
{
$data['records'] = $this->gt_package_model->counter();
$query = $this->db->get("gt_package_category");
$data['records'] = $query->result();
$this->load->view('admin/package/package_category_listing',$data );
}
View part :
<?php foreach ($records as $r) { ?>
<div class="col-sm-6 col-md-3 col-xs-12">
<div class="bg-purple">
<h4 class="text-white">
<?php echo $r->category_name; ?>
</h4>
<span>200</span> Packages
<br>
<a style="margin-top: 10px;" href="<?php echo site_url() . "/gt_package/listing/" . $r->category_id ?>" class="btn btn-default">View Packages</a>
</div>
</div>
<?php } ?>
As per your question to pass the data model to controller code bellow
Model:
public function counter()
{
$this->db->where('package_id', $id);
$this->db->from('gt_package');
$query = $this->db->get('gt_package');
$cnt = $query->num_rows();
return $cnt;
}
Controller:
public function package_category_listing()
{
$data['records'] = $this->gt_package_model->counter();
$query = $this->db->get("gt_package_category");
$data['records'] = $query->result();
$this->load->view('admin/package/package_category_listing',$data );
if($res){
$data['result'] = $res;
$this->load->view('manage_accounts_view', $data);
} else {
echo "Fail";
}
print_r($data['result']);
}

Displaying the blog pagetitle as a url in codeigniter php

I am having a blog page where the blogs are inserted from admin panel and blog title will be inserted into new column as separated by " - " if there are spaces between the page titles.For example if the page title is " welcome to something " then in the database it is will be inserted into two columns. In once column it will be inserted as same and in other column it will be inserted as welcome-to-something.
when clicking on readmore button i need to display in url as (www.example.com/blob/article/welcome-to-something) in this format i need to display the url.
Here is the code:
Controller:
public function index()
{
$this->load->model('blogs_model');
$data["records2"] = $this->blogs_model->get_all_blogs($config["per_page"], $page);
$data['mainpage'] = "blog";
$this->load->view('templates/template',$data);
}
public function article()
{
$this->load->model('blogs_model');
$data['records2']= $this->blogs_model->getblogsdata($this->uri->segment(3));
$data['mainpage']='blogs';
$this->load->view('templates/templatess',$data);
}
Model:
function get_all_blogs()
{
$this->db->select('B.*');
$this->db->from('blogs AS B');
$this->db->where(array('B.status'=>1));
$this->db->order_by("position", "asc");
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
function getblogsdata($id)
{
$this->db->select('blogs.*');
$this->db->from('blogs');
$this->db->where(array('blogs.blog_id'=>$id));
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<div class="col-md-9 blogs">
<?php if(isset($records2) && is_array($records2)):?>
<?php foreach ($records2 as $r):?>
<div class="blog1">
<img src="<?php echo base_url();?>admin/images/blogimages/thumbs/<?php echo $r->image_path;?>" class="testimonials1"/>
<h3 class="heading1"><?php echo $r->blog_title;?></h3>
<div class="blogtext1 read">
<?php echo $r->description;?>
</div>
<a href="<?php echo base_url()?>blog/article/<?php echo $r ->blog_id ;?>" class="read_more7" target="_blank" >Read More</a>
</div>
<?php endforeach ;endif;?>
<div class="pagination"><?php echo $links; ?></div>
</div>
blogs table
blog_id | blog_title | blogtitle
1 welcome to something welcome-to-something
Model:
function getblogsdata($id,$slug)
{
$this->db->select('blogs.*');
$this->db->from('blogs');
$this->db->where(array('blogs.blogtitle'=>$id));
$this->db->where(array('blogs.blogtitle' => $slug));
$this->db->order_by("ne_views", "asc");
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<a href="<?php echo base_url()?>blog/article/<?php echo $r->blogtitle;?>" class="read_more7" target="_blank" >Read More</a>

Notice Message: Array to string conversion

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

my query in codeigniter cannot produce result

i've been debbuging it many times based on my knowledge, i am new to codeigniter, any solution idea would be much appreciated... ty
here's my code
MODEL:
function userdisplay_info($id){
$result = $this->db->query("SELECT uid FROM user WHERE userIdentificationNumber LIKE '$id'");
//if($result->num_rows() > 0){
$info_data = $result->result();
$row = $info_data[0];
$result->free_result();
}
CONTROLLER:
function r_book(){
$data = array();
$bid = $this->input->post('bid');
$u_id = $this->input->post('userinputid');
$data = array(
'book_reserved' => 1
);
if($this->module2->reserved_book($bid,$data))
{
$data['info'] = $this->module2->userdisplay_info($u_id);
$this->load->view('User/template/header');
$this->load->view('User/template/navigator');
$this->load->view('User/landingpage',$data);
$this->load->view('User/template/footer');
return true;
}else
{
$this->load->view('User/template/header');
$this->load->view('User/template/navigator');
$this->load->view('User/landingpagefail');
$this->load->view('User/template/footer');
return false;
}
}
VIEW:
<div class="col-md-3">
<div class="panel panel-default">
<div class="panel-body">
<?php if($info) : foreach($info as $u_info) : ?>
<ul class="list-unstyled">
<li>Last Name : <?php echo $u_info->uid; ?></li>
<li>First Name : <?php echo $u_info->userFirstname; ?></li></li>
<li>Middle Name : <?php echo $u_info->userMiddlename; ?></li></li>
<li>Course : <?php echo $u_info->userCourse; ?></li></li>
</ul>
<?php endforeach; ?>
<?php else :?>
<h4> No Record Found</h4>
<?php endif; ?>
</div>
</div>
</div>
Rewrite your model function as
function userdisplay_info($id){
$this->db->select('uid');
$this->db->like('userIdentificationNumber', $id);
$query = $this->db->get('user');
return $query->result_array();
}

Error message not displaying in CodeIgniter

I want to edit some data after a POST in CodeIgniter.
The problem is when I make an edit I want to show a message that tells me the edit is done. I tried to do the following, but no message is displayed. Why?
Controller
function edit($id = 0) {
$this->load->model('Event_Model');
if ($id > 0) {
$this->data['eventinfo'] = $this->Event_Model->getInfo($id);
}
$this->data['pagetitle'] = "Edit";
$this->data['formID'] = $this->uri->segment(2)."-".$this->uri->segment(3);
$this->template->build('admin/events/add',$this->data);
}
function do_edit() {
$this->load->model('Event_Model');
$id = $this->input->post('hID');
$data = array(
'ev_text' => $this->input->post('ev_text'),
'ev_pic' => $this->input->post('ev_pic'),
);
$this->Event_Model->update($id, $data);
$this->data['success'] = "Done";
$this->data['errors'] = $this->errors;
$this->template->build('admin/events/add',$this->data);
}
Model
function update($id, $data) {
$this->db->where('ev_id', $id);
$this->db->update('events', $data);
}
View (where I want the message)
<?php
if (isset($success)) { ?>
<div class="alert alert-success normal-alert" style="display: block;">
<p><span class="ico-text ico-alert-success" ></span><?= $success; ?></p>
</div>
<?php
}
if (isset($errors)) { ?>
<div class="alert alert-error normal-alert" style="display: block;">
<div>
<span class="ico-text ico-alert-error"></span>
<?php if (count($errors) > 0) { ?>
<ul>
<?php
foreach ($errors as $error) {
echo "<li>$error</li>";
}
?>
</ul>
<?php } ?>
<div class="clear"></div>
</div>
</div>
<?php } ?>
and the second error, update data with picture not work, its get me error in dubg that call me the $_FILES['choose-file']['name'] not define
Try This:
Controller
function do_edit() {
$this->load->model('Event_Model');
$id = $this->input->post('hID');
$data = array(
'ev_text' => $this->input->post('ev_text'),
'ev_pic' => $this->input->post('ev_pic'),
);
$result = $this->Event_Model->update($id, $data);
if($result) {
$this->data['success'] = "Done";
$this->template->build('admin/events/add',$this->data);
}
$this->data['errors'] = $this->errors;
$this->template->build('admin/events/add',$this->data);
}
Model:
function update($id, $data) {
$this->db->where('ev_id', $id);
$this->db->update('events', $data);
return $this->db->affected_rows();
}

Categories