Trying to get property of non-object ( Codeigniter ) - php

i am getting an error "Message: Trying to get property of non-object"
when i try to display slider from database.
Message: Trying to get property of non-object
Filename: home/index.php
Line Number: 78
From admin side , admin enters slider name , image and Description and
it will display on user side at home page
At localhost (using wamp) it is working perfect but when i will upload online , it is giving error
Home.php ( Controller File )
if ( ! defined('BASEPATH'))
exit('No direct script access allowed');
class Home extends CI_Controller
{
public function index()
{
$this->load->view('home/index');
}
}
Index.php ( View File )
<div class="body_resize">
<div class="body">
<div class="border_box">
<div class="box_skitter box_skitter_large">
<?php
$info=$this->dbcommon->getInfo(array('homepage_image'),'homepage',array('status'=>0),'1','','sort_order');
foreach($info as $row)
{
print_r($row);
?>
<ul>
<li>
<img src="<?php echo homepage.'medium/'.$row->homepage_image ?>" class="cubeHide" alt="8_abs.jpg"
width="900" height="400" />
</li>
</ul>
<?php
}?>
</div>
</div>
<script src="js/jquery.skitter.js" type="text/javascript" defer="defer"></script>
<script type="text/javascript" defer="defer">
$(document).ready(function () {
var options = {};
if (document.location.search) {
var array = document.location.search.split('=');
var param = array[0].replace('?', '');
var value = array[1];
if (param == 'animation') {
options.animation = value;
}
else if (param == 'type_navigation') {
if (value == 'dots_preview') {
$('.border_box').css({ 'marginBottom': '40px' });
options['dots'] = true;
options['preview'] = false;
}
else {
options[value] = true;
if (value == 'dots') $('.border_box').css({ 'marginBottom': '40px' });
}
}
}
$('.box_skitter_large').skitter(options);
});
</script>
<div class="body_home">
<h4><strong>Welcome to Softloopers!</strong></h4>
<?php $info=$this->dbcommon->getInfo('','master','','','0,1','');
echo $info->homepage;
?><br />
</div>
getInfo function
function getInfo($field_name='',$table_name,$array='',$return="",$limit='',$orderby='')
{
if($field_name=='')
{
$sql='select * ';
}
else
{
$str='';
foreach($field_name as $row)
{
$str.=$row.',';
}
$str=rtrim($str,',');
$sql="select ".$str;
}
$sql.= ' from ' .$table_name .' where 2>0 ';
if($array!='')
{
foreach($array as $key=>$value)
{
$sql.="and $key='$value' and";
}
$sql=trim($sql,' and');
}
if($orderby!='')
{
$sql.=" order by $orderby";
}
if($limit!='')
{
$sql.=" limit $limit";
}
$query = $this->db->query($sql);
if($return=='')
{
return $query->row();
}
else
{
return $query->result();
}
}

If you want to pass data to view from controller, you can do like this
Home.php ( Controller File )
if ( ! defined('BASEPATH'))
exit('No direct script access allowed');
class Home extends CI_Controller
{
public function index()
{
$data['info']=$this->dbcommon->getInfo(array('homepage_image'),'homepage',array('status'=>0),'1','','sort_order');
$this->load->view('home/index');
}
}
Index.php ( View File )
<div class="box_skitter box_skitter_large">
<?php
foreach($info as $row)
{
print_r($row);
?>
<ul>
<li>
<img src="<?php echo homepage.'medium/'.$row->homepage_image ?>" class="cubeHide" alt="8_abs.jpg"
width="900" height="400" />
</li>
</ul>
<?php
}?>
</div>

Related

get result by category id in codeigniter

