dongzituo5530 2013-07-09 08:59
浏览 13
已采纳

PDO打破time_ago函数

Im currently in the process of converting my site over to PDO but ive hit a little snag, i have a function that converts a datetime into text saying how long ago it was posted, but after changing my query to PDO the function is throwing me an error:

Error:

Notice: Undefined variable: retval in C:\wamp\www\pdoconvert\threads.php on line 301 (line 20 of the function)

Function:

function time_ago($date,$granularity=2) {
    $date = strtotime($date);
    $difference = time() - $date;
    $periods = array('decade' => 315360000,
        'year' => 31536000,
        'month' => 2628000,
        'week' => 604800, 
        'day' => 86400,
        'hour' => 3600,
        'minute' => 60,
        'second' => 1);
    if ($difference < 5) { // less than 5 seconds ago, let's say "just now"
        $retval = "Posted just now";
        return $retval;
    } else {                            
    foreach ($periods as $key => $value) {
        if ($difference >= $value) {
            $time = floor($difference/$value);
            $difference %= $value;
            $retval .= ($retval ? ' ' : '').$time.' ';        <-- Line 301
            $retval .= (($time > 1) ? $key.'s' : $key);
            $granularity--;
        }
        if ($granularity == '0') { break; }
    }
    return ' Posted: '.$retval.' ago';      
    }
}

Query:

$nostmt = $db->prepare ("SELECT threads.Thread_ID, threads.Thread_Title, threads.Board_ID, threads.Author, threads.Thread_Sticky, threads.Thread_Locked, Sub1.LatestPost, Sub1.PostCount, members.Post_As, members2.Member_ID AS LastPostMemberID, members2.Post_As AS LastPostMemberPostAs
        FROM threads 
        INNER JOIN (SELECT Thread_ID, MAX(posts.Post_DateTime) AS LatestPost, COUNT(*) AS PostCount FROM posts GROUP BY Thread_ID) Sub1
        ON threads.Thread_ID = Sub1.Thread_ID
        INNER JOIN members 
        ON threads.Author = members.Member_ID
        INNER JOIN posts posts2
        ON posts2.Thread_ID = Sub1.Thread_ID AND posts2.Post_DateTime = Sub1.LatestPost
        INNER JOIN members members2
        ON members2.Member_ID = posts2.Member_ID
        WHERE threads.Board_ID=:Board_ID 
        AND threads.Thread_Sticky='0'
        ORDER BY Sub1.LatestPost DESC
        LIMIT $offset, $rowsperpage");
$nostmt->bindValue( ":Board_ID", $Board_ID);
$nostmt->execute();
while($row = $nostmt->fetch(PDO::FETCH_ASSOC)) {
    $Thread_ID = $row['Thread_ID']; 
    $Thread_Title = $row['Thread_Title'];   
    $Board_ID = $row['Board_ID'];       
    $Author = $row['Author']; 
    $Thread_Sticky = $row['Thread_Sticky'];  
    $Thread_Locked = $row['Thread_Locked'];  
    $Post_Author = $row['Post_As']; 
    $Posts = $row['PostCount']; 
    $Post_DateTime = time_ago($row['LatestPost']);  <--time-ago function called
    $Member_ID = $row['LastPostMemberID']; 
    $Post_As = $row['LastPostMemberPostAs'];    

what really confuses me is i get this error twice per outputted row but i also get the correct time-ago output aswell.

how should i go about fixing this? is it my shoddy PDO conversion or a fault with the function?

  • 写回答

2条回答 默认 最新

  • dragonpeng200811111 2013-07-09 09:10
    关注

    Your $retval variable isn't defined at that point(line 301).

    To solve this:

    • Define the $retval variable on the first line of the function $retval="";

    or

    • Remove the concatenation from the line 301 $retval=($retval ? ' ' : '').$time.' ';

    I recommend to use the first option

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

报告相同问题?

悬赏问题

  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'