Welcome Guest, Not a member yet? Register   Sign In
URI Routing, best way to do that
#1

[eluser]daweb[/eluser]
I everybody,

I have to read from a DB some records and I have to build my dynamic query based on uri segments. (and some $_POST values...)

I have, for example:

http://www.my-site.com/buy

http://www.my-site.com/sell

http://www.my-site.com/buy/car

http://www.my-site.com/sell/motocycle/red

http://www.my-site.com/buy/dog/big/italy...g-name/123

I want to build the query in just one controller...

Is it the right way to make: ?

$route['buy/(:any)/(:any)/(:any)/(:any)/(:any)'] = 'home/$5';
$route['sell/(:any)/(:any)/(:any)/(:any)/(:any)'] = 'home/$5';

$route['buy/(:any)/(:any)/(:any)/(:any)'] = 'home/$4';
$route['sell/(:any)/(:any)/(:any)/(:any)'] = 'home/$4';

$route['buy/(:any)/(:any)/(:any)'] = 'home/$3';
$route['sell/(:any)/(:any)/(:any)'] = 'home/$3';

$route['buy/(:any)/(:any)'] = 'home/$2';
$route['sell/(:any)/(:any)'] = 'home/$2';

$route['buy/(:any)'] = 'home/$1';
$route['sell/(:any)'] = 'home/$1';

$route['buy'] = 'home';
$route['sell'] = 'home';

What's the best solution to make this?

I'm very sorry about my english...

Will be better tomorrow... :-)
#2

[eluser]xwero[/eluser]
I think you don't have to do any routing for this. You just check how many segments there are and take action based on that data
Code:
function buy()
{
   if($this->uri->total_segments() > 1)
   {
      $segments = $this->uri->segment_array();
      array_shift($segments);
      // do something with the segments
   }
   else
   {
      // default page
   }
}

function sell()
{
   // see buy
}
#3

[eluser]daweb[/eluser]
thank you xwero,

just a question...

if buy and sell are dynamic too, can I use your code in my default controller?

is the right way?

PS. This quesions 'cause I'm using Ocular library first time, and... it's difficoult for me.

Thank you so much...
#4

[eluser]xwero[/eluser]
If buy and sell are dynamic too you could do something like
Code:
function home()
{
   $segments = $this->uri->segment_array();
   $first =  array_shift($segments);
   switch($first)
   {
      case 'buy':
         if(count($segments) == 0)
         {
             // default buy page
         }
         else
         {
            // do something with the segments
         }
         break;
      case 'sell':
         // see buy
         break;
      default:
         // error or default page
         break;
   }
}
#5

[eluser]daweb[/eluser]
Thats'ok. I'll try.

Regards




Theme © iAndrew 2016 - Forum software by © MyBB