Using external class in my Controller - php

Im working on my first project in CodeIgniter and I wonder How can I use my class from library in my Controller.
libraries/Twitterclass.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Twetterclass {
public function __construct($hashtag, $tweet_id)
{
require_once('TwitterAPIExchange.php');
//There is my working code
//I want to make use of this $n in my Controller
return $n;
}
}
/* End of file Twetterclass.php */
My Controller:
public function microtweets()
{
$params = array('hashtag' => 'somehashtag', 'tweet_id' => '673899616799191040');
$data['count_tweets'] = $this->load->library('Twetterclass', $params);
$this->load->view('tweets', $data);
}
I want to use his extended class in my controller and work there on this $n value or for example display it in my View.
I get few errors:
A PHP Error was encountered
Severity: Warning
Message: Missing argument 2 for Twetterclass::__construct(), called in
/home/jail/kg7dad5/home/kg7dad5/domains/badzlepszy.pl/public_html/coinmonitor/system/core/Loader.php
on line 1246 and defined
Filename: libraries/Twetterclass.php
Line Number: 5
Backtrace:
File: /application/libraries/Twetterclass.php Line: 5 Function:
_error_handler
File: /application/controllers/Cointweet.php Line: 24 Function:
library
File: /public_html/coinmonitor/index.php Line: 292 Function:
require_once

first try to debug parameter you passed in constructor
public function __construct($hashtag, $tweet_id)
{
echo $hashtag;
echo $tweet_id;
die;
}

according to your code, change like this,
public function __construct($arr)
{
echo $arr['hashtag'];
echo $arr['tweet_id'];
exit;
}
Because you are passing 1 array, so access in library with array index.

Related

Undefined property: update_user::$update_model - codeigniter

I am a noob at both PHP and codeigniter, so please be gentle.
I was messing around with this file and got this error.
Message: Undefined property: update_user::$update_model
Filename: controllers/update_user.php
Fatal error: Call to a member function update_user_id() on null in C:.....update_user.php on line 20
here is my code
<?php
class update_user extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('user_model');
}
function show_user_id() {
$id = $this->uri->segment(3);
$data['users'] = $this->update_model->show_users();
$data['single_user'] = $this->update_model->show_user_id($id);
$this->load->view('update_view', $data);
}
function update_user_id() {
$id= $this->input->post('did');
$data = array(
'userName' => $this->input->post('dname'),
'userPass' => $this->input->post('dpass'),
);
$this->update_model->update_user_id($id,$data);
$this->show_user_id();
}
}
this is line 20
$this->update_model->update_user_id($id,$data);
anybody can give any pointers to what is wrong?
thanks in advance
This line:
$this->update_model->update_user_id($id,$data);
Should be:
$this->user_model->update_user_id($id,$data);
Also, every other place where you have used update_model, it should be changed to user_model.
This is assuming that you have only a user_model and no update_model. That's what your code shows.
The error is indicating the problem clearly. you are using update_model model which you haven't loaded in your update_user Controller. If this is it than load the update_model like this $this->load->model('update_model'); else change the code:
$this->update_model->update_user_id($id,$data);
to
$this->user_model->update_user_id($id,$data);
Also please share your model code if there is any issue.

how to use router inside codeigniter helper by ci instance like $CI->router->fetch_method()

I try to create new method inside custom helper,
if ( ! function_exists('active_link'))
{
function active_link($method_name)
{
if($CI->router->fetch_method()==$method_name){
return 'active';
}
return 'none';
}
}
the code dose not work, because I cant get the router by CI instance.
A PHP Error was encountered
Severity: Error Message: Call to a member function
fetch_method() on null Filename: helpers/smart_helper.php
Line Number: 8
Use
$this->router->fetch_method();
Instead of
$CI->router->fetch_method()
I've solved this issue by moving the request of router to the view and pass the two parameters to the function:
in the helper
<?php
if ( ! function_exists('active_link'))
{
function active_link($feched_method,$method_name)
{
if($feched_method==$method_name){
return 'active';
}
return false;
}
}
in the view:
<?php echo active_link($this->router->fetch_method(),'index');?>

How to pass an array to the main view in Codeigniter

