开发者

Codeigniter how to grab the original string from url_title()?

开发者 https://www.devze.com 2023-04-11 08:07 出处:网络
I\'m trying to learn how to use code ignitor but I\'ve run into a little problem. As with most people when they first use a framework, I too am making 开发者_如何学Pythona blog. I\'m trying to make my

I'm trying to learn how to use code ignitor but I've run into a little problem. As with most people when they first use a framework, I too am making 开发者_如何学Pythona blog. I'm trying to make my view links look like: http://localhost/blog/view/my-blog-post-title and I've gotten that far. But when I get to the actual view method is when I run into problems. Basically I'm my-blog-post-title refers to the 1st record of posts in my database. But the actual title looks like My Blog, Post Title.

So how do I get the id from my-blog-post-title when the original is My Blog, Post Title so I can pull that post from the database? Or should I just use numbers(I don't want to ;_;).


Well, I think the best solution and the easier approach would be to create a column in your posts table, something called "slug", which contains the url_title() output (the moment you create your article, you save that value in this db column as well as the other infos), and query against that instead of using this more complicated method.

So, you grab the last segment of the url, either via $this->uri->segment(3) or just by passing the whole uri to your controllers' method, and query against that column:

class Blog extends CI_Controller {

  public function view($slug)
  {
    $this->load->model('blog_model');
    $data['posts'] = $this->blog_model->search_slug($slug);
    $this->load->view('myview',$data);
  }
}

Model:

function search_slug($slug)
{
  $this->db->select('id,title')
           ->from('posts')
           ->where('slug',$slug);
  $query = $this->db->get();
  return $query->row();
}

View 'myview.php':

echo $posts->id;
echo $posts->title;


You should be able to use something like

$parts = explode("/",$_SERVER['REQUEST_URI']);
$title=$parts[(count($parts)-1)];

to turn your url into an array and then grab the title from the last section. string replace the "-" with " " and then do a %like& search in your db for the title. Not sure that's the best approach but should work.


This code can get you as far as extracting the title:

$url_string = "view/my-blog-post-title";

function getOriginal($url_string) {
    $url_parts = explode("/",$url_string);
    $url_title = $url_parts[1];
    $title_parts = array_map("ucfirst",
           explode("-",$url_title));
    return implode(" ",$title_parts);
}

echo getOriginal($url_string);

Which will output:

My Blog Post Title

The tricky part is where to insert the comma (,). This is tricky because blog post titles may be have more words like my-blog-post-title-some-other-words or my-blog-title-word-word-word. The comma can go anywhere.

If it is always constant that the comma is to be inserted after My Blog (My Blog is constant) then you just do an str_replace after calling getOriginai(..);

echo str_replace("My Blog","My Blog,","My Blog Post Title");
0

精彩评论

暂无评论...
验证码 换一张
取 消