2011-06-10 62 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