我在 Code Igniter 中有以下 URL:
哪里:
- 消息: Controller 名称
- id:用于从DB中获取相应消息的参数
这将呈现带有此 id 的新闻(也带有标题和正文等) 我需要显示(并且只显示)此别名的 url。
我该怎么做?
最佳答案
使用 PHP is_numeric 函数 http://php.net/manual/en/function.is-numeric.php
is_numeric — Finds whether a variable is a number or a numeric string
public function news($parameter)
{
$this->load->database();
// If the parameter is number, fetch data with id.
if ( is_numeric($parameter) ){
$news = $this->db->from('news')
->where('id', $id)
->get()->row_array();
}
// Otherwise, fetch data with title
else{
$news = $this->db->from('news')
->where('title', $title)
->get()->row_array();
}
// Render them
}
关于php - 在 CodeIgniter 中用别名值(文章标题)替换 URI 参数(文章 ID),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24231524/