我正在使用CodeIgniter框架。我创建了一个名为common_funtions
的库。这个库使我能够在不同的控制器中调用一个函数。一个函数特别需要一个参数,但是当我调用函数时我得到三个错误:
A PHP Error was encountered Severity: Warning Message: Missing argument 1 for Common_functions::time_ago(), called in \...\get_comments.php on line 11 and defined Filename: libraries/Common_functions.php Line Number: 20 A PHP Error was encountered Severity: Notice Message: Undefined variable: time Filename: libraries/Common_functions.php Line Number: 26 A PHP Error was encountered Severity: Notice Message: Undefined variable: time Filename: libraries/Common_functions.php Line Number: 39
这是我的代码: 在库中:common_functions.php
// The following function calculates ‘how long ago’
function time_ago($time)
{
$periods = array('second', 'minute', 'hour', 'day');
$lengths = array('60', '60', '24', '7');
$tense = 'ago';
$now = time();
$time_diff = $now - $time;
for ($j = 0; $time_diff >= $lengths[$j] && $j < count($lengths)-1; $j++)
{
$time_diff /= $lengths[$j];
}
$time_diff = round($time_diff);
if ($time_diff != 1)
{
$periods[$j].='s';
}
if ($time_diff > 7 && $periods[$j] == 'days')
{
$time_ago = date("l dS F, Y", $time);
}
else
{
$time_ago = $time_diff." ".$periods[$j]." ago";
}
return $time_ago;
}
在我的控制器中:
$query = $this->db->query(“
SELECT *
FROM post_comments
WHERE p_id = ‘$id’
ORDER BY date ASC”);
$comments = $query->result_array();
foreach ($comments as $comment)
{
$comment['date'] = $this->common_functions->time_ago($comment['date']);
}
我的代码有问题吗?或者也许这是我框架中的错误? 这不是我第一次遇到这种情况。
答案 0 :(得分:0)
您是否使用$this->load->library('common_functions');
加载了库?
在这种情况下,您似乎更适合使用模型而不是库,请参阅:http://codeigniter.com/user_guide/general/models.html