2011-06-10 61 views
2

我正在尝试创建固定链接,以在使用分页和按关联/投票/大多数评论进行排序的页面上发表评论。这意味着评论可以在任何页面上。现在我似乎无法弄清楚如何为特定评论创建永久链接。有任何想法吗?创建固定链接以对带分页的页面发表评论

我在Codeigniter中使用PHP/mySQL。

+0

是否会评论或发表评论有其自己的网页,或者是您可以在列表中查看它的唯一方法? – dqhendricks 2011-06-10 07:08:00

回答

1

您可以使用临时表来做到这一点。

比方说,你有文章控制器,显示方法:

article/show/[article_id]/[sort_by_something]/[sort_order]/page/[page_number]/article-magic-seo-friendly-title.html 

那么永久链接可能是这样的:

article/show_comment/[article_id]/[sort_by]/[sort_order]/[comment_id]/[comment_dom_id] 

然后你抓住所有你需要为你的查询:

function show_comment($article_id, $sort_by, $sort_order, $comment_id, $comment_dom_id) 
{ 
    // for the sake of example no validation and prepping here - do it in your code ofc 
    $this->db->query('CREATE TEMPORARY TABLE tmp_comments (position INT,id INT,sort INT)'); 
    $this->db->query('SET @pos := 0'); 
    $this->db->query(
     "INSERT INTO tmp_comments (position, id, sort) 
      SELECT @pos := @pos+1, id, $sort_by 
      FROM comments 
      WHERE article_id = $article_id 
      ORDER BY $sort_by $sort_order 
     "); 
    $result = $this->db->query("SELECT position FROM tmp_comments WHERE id = $comment_id LIMIT 1")->row()); 
    $position = $result->position; 
    // having current comment's position we can easily calculate page number, 
    // eg. for 10 comments per page: 
    $page = ceil($position/10); 

    // then just redirect to that page: 
    redirect('article/show/$article_id/$sort_by/$sort_order/page/$page/article-magic-seo-friendly-title.html#$comment_dom_id'); 
} 
+0

哇真棒:)顺便说一句,DOM中的DOM ID是什么意思? – Nyxynyx 2011-06-11 01:30:44

+1

http://www.w3.org/DOM/这意味着它就像

2011-06-11 08:17:52

+0

非常感谢!!! – Nyxynyx 2011-06-14 00:03:33