问题描述
||
我试图在使用分页和按相关性/投票/多数评论排序的页面上创建评论永久链接。这意味着评论可以在任何页面上。现在,我似乎无法弄清楚如何为特定评论创建永久链接。有任何想法吗?
我正在将PHP / mySQL与Codeigniter结合使用。
解决方法
您可以使用临时表执行此操作。
假设您有带有show方法的文章控制器:
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,$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\');
}