duanniubeng2265 2010-04-19 11:10
浏览 51
已采纳

可能很简单 - PHP / CodeIgniter'未定义变量'

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 got 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...

  • 写回答

2条回答 默认 最新

  • dozc1071 2010-04-19 11:16
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试