CodeIgniter Forums
Load different data depending on URL? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Load different data depending on URL? (/showthread.php?tid=35238)

Pages: 1 2 3


Load different data depending on URL? - El Forum - 10-23-2010

[eluser]invision[/eluser]
Hi,

I have a URL: http://www.site.com/index.php/scotland/glasgow/hotels/

I want to do different things depending on what part of the URL the user is visiting.
E.g. if they're on /index.php/scotland/ it'd show different data than when on the full URL above.

How do I achieve this with CodeIgniter?


On a previous thread, I got as far as the following...but it didn't work Sad
I tried to take in the three different parts of the URLs as parameters ($param_1 etc...)

Controller:
Code:
function index($param_1 = FALSE, $param_2 = FALSE, $param_3 = FALSE) {
    //check params to see which are set and act accordingly
    

    // retrieves country
    if (($param_1 !== FALSE) && ($param_2 === FALSE) && ($param_3 === FALSE)) {
      $data['entries'] = $this->Mains->get_entries($param_1);
      $data['title'] = 'country';    
      $data['main'] = 'public_page';
      $this->load->vars($data);
      $this->load->view('template');    
    }

    // retrieves city
    if (($param_1 !== FALSE) && ($param_2 !== FALSE) && ($param_3 == FALSE)) {
      $data['entries'] = $this->Mains->get_entries($param_1, $param_2);
      $data['title'] = 'city';    
      $data['main'] = 'public_page';
      $this->load->vars($data);
      $this->load->view('template');    
    }
    
    // retrieves category
    if (($param_1 != FALSE) && ($param_2 != FALSE) && ($param_3 != FALSE)) {
      $data['entries'] = $this->Mains->get_entries($param_1, $param_2, $param_3);
      $data['title'] = 'category';  
      $data['main'] = 'public_page';
      $this->load->vars($data);
      $this->load->view('template');  
    }      
    
    // no specific category chosen
    if (($param_1 == FALSE) && ($param_2 == FALSE) && ($param_3 == FALSE)) {
      $data['entries'] = $this->Mains->get_entries($param_1, $param_2, $param_3);
      $data['title'] = 'no specific category chosen';  
      $data['main'] = 'public_page';
      $this->load->vars($data);
      $this->load->view('template');  
    }
    
  }


-



-
For reference my Model is:
Code:
<?php
class Mains extends Model {
  function Mains() {
    parent::Model();
  }

  function get_entries($country = FALSE, $city = FALSE, $category = FALSE) {

    $this->db->select('entry.*, city.title as city_title, country.iso3 as country_title, category.title as category_title');
    $this->db->from('entry');
    $this->db->join('category', 'entry.category = category.id');
    $this->db->join('city', 'entry.city = city.id');
    $this->db->join('country', 'city.country = country.iso3');
    if($country !== FALSE) {
      $this->db->where('country.iso3', $country);
    }
    if($city!== FALSE) {
      $this->db->where('city.title', $city);
    }
    if($category!== FALSE) {
      $this->db->where('category.title', $category);
    }
    $result = $this->db->get();
    if($result->num_rows() > 0) {
      return $result->result_array();
    } else {
      return FALSE;
    }
  }
  
  function get_cities($country) {
    $this->db->from('city');
    $this->db->join('country', 'city,country = country.iso3');
    $this->db->where('country.iso3', $country);
    $result = $this->db->get();
  
    if($result->num_rows() > 0) {
      return $result->result_array();
    } else {
      return FALSE;
    }
  }
  
}  
?>


I would massively appreciate any guidance with this.



Many thanks.


Load different data depending on URL? - El Forum - 10-23-2010

[eluser]WanWizard[/eluser]
Use a different way of processing the parameters:
Code:
if ( $param_1 === FALSE )
{
    // no parameters given at all
}
else
{
    if ( $param_2 === FALSE )
    {
         // load full country information
    }
    else
    {
        // load limited country information

        if ( $param_3 === FALSE )
        {
            // load full city information
        }
        else
        {
            // load limited city information

            // load the category information
        }
    }
}



Load different data depending on URL? - El Forum - 10-23-2010

[eluser]invision[/eluser]
Aaaaaaah, now that sounds sensible Smile

