Welcome Guest, Not a member yet? Register   Sign In
Howto change site uri segment based on database input?
#1

[eluser]bronco[/eluser]
Hi there,

I'm new to CI and have a question about creating a view in runtime named after data from a database... like on a blog.

example scenario: user clicks on a link -> a view should be filled with database input. the site uri should change to domain/site/entry-from-database

how do I implement that? my first thought was to use a function in the corresponding controller to load the data from the model to the view. but how can i affect the last site uri segment to be named after of the fields from the model and not the controller function?

I checked the documentation and couldn't figure out how to do this, the search on the forums didn't bring up matching results for my problem.

Thanks in advance.
#2

[eluser]ddutra[/eluser]
Bronco, hi.

Lets say you have a domain called www_broncoblog_com, and you want the user to access stuff from user profiles, right, but you want the URL to contain the User's name?

I would simply create a controller named user, and a function named profile($login), so profile receives login as a parameter and look on the database, serving the profile info, like name, address, and werever you need.

www_broncoblog_com/user/profile/bronco -> This will serve bronco's profile
www_broncoblog_com/user/profile/ddutra -> This will serve ddutra's profile

Code:
Controller {
function profile($login) {
query database;
serve data loading view;
}
}

Notice that in this sample you have 3 levels after the baseURL, one being the controller name, the other being the function name and the rest are the parameters.

It would help more if you said what exactly is "entry-from-database". If you want to display human readeble data on the url (like LOGIN, Client Name, Suppliers name, Article Name) but only have the code to work it on links i would suggest a redirect to the same function, since as far as i know you cant rewrite the URL with CI.

Lets say "entry-from-database" is login, but you only have the login ID's to build the links, i would do something like this:

Your link: www_broncoblog_com/user/profile/1 -> This is bronco profile, but due to google indexing or user experience, i dont want the URL on the browser to be displaying a code "1", i want it to show the user login name, makes more sense... well no problem:

Code:
controller {
    function profile($login) {
        //First lets check if we are receiving a ID or a login name
        if (isset && is_numeric($login)) {
            //Login is set and is numeric, this will require a redirct using CI resources
            
            //First lets grab the login name from the db, i will assumo you know how to do that
            $login_name = $queryDB;
            redirect(base_url().'/user/profile/'.$login_name, 'location', 301);
            exit;
        }
        else {
            //The url is showing a Login Name, so now we can serve the data, and maybe hope google indexes it right :P
            $this->Load view.
        }
    }
}


All links have been modified due to this forum rules, wich are a lame way of dealing with spammers IMO.

Hope i got your question right, and hope my anwser is right too. I am also new to CI.

Thanks and best of luck.
#3

[eluser]bronco[/eluser]
hey ddutra,

this looks very promising, I'll give it a try on the weekend. And yes, you've got my question right =)

thanks
#4

[eluser]ddutra[/eluser]
Another thing, if you are considering SEO optimization i would recomment to have all your link parameter to be human readeble data. Try not to use ids. Article names for instance, must be unique, just pass it tru a function that removes all spaces and replace with underscore, remove special chars, etc.

site_com/article/codeigniter_is_nice
site_com/product/shampoo_etc


You must find something relevant and unique to key your data, besites the ID of course.


Best of luck.
#5

[eluser]bronco[/eluser]
Hi,

I made it work so far with the hints supplied earlier (with IDs). My only problem left is that I don't know how to resolve the readable links to my database data.

What my table looks like:
ID, Name, etc...

What my link looks like:
domain/page/detail/Name

Usually it would be pretty easy to resolve the name to the corresponding ID, but "Name" is not always one word or free from special characters so that I needed to encode the name for the link (in the view) with the "url_title" function from CI. But how can I resolve these encoded names back for my query to work (in the controller/model)?

Unfortunately, "redirect" is not the solution for me, because all views are loaded via AJAX and I don't see a way to use it here.

Is it possible to deliver a second parameter (the ID for example) to the controller to make it easier to look up the data without revealing it to the user?




Theme © iAndrew 2016 - Forum software by © MyBB