I need to get the the subcategories of its categories in codeigniter.
I am stuck at passing category id to the my model method
my code is below :
this is my view : index.php
<ul class="list-inline">
<?php
if($category->num_rows() > 0)
{
foreach ($category->result() as $row)
{
?>
<li><img class="img-responsive center-block" width="80px" src="<?php echo base_url(); ?>assets/admin/uploads/category/<?php echo $row->cat_home_image; ?>" alt="icon" title="icon" class="img-responsive" /><p style="font-size:16px;"><?php echo $row->category_description; ?> </p></li>
<?php
}
}
?>
</ul>
this is my controller : Main.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function index()
{
$this->load->model("category_model");
$data["category"] = $this->category_model->fetch_category();
$this->load->model("ads_model");
$data["ads"] = $this->ads_model->fetch_home_ads();
$this->load->view('index',$data);
//$this->load->view('category');
}
public function getsubcategory()
{
$this->load->model("category_model");
$data["subcategory"] = $this->category_model->fetch_subcategory();
$this->load->view('category',$data);
}
}
this is my Model : category_model.php
<?php
class Category_model extends CI_Model
{
function fetch_category()
{
$query = $this->db->query("SELECT * FROM category AS c INNER JOIN category_language AS cl ON c.id=cl.category_id where parent_id IS NULL && lang_id='1' && c.id!='4' ORDER BY category_description");
return $query;
}
function fetch_subcategory()
{
$subcat = $this->db->query("SELECT * FROM category AS c INNER JOIN category_language AS cl ON c.id=cl.category_id where parent_id='1' && lang_id='1' ORDER BY category_description");
return $subcat;
}
}
?>
How to pass the category id to model to get the appropriate subcatory of category_id
Hope this will help you :
Your view should be like this :
<ul class="list-inline">
<?php
if($category->num_rows() > 0)
{
foreach ($category->result() as $row)
{?>
<li>
<a href="<?=site_url('main/getsubcategory/'.$row->id);?>">
<img class="img-responsive center-block" width="80px" src="<?=site_url('assets/admin/uploads/category/'.$row->cat_home_image); ?>" alt="icon" title="icon" class="img-responsive" />
<p style="font-size:16px;"><?php echo $row->category_description; ?> </p>
</a>
</li>
<?php
}
}
?>
</ul>
Your controller's getsubcategory method should be like this :
public function getsubcategory($id)
{
if ($id)
{
$this->load->model("category_model");
$data["subcategory"] = $this->category_model->fetch_subcategory($id);
$this->load->view('category',$data);
}
}
Your model method fetch_subcategory should be like this :
Note : make sure all column name belongs to correct table alias
function fetch_subcategory($id)
{
$this->db->select('*');
$this->db->from('category c');
$this->db->join('category_language cl', 'cl.category_id = c.id');
/* Use $id here like this
$this->db->where('parent_id', $id);
*/
$this->db->where('c.parent_id', '1');
$this->db->where('cl.lang_id', '1');
$this->db->order_by('c.category_description', 'DESC');
$result = $this->db->get()->result();
return $result;
}

fetch data from the database using codeigniter

i have uploaded a image and related content for it in the database, now im trying to fetch it from their and display it on my webpage, but im getting some errors while fetching it
, so please can any one help me where im going wrong? i don know the concept correctly but just how much i know i have implemented it please help me
this is my Home.php(controller)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
//load database libray manually
$this->load->database();
//load Model
$this->load->model('Contact_model');
// load form and url helpers
$this->load->helper(array('form', 'url'));
// load form_validation library
$this->load->library('form_validation');
}
public function Latest_news()
{
$this->form_validation->set_rules('first_content', 'First content', 'required');
$this->form_validation->set_rules('second_content', 'Second content', 'required');
if ($this->form_validation->run()==TRUE)
{
$config['upload_path'] = FCPATH.'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( $this->upload->do_upload('filename') )
{
//print_r($this->upload->data());die;
$data['first_content'] = $this->input->post('first_content');
$data['second_content'] = $this->input->post('second_content');
$data['filename'] = $this->upload->data('file_name');
//Transfering data to Model
$this->Contact_model->latest_news($data);
//Redirecting to success page
redirect(site_url('Home/Latest_news'));
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
}
}
else
{
$this->load->view('Latest_news');
}
}
public function dispdata_latest_news()
{
$result['data']=$this->Contact_model->displayrecords_latest_news();
$this->load->view('display_records',$result);
}
?>
Contact_model(model)
<?php
class Contact_model extends CI_Model
{
function latest_news($data)
{
//saving records
$this->db->insert('latest_news', $data);
}
//Display
function displayrecords_latest_news()
{
//Displaying Records
$query=$this->db->query("select first_content from latest_news");
return $query->result();
}
}
?>
index.php(view)
<div class="lates">
<div class="container">
<div class="text-center">
<h2>Latest News</h2>
</div>
<div class="col-md-4 wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="300ms">
<img src="<?php echo base_url('images/4.jpg');?>" class="img-responsive" />
<table>
<tr>
<th>Content</th>
</tr>
<?php foreach($query as $r): ?>
<tr><?php echo $r->content; ?>
</tr>
<?php endforeach; ?>
</table>
</div>
Hope this will help you :
Your controller dispdata_latest_news should be like this :
public function dispdata_latest_news()
{
$rows = $this->Contact_model->displayrecords_latest_news();
$result = array('data' => $rows);
/* OR use it like this
$result = ['data' => $rows];
*/
$this->load->view('display_records',$result);
}
Your model displayrecords_latest_news should be like this :
function displayrecords_latest_news()
{
$query = $this->db->get("latest_news");
if ($query->num_rows() > 0)
{
return $query->result();
}
}
Your view should be like this :
<?php
if (! empty($data))
{
foreach($data as $r){ ?>
<tr><?php echo $r->first_content; ?></tr>
<?php }
}
else { ?>
<tr>no record found</tr>
<?php } ?>
I have check your all files and codes and i think you have wrong in your view file.
You have use $r->content. I think where you have get the data in model file the column name is different i.e. first_content.
you have to use like this.
<?php foreach($query as $r): ?>
<tr> <?php echo $r->first_content; ?>
</tr>
<?php endforeach; ?>

