call my own library within a view in codeigniter - codeigniter

i just created my own library on this folder (application/library) and following all steps to create individual library,
once i load this library in my controller it execute the function, but when trying to pass it to the view, nothing return
here is my code
MY OWN FUNCTION
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common {
public function date_arabic()
{
$daysarabic=array('الأحد','الاثنين','الثلاثاء'
,'الأربعاء','الخميس','الجمعة','السبت');
$monarabic=array('','يناير','فبراير','مارس',
'أبريل','مايو','يونيو','يوليو'
,'أغسطس','سبتمبر','أكتوبر','نوفمبر','ديسمبر');
$date=getdate(time());
echo $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
}
}
MY Controller
//arabic date
$this->load->library('Common');
$this->common->date_arabic();
here it prints out the data in my own function, i tried to store this in a $data to pass it to the view like that
//arabic date
$this->load->library('Common');
$data['date_arabic'] = $this->common->date_arabic();
...
$this->load->view('home_page.php', $data);
then when going to view i just type
<?php echo $date_arabic ; ?>
but nothing returned

In your function, change the last line from this:
echo $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
to this:
return $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;

when you are writing libraries, you have to manually grab the Codeigniter instance like this
$CI =& get_instance();
then you would use $CI where you would normally use $this to interact with loaded codeigniter resources
so...
instead of
$this->input->post();
you would write
$CI->input->post();
EXAMPLE LIBRARY STRUCTURE
class Examplelib {
// declare your CI instance class-wide private
private $CI;
public function __construct()
{
// get the CI instance and store it class wide
$this->CI =& get_instance();
}
public function lib_function()
{
// use it here
$this->CI->db->etc()
}
public function another_func()
{
// and here
$this->CI->input->post();
}
}

Related

Xcrud with codeigniter 3

I'm a very happy user of XCRUD V 1.6. I want to integrate it in Codeigniter 3 but when I use the codeigniter session-library in for my XCRUD-pages I get an error in XCRUD: "The verification key is out of date". When the session-library from codeigniter is not being used, XCRUD is working fine. Is there anyone who can tell me how to fix? I need the codeigniter session-library in my xcrud pages for user-details.
This is my current controller: (the session library is loaded in the login_model)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cms extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
include('cms/xcrud/xcrud.php');
$this->load->model("login_model");
}
public function nieuws()
{
$xcrud = Xcrud::get_instance();
$xcrud->table('nieuws');
$output['output'] = $xcrud->render();
$this->_cms_output($output);
}
function useAuthorID($post_array) {
$post_array['afdeling_id'] = $this->login_model->id();
return $post_array;
}
function useCatID($post_array) {
$post_array['nieuwscat_afdeling_id'] = $this->login_model->id();
return $post_array;
}
function _cms_output($output = null)
{
$this->load->view('beheer/default',$output);
}
}
Assuming that you are using xcrud's CI integration examples, you may try the following in xcrud_config.php:
Use the same session name that your CI installation is using, if it differs from "PHPSESSID".
Use alternative session.

Codeigniter extend Blueimp Upload library

I have blueimp file upload working well with codeigniter. I use the UploadHandler library as is. But I want to extend it to replace two functions that create the unique filename. The code for this is in the BlueImp Github wiki.
I created an extended library thus:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_UploadHandler extends UploadHandler {
public function __construct() {
parent::__construct();
$CI =& get_instance();
$CI->load->library('UploadHandler');
}
protected function upcount_name_callback($matches) {
$index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
$ext = isset($matches[2]) ? $matches[2] : '';
return '-'.$index.''.$ext;
}
protected function upcount_name($name) {
return preg_replace_callback(
'/(?:(?:-([\d]+))?(\.[^.]+))?$/',
array($this, 'upcount_name_callback'),
$name,
1
);
}
}
When I try to run it, I get an 'unable to load UploadHandler' error. If I remove my MY extension, the original code runs. What is wrong with my extension code? Isn't this the proper way to extend CI libraries?
And, yes, the filename for my file is MY_UploadHandler.php
Thanks!
I noodled over this for a while and finally figured it out. To extend a custom library, you need to 'require once' the file it is extending, prior to the definition of the class.
EX:
require_once("UploadHandler.php");
class MY_UploadHandler extends UploadHandler
{
}
Hopefully that helps.

Rolling-curl Codeigniter library

