Welcome Guest, Not a member yet? Register   Sign In
How to dynamically react on different parameter counts?
#1

[eluser]Comanche[/eluser]
Hi there,

I'm currently building an index page and got some trouble.

I want to realize the following things:

1. Show all items and paginate them
2. Show just items starting with a specific letter and paginate them
3. Sort items by id or name

So I need three parameters, one for the letter, one for the pagination and one for the sort order.

The problem is, that when showing all items, at least the parameter for the letter isn't needed.

Let's say I have the following function:

function test($letter='',$page=0,$sort=0)

When calling the whole thing with .../index.php/project/test/A/0/0 there is no problem.
But when calling it with .../index.php/project/test/0/0 to see all items it will take the first '0' as letter. Since I also need numbers for $letter I can't just check, if a number was given for $letter.

I think I could check the number of parameters given, but there is another problem:
Why should I send parameters to the function if just the default values are needed?

For example .../index.php/project/test/A/0/1 wouldn't make sense, since 0 is the default value for the two other parameters. So I the best would be just to call .../index.php/project/test/A/1.
But then I can't figure out, if the value 1 is the sort order or the page.

With "normal" PHP it wouldn't be a big problem I think, I could just call
test.php?letter=X&page=1&sort=1, and handle the different parameters via $_GET but with CodeIgniter I don't see a possibility to differ between the given parameters (when trying it "test/A/0/1"-style and not "test.php?letter=X&page=1&sort=1"-style).

I could even write different functions for that (index($page,$sort), indexl($letter,$page,$sort)) but even then the problem to differ between $page and $sort would remain. And I want to do this all in just one function, if it's possible.

If one of you has an idea how to solve this problem, I'd be very thankful.

Greeting,
Comanche
#2

[eluser]Comanche[/eluser]
Ok,

after thinking about it a lot of time I chose to do it the following way:

I just take one parameter which describes which letter was chosen and what the sort order should be and a second parameter which holds current page number.

Letters are enumerated from 1 (A) to 26 (Z) and from 27 (0) to 36 (9), 0 means, that no specific letter was chosen.
To change the sort order I add (or subtract) 100 to (from) this value.

For example "25" means "show entries starting with Y, order ascending" and "125" means "show entries starting with Y, order descending". "0" means "show all entries, order ascending", "100" means "show all entries, order descending".
#3

[eluser]sophistry[/eluser]
hi comanche,

this seems very cumbersome. whenever i find myself doing somersaults to get somewhere that i should be able to walk to i go back to the cogent CI manual:

http://ellislab.com/codeigniter/user-gui...s/uri.html

there you will find the uri class method uri_to_assoc() which lets you name your parameters in the URI itself. this seems like a better way to structure it. especially if you have to hand off this project one day!
#4

[eluser]Comanche[/eluser]
Hi Sophistry,

thank you very much for your reply, I think this could be the function I need, I'll check it out right now.

Thanks a lot
#5

[eluser]Comanche[/eluser]
It's me again,

I just want to confirm, that "uri_to_assoc" is exactly what I needed. I rewrote the whole code within just two hours while I needed about eight hours to write the old code.
And it's much more structured now and easier to read.

Thanks a lot for your help Smile
#6

[eluser]sophistry[/eluser]
sure! that's the special thing about codeigniter... not only does it help you write better programs, it makes you a better programmer.




Theme © iAndrew 2016 - Forum software by © MyBB