I'll give it a try and report back post haste.


Load different data depending on URL? - El Forum - 10-23-2010

[eluser]invision[/eluser]
I've given this a shot, but it displays 'country' no matter what part of the URL I'm at Sad

I just need to show Country data if the URL is at: http://localhost/ae/index.php/gbr/
City data if the URL is at: http://localhost/ae/index.php/gbr/glasgow/
Category data if the URL is at: http://localhost/ae/index.php/gbr/glasgow/hotels/


Thanks again for any advice.


Load different data depending on URL? - El Forum - 10-23-2010

[eluser]invision[/eluser]
My Controller code:

Code:
<?php
class Main extends Controller {
  function Main(){
    parent::Controller();
    session_start();
  }
  
  function index($param_1 = FALSE, $param_2 = FALSE, $param_3 = FALSE) {
    //check params to see which are set and act accordingly
        
        if ( $param_1 === FALSE )
        {
            // no parameters given at all
            
        }
        else
        {
            if ( $param_2 === FALSE )
            {
                 // load full country information  
                  $data['entries'] = $this->Mains->get_entries($param_1);
                  $data['title'] = 'country';    
                  $data['main'] = 'public_news';
                  $this->load->vars($data);
                  $this->load->view('template');
            }
            else
            {
                // load limited country information
        
                if ( $param_3 === FALSE )
                {
                    // load full city information
                        $data['entries'] = $this->Mains->get_entries($param_1, $param_2);
                        $data['title'] = 'city';    
                        $data['main'] = 'public_page';
                        $this->load->vars($data);
                        $this->load->view('template');  
                }
                else
                {
                    // load limited city information
        
                    // load the category information
                          $data['entries'] = $this->Mains->get_entries($param_1, $param_2, $param_3);
                          $data['title'] = 'category';  
                          $data['main'] = 'public_page';
                          $this->load->vars($data);
                          $this->load->view('template');  
                }
            }
        }        
        
  }
    
}



Load different data depending on URL? - El Forum - 10-23-2010

[eluser]WanWizard[/eluser]
Ok, then I misunderstood the question. But indeed, if you leave out the 'limited' parts like in your last post, it should work the way you want.


Load different data depending on URL? - El Forum - 10-23-2010

[eluser]invision[/eluser]
Thanks for the reply.

It still doesn't seem to work when I use the code in my last post.

It still displays 'Country' even when I'm down at the 'City' or 'Category' part of the site.


Is there a better way of doing this that you know of?


Really appreciate the help.


Load different data depending on URL? - El Forum - 10-23-2010

[eluser]techgnome[/eluser]
Reverse the check... check for the third parameter FIRST... if it exists, display the info. If it doesn't, then check the second... if it exists, display the info... if it doesn't then check the first... and so on.

-tg


Load different data depending on URL? - El Forum - 10-23-2010

[eluser]invision[/eluser]
Thanks for the reply TG.

Can you explain what you mean using my code from post #4.

I've tried reversing it at my end, but it's not quite working.


Thanks again Smile


Load different data depending on URL? - El Forum - 10-23-2010

[eluser]techgnome[/eluser]
See if this works:
Code:
<?php

if ($param3 === FALSE)
{
    // If there is no Paramer 3... check #2
    if ($param2 === FALSE)
    {
        // If there is no Parameter 2... check #1
        if($param1 === FALSE)
        {
            // No parameters given
                        
        } else {
            // Load Country info
            $data['entries'] = $this->Mains->get_entries($param_1);
            $data['title'] = 'country';  
            $data['main'] = 'public_news';
            $this->load->vars($data);
            $this->load->view('template');
        }
    } else {
        // Load city information
        $data['entries'] = $this->Mains->get_entries($param_1, $param_2);
        $data['title'] = 'city';  
        $data['main'] = 'public_page';
        $this->load->vars($data);
        $this->load->view('template');
    }
} else {
    // Load Category information
    $data['entries'] = $this->Mains->get_entries($param_1, $param_2, $param_3);
    $data['title'] = 'category';  
    $data['main'] = 'public_page';
    $this->load->vars($data);
    $this->load->view('template');
}

-tg