Welcome Guest, Not a member yet? Register   Sign In
Query Single Post
#1

[eluser]gainesms[/eluser]
Something I've been having some difficulty with. After following along the Codeigniter tutorial, I'd like to be able to select and view a single post.

Basically what I want to be able to do..

http://site_name/blog/post/id

The blog page would have all the posts listed. A 'Read More' link would direct the user to read the entire post's contents.

And if possible, to make things SEO friendly, rather than using the ID in the URL, is it possible to use the post's title?

Any help is appreciated.
#2

[eluser]jblack199[/eluser]
yeah you can use the posts title, as long as you want the entire title in the url.... your 'post' index would be something like this...

Code:
$title = $this->uri->segment('3');

$query = $this->db
->where('title', $title)
->get('table');

if ($query->num_rows() > 0) {
// do something
} else {
  // that doesnt exist anymore.. redirect back to main page or to hellokitty online or wherever you want..
}

pretty simple and straight forward.. most people generally do it using the ID because its the easiest to really do...
#3

[eluser]gainesms[/eluser]
Thanks!

So as far as the database query, how might I go about selecting and displaying a single post from the database ?
#4

[eluser]LuckyFella73[/eluser]
Quote:pretty simple and straight forward.. most people generally do it using the ID because its the easiest to really do…

To get a specific post you call the table by id because you want to get exactly
that post entry. If you have several posts with the same title you would get
multiple results from your db.
#5

[eluser]jblack199[/eluser]
as LuckyFella has stated you'd want to use the ID from the database because I overlooked the most obvious point of using the ID -- so thanks for clarifying that for him Lucky...

and use the same query as I've listed above.. then output it to the $data[] array, and call it quits from there...
#6

[eluser]boltsabre[/eluser]
i use this url structure for stuff like this

www.myDomain/forum/id/title

You cannot use just the title as you cannot have something like this (this is my title) in your url, it'll break! So you'll need to include the ID in there somewhere.

And to get the 'seo friendly' url you'll have to build a function that converts titles into urls, what you'll want it to do is to replace all none alpha / numeric characters (including white space) with a hypha/dash - ... but that can then lead to some interesting situations. Consider this title with the above methodology:

tim - the best ever

It would convert it to tim---the-best-ever (three dashes in a row)... what I do is after replacing with dashes I do a search for 5 dashes in a row, if I find it, replace it with one dash, then search for 4, then 3, then 2. This should cleanse your url nicely.

But what about this:

tim's awesum title. it's this!

-tim-s-awesum-title--it-s-this-

So you have to trim first to remove white space. I also just replace ' with nothing (don't replace ' with a -). So, if you do all the above mentioned you'll end up with this

tims-awesum-title-its-this

Which is perfect!

I know it's not what you asked, but I hope this helps you anyway!




Theme © iAndrew 2016 - Forum software by © MyBB