Calling Curl(ed) json content from Code Igniter - php

This is my function in model
<?php
class Clothes_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_clothes()
{
$url="http://192.168.0.105/ci/json_source/clothes.php";//clothes.php contain json data
$data= this->curl->simple_get($url);
$result = json_decode($data,true);
return $result;
}
}
This is my controller
<?php
class Clothes extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('clothes_model');
}
public function view($result)
{
$data['clothes_item'] = $this->clothes_model->get_clothes();
if (empty($data['clothes_item']))
{
show_404();
}
$data['title'] = $data['clothes_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('clothes/view', $data);
$this->load->view('templates/footer');
}
}
When I tried to run I got this error
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in
/var/www/ci/application/models/clothes_model.php on line 12
Is there anything wrong?

try changing:
this->curl->simple_get($url);
to
$this->curl->simple_get($url);
And make sure your $this->curl is valid

I did like this and works for me
$string=file_get_contents("proj-data/food_query.php"); //place where json file is
$decoded=json_decode($string,TRUE);

Related

Codeigniter - count all on the table not working

could ya'll help me on this one please. I'm trying to count how many are there in my table as it doesn't show up the result on my view.
Controller
public function enrollment_summary()
{
$data['title'] = 'Enrollment Summary';
$this->load->model('report_model');
$data['query'] = $this->report_model->get_students();
$this->load->view('report_enrollment_summary', $data);
}
Model
<?php
class report_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_students()
{
$query = $this->db->count_all('students');
}
}
View
<body>
<h1>Enrollment Summary</h1>
<?php echo $query; ?>
</body>
Your get_students() is not returning anything?
So you need to add in a return.
Your current method...
public function get_students()
{
$query = $this->db->count_all('students');
}
becomes
public function get_students()
{
$query = $this->db->count_all('students');
return $query;
}
OR
public function get_students()
{
return $this->db->count_all('students');
}

Call to undefined method, Database error

Can anybody tell me what's wrong with my code? It said that the problem is in the controller, on line 13. Please help
Controller
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('model_databis'); //line13
}
function index(){
$data=array('data' => $this->model_databis->bus());
$this->load->view('admin/databis',$data);
}
Model
class Model_databis extends CI_Model{
function __construct(){
parent::__construct();
}
function dbis(){
$query = $this->db->get('bus');
return $query->result_array();
}
function tambah($data){
$this->db->insert('bus', $data);
return TRUE;
}
}
You can try this solution for your problem :
Please change your function name in controller.
function index(){
$data=array('data' => $this->model_databis->dbis()); // bus into dbis
$this->load->view('admin/databis',$data);
}

Call to a member function error - Codeigniter

Everything is working as expected on my local server, but when I try to run it on my live server I get an error:
Fatal error: Call to a member function call_admin_login() on null in codeigniter HMVC
What am I possibly doing wrong?
Below is my code:
<?php
class Admin_login extends MY_Controller {
public function __construct() {
parent::__construct();
//Check Login Status
$logged_info = $this->session->userdata('logged_info');
if ($logged_info != FALSE) {
redirect(base_url('admin/view_products.html', 'refresh'));
}
//Load Model
$this->load->model('admin_login_model', 'login_mdl');
}
public function index() {
$data['title'] = 'Admin Login';
$this->template->call_admin_login($data);
}
<?php
class Template extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function call_admin_master($data = NULL) {
$this->load->view('admin_master_v', $data);
}
public function call_admin_login($data = NULL) {
$this->load->view('admin_login_v', $data);
}
}

Using active records to display data in codeigniter

so I believe I have it right (im new to active records).
the problem I am having I keep getting error
Message: Call to a member function getbank() on a non-object
My code is simple
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
if($query = $this->profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
model
class Profile_model extends CI_Model {
function getbank()
{
$query = $this->db->get('bank');
return $query->results();
}
Its so simple, I cant see a error, thank you.
Try this
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('profile_model'); # Added
$query = $this->profile_model->getbank(); # Changed
if(!empty($query)) # Changed
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
Model
class Profile_model extends CI_Model {
public function getbank() # Changed
{
$query = $this->db->get('bank');
return $query->result(); # Changed
}
}
Update
Simple answer, thanks to all those who tried helping. I had the model file name with a small letter not a capital.
Eg profile_model.php
should be Profile_model.php
However, this solution is elegant and simple, so it improved my code so il accept this one.
It may be because you didn't load your model?
Try changing your controller to:
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('Profile_model');
if($query = $this->Profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}

Message: Undefined property: News::$news_model in Codeigniter

I am learning Codeigniter from Codeigniter mannual step by step. Therefore i used as it is code from the manual.
The Controller class is :
<?php
class News extends CI_Controller{
public function _construct()
{
parent::_construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header',$data);
$this->load->view('news/index',$data);
$this->load->view('templates/footer');
}
?>
The Model Class is:
class News_model extends CI_Model{
public function _construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news',array('slug'=>$slug));
return $query->row_array();
}
?>
Please help me to fix this error. I've Tried every possible solution on internet but couldn't find any mistake.
CodeIgniter uses __construct with two _.
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
The above code should work.
As a side note, you can call your models like this:
$this->load->model('news_model', 'news');
and then you can call it like this:
$this->news->get_news();
But your method works fine, just makes it a bit easier.

Categories