It seems that I just can't pass an array in another function into main view.
My index() function is =>
public function index()
{
$this->load->view('anasayfa');
}
My aforementioned function is =>
public function pozisyon_tutma()
{...
....
print_r(array_values($pozisyon));
$GLOBALS['pozisyong'] = $pozisyon;
}
which works...
I am seeking something like =>
public function index()
{
$this->pozisyon_tutma();
$data['hoppa']=$GLOBALS['pozisyong'];
$this->load->view('anasayfa', $data);
}
and I am seeking in view 'anasayfa' (which is the main view) something like this code to work=>
<?php
print_r(array_values($hoppa));
?>
However this code gives the following error=>
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: hoppa
Filename: views/anasayfa.php
Line Number: 19
Backtrace:
File: D:\wamp\www\proje\application\views\anasayfa.php
Line: 19
Function: _error_handler
File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 24
Function: view
File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: array_values() expects parameter 1 to be array, null given
Filename: views/anasayfa.php
Line Number: 19
Backtrace:
File: D:\wamp\www\proje\application\views\anasayfa.php
Line: 19
Function: array_values
File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 24
Function: view
File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
All help will be much appreciated. I checked all other threads in stackoverflaw and could not make any of them work in my case... Thank you all
You should use in your controller:
$data = array('key1'=>$value1, 'key2'=>$value2, 'key3'=>functionX()....);
$this->load->view('yourview.php',$data);
And in the view to access to the array of values:
echo $key1; // prints $value1.
echo $key2; // prints $value2.
Also, you could use $_SESSION to store some variables in CI. Hope it helps.
I did it in the following way=>
public function index()
{
require('Hesaplama.php');
$Hesaplama = new Hesaplama();
$data['hoppa']=$Hesaplama->pozisyon_tutma();
$this->load->view('anasayfa', $data);
}
It works. I returned $pozisyon which is an array as #mic suggested. I will keep the suggestions of #JP. Aulet in case I need them in the near future. Thank you all...

Codeigniter undefined property on core/Model.php

Controller
public function editItem(){
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$item_details = $this->ItemModel->edititem($this->input->get('id'));
$data2['item_name'] = $item_details->name; //THIS IS LINE 28
$data2['item_description']= $item_details->description;
$data2['item_price'] = $item_details->price;
$this->load->view('item/item_edit',$data2);
}
There's an error to my view, and I don't know why
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Item::$name
Filename: core/Model.php
Line Number: 77
Backtrace:
File: C:\xampp\htdocs\itwa213\application\controllers\Item.php
Line: 28
Function: __get
File: C:\xampp\htdocs\itwa213\index.php
Line: 292
Function: require_once
I already checked my autoload on config and it's properly configured with
$autoload['libraries'] = array('database');
You could try this
public function editItem() {
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$item_details = $this->ItemModel->edititem($this->input->get('id'));
$data2['item_name'] = $item_details['name']; //THIS IS LINE 28
$data2['item_description'] = $item_details['description'];
$data2['item_price'] = $item_details['price'];
$this->load->view('item/item_edit',$data2);
}
Your controller function change to like this
public function editItem(){
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$data2['item_details'] =$this->ItemModel->edititem($this->input->get('id'));
$this->load->view('item/item_edit',$data2);
}
and in your view page
echo $item_details->name;
You should look at your code for the get function you use. try to replace the $this->input->get('id') to existing id number first. check whether it success or not. If success by using existed id number, so it should your get function not working or something wrong. i'm seldom use $this->input->get() but $this->input->post().
on your edit link, you can use:
edit.
so the id is the third segment uri. use $this->uri->segment(3); to get the third segment and store into $item_id variable into your controller. so, no need to use $this->input->get(); to get the item_id value.

rows from db in header, which set to display in all page

In CodeIgniter I'm try to made header with code from DB, my controller code:
public function index()
{
$this->load->model('main_model');
$data['result'] = $this->main_model->get_tipsters();
$this->load->view('template/header_view',$data);
}
And header_view:
<?php foreach($result->result() as $row): ?>
<div id="tipster"><img src="<?=$row->photo;?>" /><br /><?=$row->name;?></div>
<?php endforeach; ?>
Header work's only It self view file, but not In others pages.
I Including header like this in controllers:
$this->load->view('template/header_view');
$this->load->view("/bets/index",$data);
$this->load->view('template/footer_view');
Getting this error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: result
Filename: template/header_view.php
Line Number: 14
Fatal error: Call to a member function result() on a non-object in /home/user/domains/test.com/public_html/application/views/template/header_view.php on line 14
Line 14 is foreach, I have copied early.
Since your header view file is always expecting $result, you'll need to provide it for all your controller methods:
$this->load->model('main_model');
$data['result'] = $this->main_model->get_tipsters();
$data['main'] = $this->main_model->get_main_data(); //example
$this->load->view('template/header_view',$data);
$this->load->view("bets/index",$data);
$this->load->view('template/footer_view');
This can become cumbersome, so consider creating a MY_Controller file that extends CI_Controller - more about that here.
You can make a function in your Common_model
for fecthing result on your header file.
and directly get result from that function by calling it in a view file.
common_model.php
function your_function()
{
// code for fetching data for header
/// return your result here
}
call directly this function in a view file as
$rs = $this->Common_model->your_function();
Note: common_model is load by default .if disable then you need to load model in a view file.
Best way to load CI object on view element file(header_view) and load and call model method with CI object
$CI = & get_instance()
$CI->load->model('main_model');
$result = $CI->main_model->get_tipsters();
<?php foreach($result->result() as $row): ?>
<div id="tipster"><img src="<?=$row->photo;?>" /><br /><?=$row->name;?></div>
<?php endforeach; ?>
and remove code from controller

Categories