Welcome Guest, Not a member yet? Register   Sign In
Creating Dynamic Pages
#1

[eluser]barisv[/eluser]
Hi,

I am dynamically creating links that will refer news are added by admin.

The news are added to the database as id, title and its content. And I am showing the list of news on any view by using related table on db. The news are shown like list of item by their title. For instance;


Code:
<a href="http://localhost/page/news/this-is-a-news/1">this is a news</a>
"news" is my controller and next segment the title of news and the last one is its id.

If I can get id of the news from url, I can dynamically create the page but I couldnt reach it because it says 404 page not found as I expect also. How can I design the system to solve this issue ?

I suppose that it waits for methods after
Code:
http://localhost/page/news/
but its not.

I know that I can reach url segment as below, but I didnt get how to use it for this problem.
Code:
$id = $this->uri->segment(5);


Thanks in advance.
#2

[eluser]ramm[/eluser]
I'm guessing you have a controller named "page.php" which contains a method called "news($id)".

If thisd is correct, you can try doing something like:
Code:
//within your page.php controller

function news($id)
{
    $id = $this->uri->segment(5);
    echo $id;
    die();
}

And then call your url http://localhost/page/news/something/xx

You should get "xx", if you don't, there something else wrong with your site configuration.
#3

[eluser]barisv[/eluser]
page is my root folder. news is my controller. How can I pass id to the controller from url ? Because after I click the link (http://localhost/page/news/this-is-a-news/1), it says "page not found". I doesnt run index function of news controller.
#4

[eluser]ramm[/eluser]
Because you don't have a method called "this-is-news" in your "news" controller. Remember CI by default will look for base_url/controller/method/parameters unless we change that.

What i did in a case like that, was this:
In the config/routes.php file
Code:
$route['news/:any'] = "news/article/$1";

And then in your controller
Code:
function article($id)
{
    $id = $this->uri->segment(5);
    $this->your_news_model->get_news($id);
    //Then display the result.
}

As you can see it here http://ellislab.com/codeigniter/user-gui...uting.html
#5

[eluser]InsiteFX[/eluser]
Correct way!

You can match literal values or you can use two wildcard types:

(:num) will match a segment containing only numbers.
(:any) will match a segment containing any character.

InsiteFX
#6

[eluser]barisv[/eluser]
This is exactly what I needed. It works now. Thank you very much.




Theme © iAndrew 2016 - Forum software by © MyBB