Probably an easy one - PHP/CodeIgniter 'Undefined Variable'
Morning y'all
This is probably an easy one but I barely got any sleep last night and am struggling to comprehend anything.
I've g开发者_如何学Cot a CodeIgniter library I've made called Points.php. Here's the contents of Points:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Points
{
function __construct()
{
$this->ci =& get_instance();
$this->ci->load->database();
}
function getpoints($params)
{
echo $userid;
}
}
/* End of file Points.php */
/* Location: ./application/libraries/Points.php */ ?>
As you can see, I'm building it up slowly and it's being kept simple.
In one of my views, I want it to display the number of 'points' (which for the time being is simply the third segment of the URI). I call it like this:
<p>Points: <?php $params['user_id']=$this->uri->segment(3,1); echo $this->points->getpoints($params); ?></p>
The warning I get back in the view is this:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: userid
Filename: libraries/Points.php
Yes I know it's such a simple problem but I've tried lots of things. Some variations include echoing in Points.php $params['userid']; etc. But I don't see what I'm doing wrong?
This is my first CodeIgniter class and I've fallen at the first step, haha...
Shouldn't it be
function getpoints($params)
{
return $params['user_id']; // since you're passing the whole array
}
Use return
instead of echo
, you're already using echo on the other side
Crap didnt read your source code properly, you need to access it as an array not a single variable.
$params['userid']
精彩评论