Codeigniter网址:如何在网址中显示ID和文章标题 [英] Codeigniter URL: How to display id and article title in the URL
问题描述
请参阅以下链接结构
http://stackoverflow.com/questions/10672712/voting-system-like-stackoverflow
在上面的链接中, 10672712
是我猜猜的问题ID,因为如果你检查以下链接,你将得到与上述相同的位置:
In the above link the 10672712
is the question id I guess, because if you check the following link you will get to the same location as above:
http://stackoverflow.com/questions/10672712
现在如果你使用上面的链接,在浏览器的地址栏中,链接会自动在问题ID之后添加问题标题(作为slug),它就像上面的第一个链接一样。
Now if you use the above link then you will notice that in the browser's address bar the link automatically adds the question title (as slug) after the question id and it appears just like the first link above.
一个基于文章的网站使用Codeigniter。为了显示特定的文章,我有一个控制器,如下所示:
I am trying to create an article based website using Codeigniter. To display a particular article I have a controller which looks like following:
function articles(){
$id=$this->uri->segment(3);
$this->load->model('mod_articles');
$data['records']=$this->mod_articles->list_articles($id);
$this->load->view('view_article',$data);
}
我的模型:
function list_articles($id)
$this->db->select('*');
$this->db->from('cx_article');
$this->db->join('cx_author', 'cx_author.author_id = cx_article.author_id');
$this->db->where('article_id', $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{ return $query->row_array();
}
else {return NULL;}
}
如果你打这 - > localhost / my_base_url / my_controller / my_funtion / article_id
然后我的文章显示。现在我想实现的是如果有人击中 localhost / my_base_url / my_controller / my_funtion / article_id
我想自动添加文章标题为slug紧接在article_id后。像上面给出的例子)
If you hit this-> localhost/my_base_url/my_controller/my_funtion/article_id
then my article shows up. Now what I am trying to achieve is if someone hits localhost/my_base_url/my_controller/my_funtion/article_id
I want to automatically add the article title as slug right after the article_id.(Just like the example I have given above)
您能告诉我怎么做吗?
)
PS在我的数据库表中,我有一个名为 article_slug
的列, (例如:voting-system-like-stackoverflow)。
P.S In my DB table I have a column called article_slug
, in which I store my article title as slug(example: voting-system-like-stackoverflow ).
推荐答案
controllers / my_controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_controller extends CI_Controller {
function my_function($int_id = NULL, $str_slug = '') {
$row = $this->db->get_where('cx_article', array('article_id' => $int_id))->row();
if ($row and ! $str_slug) {
$this->load->helper('url');
$str_slug = url_title($row->title, 'dash', TRUE);
redirect("my_controller/my_function/{$int_id}/{$str_slug}");
}
// Run the rest of the code here
}
}
在数据库中没有一个slug列没有意义,因为你根据你的slug没有识别任何东西,唯一。
There is no point to have a slug column in your database since you don't identify anything based on your slug, nor is it unique.
这篇关于Codeigniter网址:如何在网址中显示ID和文章标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!