Welcome Guest, Not a member yet? Register   Sign In
how to take the url to became a variable
#17

[eluser]jedd[/eluser]
[quote author="yudahebat" date="1238605909"]no. I need the segment(3) to get the ID of my tabel that I choose
to show on $data[/quote]

Okay - the important thing here that I think you are missing is that if you have a function like this:
Code:
function    foo  (  $bar  )  {
          }

Then $bar will be the same as segment(3)

The good thing about using the function ($variable) approach is that you can easily have multiple variables (and they take their value from your URL : http://example.com/Controller/foo/2/3/4/5

You could get each of those values by adjusting the code above to be:
Code:
function   foo  ($bar , $hugo , $bob , $nuggen)  {

In that example, using that URL, you'd find that $bar = 2, $hugo=3, $bob=4, $nuggen=5


The bad thing about using segment(x) is that a) it's harder to read and debug, and b) if you ever adjust your controller file layout - start using sub-directories for example - these numbers are suddenly wrong and you have to re-visit your code.


Now, the reason that you see people write this kind of code:
Code:
function  foo ( $bar = 0 )  {

Is so that IF the URL is just http://example.com/Controller/foo -- so, no trailing number segment -- then $bar will be set to 0, which is more convenient than being 'not set'. If there IS a trailing number on that URL, then it over-rides - and $bar takes on that value.

Does this make more sense?


Messages In This Thread
how to take the url to became a variable - by El Forum - 03-31-2009, 08:17 PM
how to take the url to became a variable - by El Forum - 03-31-2009, 08:20 PM
how to take the url to became a variable - by El Forum - 03-31-2009, 08:48 PM
how to take the url to became a variable - by El Forum - 03-31-2009, 09:30 PM
how to take the url to became a variable - by El Forum - 03-31-2009, 09:35 PM
how to take the url to became a variable - by El Forum - 03-31-2009, 09:56 PM
how to take the url to became a variable - by El Forum - 03-31-2009, 10:00 PM
how to take the url to became a variable - by El Forum - 03-31-2009, 10:04 PM
how to take the url to became a variable - by El Forum - 03-31-2009, 10:05 PM
how to take the url to became a variable - by El Forum - 03-31-2009, 10:09 PM
how to take the url to became a variable - by El Forum - 04-01-2009, 01:23 AM
how to take the url to became a variable - by El Forum - 04-01-2009, 06:11 AM
how to take the url to became a variable - by El Forum - 04-01-2009, 06:28 AM
how to take the url to became a variable - by El Forum - 04-01-2009, 06:36 AM
how to take the url to became a variable - by El Forum - 04-01-2009, 06:45 AM
how to take the url to became a variable - by El Forum - 04-01-2009, 06:55 AM
how to take the url to became a variable - by El Forum - 04-01-2009, 07:09 AM
how to take the url to became a variable - by El Forum - 04-01-2009, 07:14 AM
how to take the url to became a variable - by El Forum - 04-01-2009, 06:33 PM
how to take the url to became a variable - by El Forum - 04-01-2009, 06:40 PM
how to take the url to became a variable - by El Forum - 04-01-2009, 07:36 PM
how to take the url to became a variable - by El Forum - 04-01-2009, 08:25 PM



Theme © iAndrew 2016 - Forum software by © MyBB