Page 1 of 1

can some one explain...

Posted: Sat May 15, 2010 6:30 pm
by Torniquet
all this in a little detail

Code: Select all

$act = $_GET['act'];
$acts = array('create_cat', 'create_subcat');
$actions = array('create_cat' => 'Create Catagory', 'creat_subcat ' => 'Create Sub Catagory');

foreach($actions AS $url => $link){
	echo "<a href='admin.php?act=" . $url . "'>" . $link . "</a>";
}
obviously i know what is happening on the first 2 lines...

but can someone please explain whats happening with everything else?

i have seen the outcome, but i am confused about the '=>' and how $url and $link become values.

Thanks xxx

Re: can some one explain...

Posted: Sat May 15, 2010 7:06 pm
by KunoNoOni
Think of it like a function. when you pass a variable into a function it has one name and in the function it has a different name. for example

Code: Select all

b = 2;
c = 3;
a = addnumbers(2,3);
function addnumbers(d,e)
{
     return d+e;
}
I hope this makes sense. And I hope I explained it right :lol:

KunoNoOni

Re: can some one explain...

Posted: Sat May 15, 2010 8:09 pm
by Torniquet
KunoNoOni wrote:Think of it like a function. when you pass a variable into a function it has one name and in the function it has a different name. for example

Code: Select all

b = 2;
c = 3;
a = addnumbers(2,3);
function addnumbers(d,e)
{
     return d+e;
}
I hope this makes sense. And I hope I explained it right :lol:

KunoNoOni

sorry... but no that didnt help what so ever :(

thnx for tryin to explain though :D

Re: can some one explain...

Posted: Sat May 15, 2010 9:40 pm
by KunoNoOni
Ok let me try again. Think the terms of association. Hand is to glove as Head is to hat. There both things that you wear. so Create Category is to URL as Create Sub Category is to Link.


Does that make sense?


-KunoNoOni

Re: can some one explain...

Posted: Sat May 15, 2010 9:43 pm
by Jony
$url and $link do not become values, they are variables that contain values.

foreach loops the array $actions and assigns the values from 'create_cat' and 'Create Catagory' into $url and $link then prints, then does the same for 'create_subcat' and 'Create Subatagory'

inside the array $actions 'creat_cat' is the key and 'Creat Catagory' is the value contained in the key.

both 'create_cat' and 'Create Catagory' are constants.

That's how i understand this code. I may be wrong.

So,for this example, you will have 2 prints:

Create Catagory - > links to something(creat_cat)
Creat Subcatagory -> links to something(create_subcat)

where something may be a function from admin.php

Re: can some one explain...

Posted: Sat May 15, 2010 9:55 pm
by Torniquet
so in the $actions array... it is setting 'create_cat' as the key, then 'creat catagory' as the value of the key? by using the '=>' sign?

and then in the foreach.. its obviously taking each array value from $actions, and says that $url has a value of 'create_cat' and $link has a value of 'Create catagory' again by using the => sign? so it would be like syaing $url is the key and $link is the value of the key?

am i close with this now? lol

Re: can some one explain...

Posted: Sat May 15, 2010 10:01 pm
by Jony
Torniquet wrote:so in the $actions array... it is setting 'create_cat' as the key, then 'creat catagory' as the value of the key? by using the '=>' sign?

and then in the foreach.. its obviously taking each array value from $actions, and says that $url has a value of 'create_cat' and $link has a value of 'Create catagory' again by using the => sign? so it would be like syaing $url is the key and $link is the value of the key?

am i close with this now? lol
That's how i read it.
Obviously, i cannot know how the 'create_cat' value is worked with in admin.php.

Re: can some one explain...

Posted: Sun May 16, 2010 12:55 pm
by Jackolantern
Torniquet wrote:so in the $actions array... it is setting 'create_cat' as the key, then 'creat catagory' as the value of the key? by using the '=>' sign?

and then in the foreach.. its obviously taking each array value from $actions, and says that $url has a value of 'create_cat' and $link has a value of 'Create catagory' again by using the => sign? so it would be like syaing $url is the key and $link is the value of the key?

am i close with this now? lol
Yep, that is how it is working. The 3rd line is creating arbitrary value and key pairs, and then the foreach construct is giving them abritrary handlers to use within its block for iteration :)