I've been using this rolling-curl library with success outside of Codeigniter http://code.google.com/p/rolling-curl/source/browse/#svn%2Ftrunk
I want to use the library with Codeigniter but cannot get it to work.
I've created a file called Rollingcurl.php and placed in codeigniter's application/library folder. In this file I've copied the originals RollingCurl.php file's content (see RollingCurl.php file in link posted above).
My controller looks like this:
class Rolling_curl extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index(){
$this->load->library('rollingcurl', 'request_callback');
$this->rollingcurl->request("http://www.google.com");
$this->rollingcurl->execute();
}
function request_callback($response, $info, $request) {
// parse the page title out of the returned HTML
if (preg_match("~<title>(.*?)</title>~i", $response, $out)) {
$title = $out[1];
}
echo "<b>$title</b><br />";
print_r($info);
print_r($request);
echo "<hr>";
}
In the index function I'm loading the rollingcurl library I've created from the orginal RollingCurl.php and passing the parameter 'request_callback', which is the name of the other function in the controller (see orginal rolling-curl example: http://code.google.com/p/rolling-curl/source/browse/trunk/example.php).
But, it doesn't work... What am I doing wrong?

Cannot load a model in codeigniter

I failed to load a model from my controller
This is the controller file, article.php:
<?php
class Article extends CI_Controller {
function show($id) { //id'ye gore getir
$this->load->model('articles_model');
$parameter = $this->articles_model>getarticle($id);
$this->my_template->build('article_view', $parameter);
}
}
?>
This is the model file, articles_model.php:
<?php
class Articles_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function Getarticle($id) {
parent::Model();
$query = $this->db->get_where('articles', array('id' => $id));
$data['articles'] = $query->result();
return $data;
$query->free_result();
}
}
?>
just to add, i even tried to load it from autoloader, still no chance, i assume something is wrong with the model, or the whole system broke.
up: the models loads without problems, if i put echo in __construct function, it works, however, i cannot call the getarticle function. geez
UP: I did it! according to http://grasshopperpebbles.com/codeigniter/codeigniter-call-to-a-member-function-on-a-non-object/
i used
$CI =& get_instance();
and called the function
$CI->articles_model->getarticle($id) and it called the function
It should be,
$CI =&get_instance();
$CI->load->model('articles_model');
$parameter = $CI->articles_model>getarticle($id);
There's a parse error in the following line:
$parameter = $this->articles_model>getarticle($id);
It should be:
$parameter = $this->articles_model->getarticle($id);
Does that fix your problem? If not, what error message are you seeing?
Leif's answer is the right one. Just to add one thing: you don't have to use the long variable name like $this->articles_model over and over, by using the second parameter:
$this->load->model('articles_model','artm');
$parameter = $this->artm->getarticle($id);
Just a little faster to type, and can reduce typos like the one in your sample.

How to change the value of a variable inside a controller from a view file (html)?

I followed this tutorial: http://codeigniter.com/wiki/Internationalization_and_the_Template_Parser_Class/
The controller that loads the language is this one:
<?php
class Example extends Controller {
function Example() {
parent::Controller();
# Load libraries
$this->load->library('parser');
# Load language
$this->lang->load('example', 'english');
}
function index() {
# Load variables into the template parser
$data = $this->lang->language;
# Display view
$this->parser->parse('example', $data);
}
}
?>
In order to change the language I have to manually change english to say spanish in the controller.
What's the best way the user can do this from the index.php file (view)?
The best thing to do is have the user select a supported language on some page, set it as a session variable and call it when ever you need to load a language
$language = $this->session->userdata("language");
$this->lang->load("example", $language);
$data = $this->lang->language;
$this->parser->parse("example", $data);
EDITED BELOW
If you're using CodeIgniter and you're new to this, I wouldn't suggest messing with the index.php file.
You want to do it inside your controller by loading a form where they can pick their language and storing it in the session. I'd also suggest autoloading your session library.
The controller:
<?php
class Home extends Controller {
function Home()
{
parent::Controller();
$this->load->library("session");
}
function index()
{
$language = $this->session->userdata("language");
$this->lang->load("example", $language);
$data = $this->lang->language;
$this->parser->parse("example", $data);
}
function set_lang()
{
if( ! $this->form_validation->run())
{
$this->load->view("select_language_form");
}
else
{
$language = $this->input->post('language', TRUE);
$this->session->set_userdata('language', $language);
redirect('home' 'location');
}
}
}

Resources