0

I am developing an application using CodeIgniter. In this case I would like to get URL like www.something.com/book/bookname while I am clicking on an anchor tag of View.I would not like to get URL like www.something.com/book/bookname/12. On the other hand I have to pass book id from View to Controller.

Could anyone say how can I do that ??

Thanks

@Allendar

I failed to express.Sorry for that.Lets have a scenerio. I have a list of books in my View. Like below

Sl no Name of book Price

  1. php book $10
  2. MySql book $15
  3. html book $20
  4. css book $12

Now I would like to see details of a book. In this case I hiperlinked the book name, where the book id is inside a anchor tag. If I clicked on a book name I'll get a URL like www.something.com/book/get_book/12. Where book is the name of Controller, get_book is a function inside the Controller and 12 is the book id. Then I can get details of a book using the id 12 from database.This is the common secenerio in almost all applications.

But I need a special URL, that is www.something.com/book/get_book/php book. But I would like to get the id of the book in Controller to find out the book in database(because id of the book is UNIQUE,but name of the book is not UNIQUE).Even I would not like to get a URL like www.something.com/book/get_book/php book/12.

I think now it is clear to you.

Thanks

7
  • So you have a Book on a certain page, and then want to build an URL based on that Book's ID? Your question is a bit vague. Commented Jan 1, 2014 at 9:15
  • In this case, you have to pass the book id as POST instead of GET. This way, it will not appear in the url. Commented Jan 1, 2014 at 9:15
  • You can pass book id through hidden value or query string. Commented Jan 1, 2014 at 9:17
  • @Allendar I have a Book on a certain page, and then want to build an URL based on that Book's NAME.In this way I can collect the book name from URL in Controller. But I would like to collect ID of the book in Controller. Thanks Commented Jan 1, 2014 at 9:33
  • 1
    Oh so you want a pretty URL. You can do that, but that URL has to be generated when the Book is created in the database. Also you need to put an UNIQUE mark on that slug-field so it can never conflict. Commented Jan 1, 2014 at 9:35

2 Answers 2

1

If you want a pretty URL you can use slugs. A few simple steps would be, when creating a new Book in your database,

1.) PHP (Generate Slugs for the new book)

$book = new StdClass;
$book->name = $some_post_value['name'];
$book->slug = preg_replace('/[^a-zA-Z0-9_]/', '_', $book->name); // You should decide your own rules and check if there are no illegal URL-characters in your book-name

2.) SQL (Check if the slug is already used)

sprintf('SELECT * FROM books WHERE slug = "%s"', $book->slug);

If the result of the above query is 1, then it means the slug is already taken. You either need to add or change something in your slug and check with the same query again until the slug is absolutely unique.

When the slug is finally unique you can just insert the Book-data into your database and use your controller like so to eventually retrieve your Book by it's slug (name):

class Book extends FrontendController {

    public function bookname($slug = FALSE) {
        if ($slug) {
            // Assuming DataMapper here, for easy examples
            $book = new Book();
            $book->get_where(array('slug' => $slug));

            if ($book->exists()) {
                // Process and load your view with the Book-data
            }
            else{
                // Book not found by Slug
            }
        }
    }

}

Using UNIQUE on the slug-field in your database will spare you a lot of headaches in the future, as you might have conflicting slug-names in the table otherwise.

An URL would look like; http://www.yourwebsite.com/book/bookname/The_Lord_of_the_Rings_Return_of_the_King

Sign up to request clarification or add additional context in comments.

1 Comment

You can use CI uri helper url_title() to convert title to slugs. Read here
0

You need a "slug" column in your database table so you can match on that but also. But because it's not unique, you'll also need the ID (as you correctly pointed out).

so set the URL to:

book/get_book/php-book/12

and pass both arguments to the controller method

get_book($slug, $id)

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.