Link to a user profile page in codeigniter

I'm trying to make a function in the CodeIgniter framework so users can click on product detail page, and see which user uploaded that certain product and they must be able to click on the product owner to go to his profile page.
Now, I am already able to echo owner information of the product owner on the details page. But the link to the owner profile page is not working some reason and the name isn't echoed anymore since I tried to make it a link.
This is my product details page (details.php) foreach loop function where I'm trying to echo the username of the owner of a product and link it to their profile page:
<?php include_once ('templates/header.php'); ?>
<div class="container">
<h3>Email van de eigenaar:</h4>
<?php
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</div>
<div class="container">
<div class="row">
<div class="col-md-4" style="margin-top:24px;">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" class="img-responsive">
</div>
<div class="col-md-5">
<h1> <div class="product_naam"> <?php echo $product['product_naam']; ?> </div> </h1>
<h3>Over dit cadeau</h3>
<div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
</div>
<div class="col-md-3">
<button type="button" class="btn btn-default">Cadeau aanvragen</button>
<div class="aangeboden_door"> Aangeboden door: <?php
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?=$row['username']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="container">
<footer>
<p>© kadokado 2017, Inc.</p>
</footer>
<hr>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
When I load the view page I do not see the username and no link to the users profile.
Here is my controller (User.php):
<?php
class User extends CI_Controller
{
public function index()
{
$this->load->view('profile', $data);
}
public function __construct()
{
parent::__construct();
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
}
public function userdetails($user_id)
{
//load the User_model
$this->load->model('User_model');
//call function getdata in de Product_model
$data['userdata_list'] = $this->User_model->getdata();
//get product details
$data['user'] = $this->User_model->get_user_info($user_id);
//laad view
$data['main_content'] = 'profiel_user';
$this->load->view('profiel_user',$data);
}
public function profile()
{
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$this->load->view('profile');
}
}
full model file (User_model.php)
<?php
class User_model extends CI_Model {
public function getUserInfoByEmail($email)
{
$q = $this->db->get_where('users', array('email' => $email), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$email.')');
return false;
}
}
public function getUserInfo($user_id)
{
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$user_id.')');
return false;
}
}
public function getdata()
{
$this->db->where('user_id', $id);
$this->db->from('users');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
}
?>
Problably because of this line:
<?=$row['username']; ?>
Maybe your server does not support short hand tags?
try this:
<?php echo $row['username']; ?>
You also missed a semicolon here at the end:
echo base_url() . 'User/profiel_user?id='.$row['user_id']
and also here:
<a href="<?php echo base_url() ?>/Cadeauaanvragen">
You also use table row and table data tags without a surrounding table tag. Or remove all tr and td tags.
See: How do browsers analyze <tr> <td> without <table>?

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>

Not working check post request in controller ... Checked after must post data send to model

