CodeIgniter Forums

Full Version: A better way to handle params?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]davewilly[/eluser]
Hey guys, I'm finding it a bit of a messy affair dealing with multiple params on a site I'm rebuilding in CI.

I have a URL like http://www.site.com/stag-do/bristol/packages/1 and have been struggling to handle it in a clean manner without some ugly if/elseif. I had thought it would be a case of a nice switch statement. I've had to cascade if/elseif statement, with what I would consider the wrong logical flow to handle it.

My psuedo code is this:

if a package (ie. /1, /2 etc) param is set
---- Test it in a switch and display package details page
elseif packages segment isset and is equal to 'packages'
---- Show the packages page
elseif location segment is set, and location segment is equal to 'bristol' and packages segment is not set..
---- Show location page
else
---- 404

I would really appreciate if someone has a better solution. Perhaps this is more of a general PHP questions opposed to a CI specific question!

Here is code from my controller, the method I am refering to is bristol()

Code:
<?php
// Stag do controller
class stag_do extends Controller {

    /**
     * Remap
     * Calls method if it exists and passes any params, else 404's
     */
    function _remap($method)
    {
        // If method exists call it
        if(method_exists('stag_do', $method))
        {
            // Call method, and pass in URI segments
            $this->$method($this->uri->segments);
        }
        else // Method doesn't exist, call index
        {
            $this->not_found();
        }

    } // End _remap

    /**
     * Index
     */
    function index()
    {
        echo "Stag do";
    } // End index
    
    /**
     * Bristol
     */
    function bristol($segments)
    {
        // Full segment dump: Array ( [1] => stag-do [2] => bristol [3] => packages [4] => 1 )
        // Show package if package param exists
        if(isset($segments[4]))
        {
            // Show appropriate package
            switch($segments[4])
            {
                // Package 1
                case '1' :
                echo "Package 1";
                break;
                
                // Package 2
                case '2' :
                echo "Package 2";
                break;

                // Package 3
                case '3' :
                echo "Package 3";
                break;

                // Package 4
                case '4' :
                echo "Package 4";
                break;

                // Package 5
                case '5' :
                echo "Package 5";
                break;

                // Package 6
                case '6' :
                echo "Package 6";
                break;                
            
                // Invalid package param
                default :
                $this->not_found();
            
            }
        }
        // Show Packages
        elseif(isset($segments[3]) && $segments[3] == 'packages')
        {
            echo "Packages";
        }
        // Show Bristol
        elseif(isset($segments[2]) && $segments[2] == 'bristol' && !isset($segments[3]))
        {
            echo "Bristol";
        }
        // Not found
        else
        {
            $this->not_found();
        }
        
        
        
    } // End Bristol

    /**
     * Bournemouth
     */
    function bournemouth() {
    
        echo "bournemouth";

    } // End Bournemouth

    /**
     * Oxford
     */
    function oxford() {
    
        echo "Oxford";

    } // End Oxford

    /**
     * Oxford
     */
    function not_found() {
    
        echo "404";

    } // End Oxford        
    
} // End Stag do

/* End of file stag_do.php */
/* Location: ./system/application/controllers/stag_do.php */

Thanks,

Dave

El Forum

[eluser]John_Betong[/eluser]
I would be tempted to look at routing the url to specific controller.

Try this link:
 
URI Routing
 
 
 

El Forum

[eluser]davewilly[/eluser]
[quote author="John_Betong" date="1280509624"]I would be tempted to look at routing the url to specific controller.

Try this link:
 
URI Routing
 
 
 [/quote]

John, thanks. I'm playinmg with the routes now. I just hope I can do with having dozens upon dozens of $route[] rules.

Dave

El Forum

[eluser]mddd[/eluser]
If I get you correctly, there are basically 3 pages:
1- a page to show info about a city
2- a page to show a list of packages
3- a page to show info about a specific package.

I would make the urls as short as possible, like so:
/stag-do/bristol : show page type 1
/stag-do/bristol/packages : show page type 2
/stag-do/bristol/2 : show page type 3 (if you like this could also be /stag-do/bristol/packages/2 but why make it longer than neccessary)

You could do all the checking in your _remap function in the stag_do class:
Code:
function _remap()
{
  $city = $this->uri->segment(2);
  $package = $this->uri->segment(3);  // i'm using url like /stag-do/bristol/2 here

  // if $city=='index' then there was no city given
  if ($city=='index')
    $this->not_found();

  // if $package is not set, show the city info
  else if (!$package)
    $this->show_city($city);

  // package is set. is it 'packages'? then show the list
  else if ($package=='packages')
    $this->show_packages($city);

  // it's not 'packages'. display the package with this number from this city.
  else
    $this->show_package($city, $package);
}
Of course you need to check if the given city exists, and if a given package exists. But the idea is clear I hope?

El Forum

[eluser]davewilly[/eluser]
mddd, Thanks there, all very clear.
Appreciate it, I hadn't thought of reducing the URL by a segment, sounds a good idea.

atb,
Dave