I'm using codeigniter as a framework.
Firstly, I use:
public function edit_ads_wait($id)
{
$where["user"]=$this->session->userdata("userid");
$where["id"]=$id;
$where["state"]=2;
$info=$this->base->get_where_row("advertisement",$where);
if($info) // if $info isset
{
$where_cat["deleted"]=0;
$data["info"]=$info;
$data["category"]=$this->base->get_where("category",$where_cat,$order);
$data["region"]=$this->base->get("region");
$this->userskin->loadview("user/ads/edit_ads",$data);
}else // if $info is not set
{
$this->clear_session();
}
}
when i execute edit_ads_wait function -where the $info isset- it also run clear_session() function and my session is cleared
the clear_session is
function clear_session()
{
foreach ($this->session->all_userdata() as $key=>$val)
{
if(strlen($key)>=20)
{
$this->session->unset_userdata($key);
}
//print_r($key);
}
}
please help me...
i guess you are using the function inside of the "view" not the "controller", if so
a quick fix : try to add exit(); at the end of your if condition before the else .
or if your function inside your controller class make sure the class looks like that
ex : if the controller file name is example.php :
<?php
class Example extends CI_Controller {
//your constructor
function exmple() {
parent::__construct();
// you can load libraries and helpers here
}
// your other functions
}
?>
Can you please try this:
if $info is an array then:
if(isset($info) && !empty($info))
if $info is a single value then:
if(isset($info) && $info != '')
Related
I am trying to learn code igniter and creating helper files. I have this as my functions_helper.php file located in my applications/helpers folder:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
if (!function_exists('check_status')){
function check_status()
{
$CI = & get_instance();
$result=$CI->user->get_status(); //this is line 14!!
if($result)
{
$status_array=array();
foreach($result as $row)
{
$status_array=array(
'source' => $row->status,
'current' => $row->id
);
if ($status_array['current'] == 1) {
$status_array['current'] = "Live";
} else {
$status_array['current'] = "Amazon is Down";
}
$CI->session->set_userdata('status',$status_array);
}
return TRUE;
}
else
{
return false;
}
} // END OF CHECK_STATUS FUNCTION
}
From everything i can find, I am doing it correctly, yet I am getting this error:
PHP Fatal error: Call to a member function get_status() on a non-object in function_helper.php on line 14. exactly what am i doing wrong? Is this the best way to call a function? Ultimately I am trying to get information returned from a db query.
My get_status function is:
//FUNCTION TO SEE THE STATUS OF AMAZON
function get_status() {
$query = $this->db->query("select * from amz where active = 1");
if($query->num_rows()==1) {
return $query->row_array();
}else {
return false;
}
} //END OF GET_STATUS FUNCTION
Remove this code
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
There is no OO in helper files. You do not have a class -__construct() are used to initialize classes.
Then move
$this->load->model('user','',TRUE); to the check_status function
$CI =& get_instance();
$CI->load->model('user','','TRUE');
I have a if else statement in codeigniter and try to direct different view but its gives error to me.However when i echo it prints to screen
This works !
public function kayitEmailOnay() {
$registrationCode = $this->uri->segment(3);
if ($registrationCode == '') {
echo "URLde onay kodu yok";
}
$registrationConfirmed = $this->kayitmodel->uyeOnay($registrationCode);
if ($registrationConfirmed)
echo "true";
else
echo "false";
}
This does not work
public function kayitSon() {
$this->load->view("kayit/kayitTamamla");
}
public function kayitHata() {
$this->load->view("kayit/kayitHata");
}
public function kayitEmailOnay() {
$registrationCode = $this->uri->segment(3);
if ($registrationCode == '') {
echo "URLde onay kodu yok";
}
$registrationConfirmed = $this->kayitmodel->uyeOnay($registrationCode);
if ($registrationConfirmed)
kayitSon();
else
kayitHata();
}
The error is that :
you should call your function like this:
$this->kayitSon();
$this->kayitHata();
Class method is called with the object or the object reference. if you are calling a class method inside that class use $this keyword
Try calling with $this
$this->kayitSon();
Okay, so i got class file, in which i got function -
var $text;
public function languages()
{
if (isset($_GET['lang']) && $_GET['lang'] != '')
{
$_SESSION['lang'] = $_GET['lang'];
}
switch($_SESSION['lang'])
{
case 'en_EN': require_once('language/lang.eng.php');break;
case 'lv_LV': require_once('language/lang.lv.php');break;
case 'ru_RU': require_once('language/lang.ru.php');break;
default: require_once('language/lang.eng.php');
}
$this->text = $text;
}
public function translate($txt)
{
if(isset($this->text[$txt]))
{
return $this->text[$txt];
}
}
If i am translating via index.php like this - > echo $index->translate('search'); it translates ok, but if i am translating something in class file for example -
if ($country_rows > 0)
{
$_SESSION['country'] = $_GET['country'];
}
else
{
$_SESSION['country'] = $this->translate('all_countries');
}
}
if ($_SESSION['country'] == '')
{
$_SESSION['country'] = $this->translate('all_countries');
}
it doesn't show up.
In index.php header i got included -
require_once('class.index.php');
$index = new index;
$index->get_country();
$index->languages();
What could be the problem, and how can i fix it, so i can translate everything inside class file too? will appreciate your help!
1st guess:
no session started?
session_start();
2nd guess:
assuming that you use $this->translate() in another class, you should initiate the object first, in the following example I pass translate class to var $index;
<?
include_once('class.index.php');
class myClass {
var $index;
public function __construct() {
$index = new index();
$index->get_country();
$index->languages();
$this->index = $index;
}
public function yourFunction() {
echo $this->index->translate('all_countries');
print_r($this->index);
}
}
?>
I tried a lot of search but unable to figure out why array $wordlinks in function DoWordLink is not carrying values from function __construct. PHP class code as below:
<?php
class autolinkkeyword
{
public $wordlinks = array();
public function __construct(){
$query = mysql_query("SELECT keyword FROM library");
while ($row = mysql_fetch_array($query))
{
$this->wordlinks [$row["keyword"]] = $row["keyword"];
}
}
public function linkkeywords ($posts)
{
function DoWordLink($match)
{
$rpl=$match[1];
if(isset($wordlinks[$rpl]))
{
$kword = $this->wordlinks[$rpl];
$rpl="<a class=\"keyword_link\" href=\"#\" onclick=\"popup('popUpDiv');
ajax_loadContent('kword', 'library.php?keyword=$kword')\">$kword</a>";
unset($this->wordlinks[$match[1]]);
}
return $rpl;
}
$wl=array_keys($this->wordlinks);
$pm="/((?<=\s|^)(?:" . implode('|',$wl) .")(?=\.|\!|\?|\,|\'|\s|$))/im";
foreach($posts as $key => $mainbody)
{
$mainbody=preg_replace_callback($pm, 'DoWordLink', $mainbody) ;
echo $mainbody;
}
}
}
?>
You can make it an actual method of that class and call it using this method:
http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback
like:
preg_replace_callback($pm, array($this, 'DoWordLink'), $mainbody);
Change DoWordLink function so it is part of the class like:
class autolinkkeyword
{
function DoWordLink($match)
{
$rpl=$match[1];
if(isset($this->wordlinks[$rpl]))
{
$kword = $this->wordlinks[$rpl];
$rpl="<a class=\"keyword_link\" href=\"#\" onclick=\"popup('popUpDiv');
ajax_loadContent('kword', 'library.php?keyword=$kword')\">$kword</a>";
unset($this->wordlinks[$match[1]]);
}
return $rpl;
}
}
aren't you missing a "this->" construct here? if(isset($this->wordlinks[$rpl]))
Use the $this everywhere you refer to $wordlinks.
$this->wordlinks
You need to access the property in your linkkeywords-method with the object-accessor, too!
public function linkkeywords ($posts)
{
// Here use $this->wordlinks not $wordlinks
}
I'm building a little MVC system (learning) and I have some problems with showing variables in my view files.
This is from my View class:
private $vars = array();
public function __set($key, $value)
{
$this->vars[$key] = $value;
}
public function __get($key)
{
return $this->vars[$key];
}
public function __toString()
{
return $this->vars[$key];
}
public function show($file)
{
global $router;
$folder = strtolower($router->current_controller);
$path = VIEWPATH.$folder.'/'.$file.'.phtml';
if ( ! file_exists($path))
{
die("Template: $file, not found");
}
include ($path);
}
And here is from my controller:
$test = new View();
$test->name = 'karl';
$test->show('name_view');
And the view file (name_view)
echo $name // doesn't work
echo $this->name // Works
What am I doing wrong? Perhaps I haft to make something global?
THX / Tobias
EDIT: I just extracted the vars array in the view class right before I include the view file and then it worked.. Thank you for all help.
There is no $key in __toString()!
Also __toString() doesn't accept any parameters!
Test it with this:
public function __toString()
{
return json_encode($this->vars);
}
After your edit I realized that your problem is not on the __toString() method (you can just delete it since you're not using it). Doing echo $this->name is the correct way to show variables from inside your view in your case, however if you want to just do echo $name may I suggest a different approach?
function View($view)
{
if (is_file($view) === true)
{
$arguments = array_slice(func_get_args(), 1);
foreach ($arguments as $argument)
{
if (is_array($argument) === true)
{
extract($argument, EXTR_OVERWRITE);
}
}
require($view);
}
}
Use the View function like this:
$data = array
(
'name' => 'karl',
);
View('/path/to/your/name_view.phtml', $data);
Now it should work just by doing echo $name;, you can adapt it to your View class if you want to. If that doesn't work, try changing the name_view view extension to .php.