I am using the CodeIgniter framework. I created a library called common_funtions
. This library enables me to call a function in different controllers. One function in particular needs an argument, but I’m getting three errors when I call the function:
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
Here is my code: In the library: 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;
}
In my controller:开发者_如何学Python
$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']);
}
Is there something wrong with my code? Or maybe it’s a bug in my framework? This is not the first time I’m experiencing this.
Have you loaded the library with $this->load->library('common_functions');
?
Also it seems in this situation, you would be more suited to use a model rather than a library, see: http://codeigniter.com/user_guide/general/models.html
精彩评论