我正在尝试创建一个指向使用分页和按相关性/投票/大多数评论排序的页面上的评论的永久链接。这意味着评论可以出现在任何页面上。现在我似乎无法弄清楚如何为特定评论创建永久链接。有什么想法吗?
我将 PHP/mySQL 与 Codeigniter 结合使用。
最佳答案
您可以使用临时表来做到这一点。
假设您有一个带有 show 方法的文章 Controller :
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');
}
关于php - 使用分页在页面上创建评论的永久链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6302182/