简体   繁体   English

使用分页在页面上创建评论的永久链接

[英]Creating a Permalink to a Comment on a Page with Pagination

I'm trying to create a permalink to a comment on a page that uses pagination and sorting by relavance/votes/most reviews.我正在尝试为使用分页和按相关/投票/大多数评论排序的页面上的评论创建一个永久链接。 This means that the comment can be on any of the pages.这意味着评论可以在任何页面上。 Now I cant seem to figure out how I can create a permalink for a particular comment.现在我似乎无法弄清楚如何为特定评论创建永久链接。 Any ideas?有任何想法吗?

I'm using PHP/mySQL with Codeigniter.我将 PHP/mySQL 与 Codeigniter 一起使用。

You can use temporary table to do that.您可以使用临时表来做到这一点。

Let's say you have article controller with show method:假设您有文章 controller 和 show 方法:

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

then the permalink might look like this:那么永久链接可能如下所示:

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

Then you grab all you need for your query:然后您获取查询所需的所有内容:

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM