0

使用下面的代码,我显示状态和评论,它按需要工作,虽然我不完全理解我自己的代码..

我想实现以下目标:如果结果来自状态,$link_type = status,如果结果来自评论,$link_type = 评论

这是我的尝试($link_type 现在总是“状态”)

    $results = db_query("
    SELECT sid as ssid, created as screated, message as smessage, recipient as srecipient 
    FROM {statuses}
    WHERE created > :logout_stamp
    UNION
    SELECT sid as ssid, created as screated, comment as smessage, uid as srecipient 
    FROM {fbss_comments} fbss
    WHERE fbss.created > :logout_stamp
    ORDER BY screated DESC LIMIT 15", 
    array(':logout_stamp' => $logout_stamp))->fetchAll();

  // foreach the results
  foreach ($results as $result) {
      $user_status = user_load($result->srecipient);
      $user_comment = user_load($result->commentuid);
      $username_status = $user_status->name;
      $username_comment = $user_comment->name;
      $date_status = $result->screated;
      $date_comment = $result->commentcreated;

      if ($result->ccreated != NULL) {
        $link_type = "comment";
      }
      else {
        $link_type = "status";
      }

print '<a href="statuses/' . ($result->ssid) . '" class="notification_wrapper">' . '<b>' . $username_status . '</b>' . ' - ' . ($link_type) . ' - ' . strip_tags(substr($result->smessage,0,30)) . ' - ' . elapsed_time($date_status) . '</a>'; 

非常感谢您的建议

4

3 回答 3

1

使用st因为它返回 1 表示状态,2 表示评论。

 SELECT 1 as st,sid as ssid, created as screated, message as smessage, recipient as srecipient 
    FROM {statuses}
    WHERE created > :logout_stamp
    UNION
    SELECT 2 as st sid as ssid, created as screated, comment as smessage, uid as srecipient 
    FROM {fbss_comments} fbss
    WHERE fbss.created > :logout_stamp
    ORDER BY screated DESC LIMIT 15
于 2013-03-20T13:23:09.023 回答
1

将此值添加为额外字段:

 SELECT sid as ssid, created as screated, message as smessage, recipient as srecipient, "status" as link_type 
FROM {statuses}
WHERE created > :logout_stamp
UNION
SELECT sid as ssid, created as screated, comment as smessage, uid as srecipient,"comment" as link_type 
FROM {fbss_comments} fbss
WHERE fbss.created > :logout_stamp
ORDER BY screated DESC LIMIT 15
于 2013-03-20T13:25:27.647 回答
1

您可以在查询中添加一个字段,如下所示:

$results = db_query("
    SELECT sid as ssid, created as screated, message as smessage, recipient as srecipient, 'status' as link_type
    FROM {statuses}
    WHERE created > :logout_stamp
    UNION
    SELECT sid as ssid, created as screated, comment as smessage, uid as srecipient, 'comment' as link_type
    FROM {fbss_comments} fbss
    WHERE fbss.created > :logout_stamp
    ORDER BY screated DESC LIMIT 15", 
    array(':logout_stamp' => $logout_stamp))->fetchAll();

现在在 if 条件中使用新字段:

$link_type = ($result->link_type == 'comment') ? "comment" : "status";
于 2013-03-20T13:31:17.787 回答