Welcome Guest, Not a member yet? Register   Sign In
URI Segments
#1

[eluser]hykoh[/eluser]
Sorry, but I cant find any useful explaination at the forum / wiki about this topic.

I beginning with an example:
Code:
http://www.example.com/index.php?page=products&category=1&supplier=5

How can I handle such a link with the CI URI segment ?

If i try

Code:
example.com/index.php/products/category/1/supplier/5

i dont know how to access the values like in the first example ($_GET['category'], $_GET['supplier'] etc.)

I cant find out how much parameters given and i cant choose a specified segment thrue the name, right ?

So what else can i do ? The segments are dynamically doing their job as filters .. there could be even more then this two.

Thanks for help
#2

[eluser]wiZe[/eluser]
if you want to pass data via GET you have to enable query strings in the system/application/config/config.php. otherwise read the userguide article about how the urls in CI work: click here
#3

[eluser]xwero[/eluser]
You can get an associative array of the segments using the uri->uri_to_assoc(). The function has one parameter where you can define the offset if it isn't 3. so in your case
Code:
print_r($this->uri->uri_to_assoc(2));
// array('category'=>1,'supplier'=>5)
#4

[eluser]hykoh[/eluser]
ok works fine :-)

Thanks alot for this nice and fast support here Smile
#5

[eluser]Dready[/eluser]
Hello,

as you noticed, in CI your pass parameters just like URI segments (/some/thing) , instead of the classic varname=value&varname2=value2. The controller function that is called should have parameters for you to get back those variables. In your example, you have to create a controller, something like that :

Code:
class products extends controller {
  function products() {
    parent::Controller();
  }

  function show ( $category,$supplier ) {
    echo "Products category $category supplier $supplier";
  }

}

And then call it with the URL :

Code:
example.com/index.php/products/show/1/5

More info on controllers in the doc
#6

[eluser]xwero[/eluser]
Dready i think the way you build your url shouldn't be forced. If you like the key/value pair way why should you have to do it with fixed segments. The key/value pairs give you more flexibility.
#7

[eluser]hykoh[/eluser]
xwero offers me the best solution i wanted :-)

my solution now:

products_controller.php:
Code:
class Products extends Controller {
/* index etc. skipped */
  function show() {
    $filter = $this->uri->uri_to_assoc();
    
    $data['arrProducts'] = $this->produkte_model->showProducts($filter);
/* etc. ... */
}

product_model:

Code:
class Products_model extends Model {
function showProducts($filter = '') {
    $db = new pdo_helper(MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_HOST, 'mysql');
    $sql_filter = '';
    
    foreach($filter as $fKey => $fVal) {
      switch($fKey) {
        case 'cat':
        $sql_filter .= "AND produkte.kategorie_id = '".$fVal."'";
        break;

        case 'lieferant':
        $sql_filter .= "AND produkte.kategorie_id = '".$fVal."'";
        break;

        default:
        $sql_filter = '';
        break;
      }
    }
}

Works fine for me Smile But maybe there's something to optimise ?

Greets




Theme © iAndrew 2016 - Forum software by © MyBB