I have this following code in the Address controller
public function index()
{
if($this->session->userdata('isLogin')) {
$this->load->driver('cache');
$this->load->model('MemberModel');
if(!$this->cache->get('province') == false) {
$this->load->model('ShippingModel');
$data['provinces'] = $this->ShippingModel->the_provinces(); // it will return json object
$this->cache->save('province', $data['provinces'], 300);
} else {
$data['provinces'] = $this->cache->get('province');
}
$userdata = $this->MemberModel->getProfile($this->session->userdata('userid'));
$data['user'] = $userdata;
$this->display_member_area('member/address', $data);
}
else {
redirect(base_url());
}
}
When I want to get the data using:
var_dump($this->cache->get('province'));
the result I get always shows
bool(false)
but when I tried to do this instead
var_dump($data['provinces']) // it's show me json object, that I want
Can anyone please show me where I'am doing wrong?
Thanks in advance
Remove false from the if condition and use this code.
if(!$this->cache->get('province')) {
$this->load->model('ShippingModel');
$data['provinces'] = $this->ShippingModel->the_provinces();
$this->cache->save('province', $data['provinces'], 300);
} else {
$data['provinces'] = $this->cache->get('province');
}
Make sure you have the correct credential on your config at:
https://github.com/bcit-ci/CodeIgniter/blob/develop/application/config/memcached.php
it will be at you application/config/memcached.php file. Hostname is the ip you setup your memcached instance. You could provide multiple server if you wish.
reference on installing memcached
Related
Hi everyone I am getting the following error when I submit my form for my CI 3 website:
Fatal error: Call to a member function insert() on null
This error is occurring on line 20 which is:
$query = $this->db->insert('temp_subscribed_users', $data);
Here is the full function:
public function add_temp_user($key)
{
echo "hello";
$data = array(
'TEMP_EMAIL' => $this->input->post('email'),
'TEMP_KEY' => $key
);
echo var_dump($data);
$query = $this->db->insert('temp_subscribed_users', $data);
if($query)
{
return true;
}else{
return false;
}
}
I am not sure what it means by null. The table name is correct and I did a var_dump to confirm that the array is being populated. I also made sure that I am getting into the function by echoing "hello" and it is outputting onto the page.
Any help is appreciated thank you!
Additional info: Running using XAMPP localhost.
load database and then call insert function. Codeigniter does not load database automatically for the performance issue.
$this->load->database();
$query = $this->db->insert('temp_subscribed_users', $data);
Well first of all you're not utilizing the MVC model of codeigniter. Controller is for functions, Model is for the database connections.
First autoload your database, If not just put it in the code. But here is how it should look like.
CONTROLLER FUNCTION
public function add_temp_user($key)
{
echo "hello";
$this->load->model('MY_MODEL');
//If you're not autoloading db include the next line
//$this->load->library('database');
$data = array(
'TEMP_EMAIL' => $this->input->post('email'),
'TEMP_KEY' => $key
);
echo var_dump($data);
//If you confirmed the data var dumped
$success = $this->MY_MODEL->insert_to_db($data);
if($success == true)
{
//Do something
}
else
{
//Do something
}
}
MODEL
public function insert_to_db($data)
{
$query = $this->db->insert('temp_subscribed_users', $data);
//
if($query)
{
return true;
}
else
{
return false;
}
}
Make sure the TEMP_EMAIL and TEMP_KEY are the columns in your database and temp_subscribed_users is your table name
Try to solve your problem by getting into the autoload.php in the config folder and add database on the array for libraries, like this: $autoload['libraries'] = array('database');
Please check whether object is created or not.
Check that object is available in that class
I can't put real code here because is very long and will be hard to
explain.
I have users table in database and I have data table in database too.
So, to get the user data I'll pass user_id as parameter. Like this:
public function get_user_data($user_id) {
}
But. I can only get 1 data per "request". (Keep reading)
public function user_data() {
$getUsers = $this->db->get('users');
foreach($getUsers->result_array() as $user)
{
$data = $this->get_user_data($user->ID);
var_dump($data); // Only return 1 data;
}
}
But, I guess that have an way to "bypass" this but I don't know. I'm having trouble thinking.
As I said, I want to "bypass" this, and be able to send multiple user IDs, my real function do not accept that by default and can't be changed.
Thanks in advance!
replace
foreach($getUsers->result_array() as $user)
{
$data = $this->get_user_data($user->ID);
var_dump($data); // Only return 1 data;
}
to this
foreach($getUsers->result_array() as $user)
{
$data[] = $this->get_user_data($user->ID);
}
var_dump($data);
If you are aiming at sending more data to the function, you always need to make signature change of your function as one of the below :
function get_user_data() {
$args = func_get_args();
/** now you can access these as $args[0], $args[1] **/
}
Or
function get_user_data(...$user_ids) {
/** now you can access these as $user_ids[0], $user_ids[1] **/
}
// Only higher version of PHP
But I am not sure how you will handle returning data.
EDIT: Yes, then in the function, you can collect data in array and return an array of data from function.
If you can change in your function from where to where_in I think you will get an easy solution.
public function get_user_data($user_ids)
{
// your db code
$this->db->where_in('ID',$user_ids); //replace where with where_in
}
public function user_data()
{
$getUsers = $this->db->get('users');
foreach($getUsers->result_array() as $user)
{
$user_ids[] = $user->ID;
}
$this->get_user_data($user_ids);
}
I am using PHP Memcached & when I delete a key, I can still retrieve the key. What could I be doing wrong?
function __construct() {
$this->_cache = array();
// if we have memcache support, load it from CACHE_POOL
//
if (class_exists('Memcached')) {
$this->_mc = new Memcached('CACHE_POOL');
$servers = $this->_mc->getServerList();
if (empty($servers)) {
//This code block will only execute if we are setting up a new EG(persistent_list) entry
$this->_mc->setOption(Memcached::OPT_RECV_TIMEOUT, 1000);
$this->_mc->setOption(Memcached::OPT_SEND_TIMEOUT, 3000);
$this->_mc->setOption(Memcached::OPT_TCP_NODELAY, true);
$this->_mc->setOption(Memcached::OPT_PREFIX_KEY, "md_");
$this->_mc->addServers(self::$_MEMCACHE_IPS);
}
$current_cache = $this->_mc->get(self::CACHE_KEY);
if ($current_cache) {
$this->_cache = array_merge($this->_cache, $current_cache);
}
}
}
function delete($key) {
self::instance()->_mc->delete($key);
}
function getSafe($key) {
return isset($this->_cache[$key]) ? $this->_cache[$key] : FALSE;
}
self::instance()->delete("test");
echo(self::instance()->getSafe("test"));
After running this, the get still returns a value. Not sure what is going on here.
You should also delete cache from _cache property in terms of the retrieving method:
function delete($key) {
self::instance()->_mc->delete($key);
unset(self::instance()->_cache[$key]);
}
But do not apply this code design in your production environment.
I am getting error page not found when I implementation URL using slug.
This is code in controller
function view($slug)
{
$this->data['halaman_item'] = $this->mhalaman->get_profil($slug);
if (empty($this->data['halaman_item'])) {
show_404();
}
$this->data['title'] = $this->data['halaman_item']['judul']; $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('mahasiswa/profil/sejarah', $this->data, true);
$this->load->view('template/wrapper/mahasiswa/wrapper_content',$this->data);
}
Model
function get_profil($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get($this->tbl_halaman);
return $query->result_array();
}
$query = $this->db->get_where($this->tbl_halaman, array('slug'=>$slug));
return $query->row_array();
}
Route
$route['profil/view'] = "profil/view";
$route['profil/(:any)'] = "profil/view/$1";
$route['profil'] = "profil";
When I run this link tkd/index.php/mahasiswa/profil/sejarah is 404 page is not found. may be you know where is the probelm.
Help me what to do. Thank you.
Well, without knowing how much of the rest of your app works...the first obvious thing is making sure $this->data['halaman_item'] actually has data in it...because if not, it's calling show_404 before anything happens.
The script works fine and is setting the data, but the website code is unable to use it and is instead setting its own memcached values. My website code is written in codeIgniter framework. I don't know why this is happening.
My script code :-
function getFromMemcached($string) {
$memcached_library = new Memcached();
$memcached_library->addServer('localhost', 11211);
$result = $memcached_library->get(md5($string));
return $result;
}
function setInMemcached($string,$result,$TTL = 1800) {
$memcached_library = new Memcached();
$memcached_library->addServer('localhost', 11211);
$memcached_library->set(md5($string),$result, $TTL);
}
/*---------- Function stores complete product page as one function call cache -----------------*/
function getCachedCompleteProduct($productId,$brand)
{
$result = array();
$result = getFromMemcached($productId." product page");
if(true==empty($result))
{
//------- REST CODE storing data in $result------
setInMemcached($productId." product page",$result,1800);
}
return $result;
}
Website Code :-
private function getFromMemcached($string) {
$result = $this->memcached_library->get(md5($string));
return $result;
}
private function setInMemcached($string,$result,$TTL = 1800) {
$this->memcached_library->add(md5($string),$result, $TTL);
}
/*---------- Function stores complete product page as one function call cache -----------------*/
public function getCachedCompleteProduct($productId,$brand)
{
$result = array();
$result = $this->getFromMemcached($productId." product page");
if(true==empty($result))
{
// ----------- Rest Code storing data in $result
$this->setInMemcached($productId." product page",$result,1800);
}
return $result;
}
This is saving data in memcached. I checked by printing inside the if condition and checking the final result
Based on the CodeIgniter docs, you can make use of:
class YourController extends CI_Controller() {
function __construct() {
$this->load->driver('cache');
}
private function getFromMemcached($key) {
$result = $this->cache->memcached->get(md5($key));
return $result;
}
private function setInMemcached($key, $value, $TTL = 1800) {
$this->cache->memcached->save(md5($key), $value, $TTL);
}
public function getCachedCompleteProduct($productId,$brand) {
$result = array();
$result = $this->getFromMemcached($productId." product page");
if( empty($result) ) {
// ----------- Rest Code storing data in $result
$this->setInMemcached($productId." product page",$result,1800);
}
return $result;
}
}
Personally try to avoid 3rd party libraries if it already exists in the core framework. And I have tested this, it's working superbly, so that should fix this for you :)
Just remember to follow the instructions at http://ellislab.com/codeigniter/user-guide/libraries/caching.html#memcached to set the config as needed for the memcache server