【问题标题】:pass values from a function to another function in controller将值从函数传递到控制器中的另一个函数
【发布时间】:2014-12-03 11:27:15
【问题描述】:

我在一个控制器中有 2 个功能,

function feed()
{
    $xml = simplexml_load_file('http://localhost/feed.php');

    // pass to other function
    $this->purchased($xml->prices);

    foreach ($xml->prices as $price) {

        echo ' <tr id="'.$price->sign.'"><td class="price">'.$price->wholesale.'</td>';
    }
}

在上述函数中,我从提要中获取一些值并使用 jquery 将其附加到前端的 html

在下面的函数中,我所做的是列出特定用户购买的所有产品。此功能也每 5 秒刷新一次。

function purchased($price)
{
    foreach ($price as $x)
    {
        $retail = $x->retail;
    }
}

我需要做的是将function feed()返回的值返回给购买的函数进行一些计算..但是当我使用上述方法时,我得到以下错误

消息:未定义变量:价格

消息:Actions::purchased() 缺少参数 1

谁能告诉我如何从feed 函数中获取prices 并将其与purchased 函数一起使用?

【问题讨论】:

  • 代码的哪一行显示了这个错误?这可以为您指明正确的方向。
  • 您确定 simplexml_load_file 正在返回您需要的内容吗? var_dump 要检查的 $xml 变量。
  • @bakkelun 上线了function purchased($price) @Craig yep it returns the values because, when i go to the feed function, it shows all the values correctly. cut the values arnt taken to the purchased function
  • 您在哪个函数和哪一行遇到这两个错误。

标签: php codeigniter codeigniter-2


【解决方案1】:

不确定我是否理解你在做什么以及你想要实现什么,但是..

仅在调用函数时传递变量才有效。因此,当您执行 feed() 函数时,您会调用 purchased() 函数传递变量。 purchased() 工作,结束,然后脚本返回 feed() 函数。

从其他任何地方调用 purchased() 不会为您提供 feed() 函数的值。

尝试将函数更改为:

function purchased($price = '')
{
    if (!isset ($price) || empty($price)) {
        $xml = simplexml_load_file('http://localhost/feed.php');
        $price = $xml->prices;
    }
    foreach ($price as $x) {
        $retail = $x->retail;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    相关资源
    最近更新 更多