Welcome Guest, Not a member yet? Register   Sign In
Beginner question
#1

[eluser]Christophe28[/eluser]
Hi,

I have a beginner question.

I'd like to create a uri like /photos/username/photo_id

So I have create a controller called Photos. But how do I have to call the function? The username is different every time ...

So far I have found how to get a segment from the uri to pass to the database ($this->uri->segment(1)Wink

I hope somebody has some time to help me Smile

Christophe
#2

[eluser]Clooner[/eluser]
Go into the routes.php file in your application/config dir

add this line!

Code:
$route['/photos/(:any)/(:num)'] = 'photos_controller/myphotofunc/$1,$2';

in your photos_controller create the function like
Code:
function myphotofun($username, $photo_id)
{
  // do whatever you want to with the username and photoid variables :P

}
#3

[eluser]Christophe28[/eluser]
Hi,

Thank you for your response.

I have added the following line in routes.php
Code:
$route['photos/(:any)/(:any)'] = 'photos/photofunction/$1,$2';

And added the following line in controller.php
Code:
function photofunction($username, $caption)
    {

        echo     $username;
        echo     $caption;
    }

But got the following warning when surfing to /photos/Christophe/mygreatphoto
Quote:A PHP Error was encountered

Severity: Warning

Message: Missing argument 2 for Photos::photofunction()

Filename: controllers/photos.php

Line Number: 13

Christophe,mygreatphoto

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: caption

Filename: controllers/photos.php

Line Number: 17

I have changed a bit here and there, like ID to caption. I think this is ok? What am I missing here?

Dank je voor je hulp Smile

Christophe
#4

[eluser]Clooner[/eluser]
[quote author="Christophe28" date="1277229863"]Hi,

Thank you for your response.

I have added the following line in routes.php
Code:
$route['photos/(:any)/(:any)'] = 'photos/photofunction/$1,$2';

And added the following line in controller.php
Code:
function photofunction($username, $caption)
    {

        echo     $username;
        echo     $caption;
    }

But got the following warning when surfing to /photos/Christophe/mygreatphoto
Quote:A PHP Error was encountered

Severity: Warning

Message: Missing argument 2 for Photos::photofunction()

Filename: controllers/photos.php

Line Number: 13

Christophe,mygreatphoto

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: caption

Filename: controllers/photos.php

Line Number: 17

I have changed a bit here and there, like ID to caption. I think this is ok? What am I missing here?

Dank je voor je hulp Smile

Christophe[/quote]

The error says it can't find the second argument of. try this
Code:
function photofunction($username='geen', $caption='leeg')
{
    echo     $username;
    echo     $caption;
}
it won't give errors this way and will ste the $username to default geen and $caption to default leeg

and I believe the routes should be like this:
Code:
$route['photos/(:any)/(:any)'] = 'photos/photofunction/$1/$2';
#5

[eluser]Christophe28[/eluser]
Yep, changed the routes and it works! Thx mate ;-)




Theme © iAndrew 2016 - Forum software by © MyBB