Checkbox is checked after value post request send to controller after get data by post request value from model
view (onlineorder/index.php)
<script src="<?php echo base_url('assets/js/ajax.jquery.min.js'); ?>"></script>
<script src="<?php echo base_url('assets/js/bootstrap.min.js'); ?>"></script>
<script src="<?php echo base_url('assets/fullcalendar/moment.min.js'); ?>"></script>
<script src="<?php echo base_url('assets/fullcalendar/jquery.min.js'); ?>"></script>
<?php echo form_open('onlineOrder/index'); ?>
<div class="row">
<div class="col-md-6">
<div class="form-horizontal well">
<fieldset>
<label>Захиалга өгөх тасаг сонгох</label>
<div class="tree-wrap">
<ul>
<?php
$counter = 0;
$getData = $getDataOrgBranch['listBranch'];
foreach ($getData as $value) {
echo '<li><input type="checkbox" value="' . $value['branch_id'] . '" class="event-branch"><span><i class="fa fa-folder-close-alt"></i>' . $value['branch_name'] . '</span></li>';
}
?>
</ul>
<input type="button" value="Get checkboxes" id="getCheckboxesButton">
<div id="debugOutput"></div>
</div>
</fieldset>
</div>
</div>
<div class="col-md-6">
<div class="form-horizontal well">
<fieldset>
<label>Захиалга өгөх үйлчилгээ сонгох</label>
<div class="tree-wrap">
<ul>
<?php
foreach ($getDataOrgService as $selectedObj) {
echo '<li class="collapsed"><input type="checkbox"><span><i class="fa fa-folder-close-alt"></i>' . $selectedObj["title"] . '</span>';
if (array_key_exists("children", $selectedObj)) {
$data = $selectedObj["children"];
echo "<ul>";
foreach ($data as $val) {
echo '<li class="leaf"><input type="checkbox"><span><i class="fa fa-folder-close-alt"></i>' . $val["title"] . '</span></li>';
}
echo "</ul>";
}
echo "</li>";
}
?>
</ul>
</div>
</fieldset>
</div>
</div>
</div>
</form>
<script>
$(document).ready(function () {
$('.event-branch').change(function () {
if ($(this).is(":checked")) {
var checkVal = $(this).val();
$.ajax({
url: "<?php echo base_url('onlineOrder/index'); ?>",
type: "POST",
data: {"branchId": checkVal},
dataType: "json",
success: function (getDataServiceOfBranch) {
alert("8888888888888888" + data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus, errorThrown);
}
});
}
console.log(checkVal);
});
});
</script>
Controller(OnlineOrder.php)
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class OnlineOrder extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('menusModel');
$this->load->model('getFromDataUrlModel');
}
public function index() {
if (isset($_POST['branchId'])){
$id = $this->input->post('branchId');
$data['getDataServiceOfBranch'] = $this->getFromDataUrlModel->getDataServiceOfBranch($id);
}
$head['page'] = 'onlineorder';
$head['menus'] = $this->menusModel->getAllData();
$head['getDataOrgAndEmployee'] = $this->getFromDataUrlModel->getDataOrgAndEmployee();
$data['getDataOrgService'] = $this->getFromDataUrlModel->getDataOrgService();
$data['getDataOrgBranch'] = $this->getFromDataUrlModel->getDataOrgBranch();
$this->load->view('header', $head);
$this->load->view('onlineorder/Index',$data);
$this->load->view('footer');
}
}
<p>Model(GetFromDataUrlModel.php)</p>
<!-- begin snippet: js hide: false -->
<?php
class GetFromDataUrlModel extends CI_Model{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->helper('url');
}
public function getDataServiceOfBranch($id='')
{
$url = 'http://192.168.1.22:9390/HomePage/TreatmentByBranch?branch_id='. $id;
$orgBranchService = json_decode(file_get_contents($url), true);
return $orgBranchService;
$file_headers = #get_headers($url);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
echo show_404();
}
else {
}
}
}
I couldn't really understand what's your issue but I think your code needs these modifications to work properly, update your Controller and Model with the following and see if that fixes it.
Controller index:
public function index() {
$id = $this->input->post('branchId');
if ($id) {
$data['getDataServiceOfBranch'] = $this->getFromDataUrlModel->getDataServiceOfBranch($id);
if (!$data['getDataServiceOfBranch'])
{
echo show_404();
}
}
$head['page'] = 'onlineorder';
$head['menus'] = $this->menusModel->getAllData();
$head['getDataOrgAndEmployee'] = $this->getFromDataUrlModel->getDataOrgAndEmployee();
$data['getDataOrgService'] = $this->getFromDataUrlModel->getDataOrgService();
$data['getDataOrgBranch'] = $this->getFromDataUrlModel->getDataOrgBranch();
$this->load->view('header', $head);
$this->load->view('onlineorder/Index', $data);
$this->load->view('footer');
}
Model method:
public function getDataServiceOfBranch($id)
{
$url = 'http://192.168.1.22:9390/HomePage/TreatmentByBranch?branch_id='.$id;
$orgBranchService = json_decode(file_get_contents($url), true);
$file_headers = #get_headers($url);
if ($file_headers[0] == 'HTTP/1.1 404 Not Found') {
return false;
}
else {
return $orgBranchService;
}
}
Hope it helps.

Categories