私は現在、自分のサイトを PDO に変換している途中ですが、少し問題があります。日時を投稿された時間を示すテキストに変換する関数がありますが、クエリを PDO に変更した後、関数は私にエラー:
エラー:
Notice: Undefined variable: retval in C:\wamp\www\pdoconvert\threads.php on line 301 (line 20 of the 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';
}
}
クエリ:
$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'];
本当に混乱しているのは、出力された行ごとにこのエラーが2回発生することですが、正しい時間前の出力も得られます。
これを修正するにはどうすればよいですか?それは私の見掛け倒しの PDO 変換ですか、それとも関数の障害ですか?