Goldeneye Posted September 7, 2008 Share Posted September 7, 2008 I'll try and make this as clear as possible.. I'm not actually stuck, I just want some opinions/advice about how to do/improve this. I've got a function that generates an unordered list from a 3-Dimensional array. There are some links where I have to include $_GET parameters (anywhere between 1 and 5 of them). So I'm wondering how you could use them without actually passing them through the function as a parameter. With the way I have it set up right now, I have to pass any $_GET variables I might use through the function as a parameter. I could keep it this way, but I find it's tedious. So, say you wanted to embed a specific array of links onto foo.php, I'd use: <?php navigation('foo.php', 1, $_GET['x1'], $_GET['x2'], $_GET['x3'], $_GET['x4']) // 'foo.php' is the name of the page and also tells the function to use $sub[$s]['foo.php'] // 1 tells the function to use $sub[$s]['foo.php'][1] // The last four parameters are for passing $_GET values through the function. ?> So with those parameters, it'll use this array (assuming you're logged in): <?php $s = $loggedin ? 1 : 0; //if the user is logged in use $sub[1] or else use $sub[0] $sub[1]['foo.php'][1] = array( 'foo.php' => 'Unread', 'foo.php?user='.$get.'&par='.$get2.'&obj='.$get3 => 'Read', 'foo.php?node=true' => 'Foo', 'foo.php?node=false' => 'Bar', 'add.php?area='.$get4 => 'Add' ); ?> <?php function navigation($page, $mode, $get='', $get2='', $get3='', $get4='', $get5='', $subnav=true){ //First, define arrays for the Sub Navigation //$sub[0] They are not logged in //$sub[1] They are logged in $sub = array(); $sub[1] = array(); $sub[1]['foo.php'] = array(); $sub[1]['foo.php'][0] = array( 'foo.php' => 'Unread', 'foo.php?user='.$get.'&par='.$get2.'&obj='.$get3 => 'Read', 'foo.php?node=true' => 'Foo', 'foo.php?node=false' => 'Bar' ); $sub[1]['foo.php'][1] = array( 'foo.php' => 'Unread', 'foo.php?user='.$get.'&par='.$get2.'&obj='.$get3 => 'Read', 'foo.php?node=true' => 'Foo', 'foo.php?node=false' => 'Bar', 'add.php?area='.$get4 => 'Add' ); $sub[1]['bar.php'] = array(); $sub[1]['bar.php'][0] = array( 'x.php' => 'Area X', 'y.php?par='.$get => 'Y Values', 'z.php?param='.$get => 'Z Functions' ); $sub[1]['bar.php'][1] = array( 'x.php' => 'Area X', 'y.php?par='.$get => 'Y Values', 'z.php?param='.$get => 'Z Functions', 'o.php?x='.$get.'&y='.$get2.'&z='.$get3 => 'Options' ); $sub[1]['bar.php'][2] = array( 'x.php' => 'Area X', 'y.php?par='.$get => 'Y Values', 'z.php?param='.$get => 'Z Functions', 'edit.php?x='.$get.'&arr='.$get2 => 'Edit Arguments' ); //We have defined the sub-navigation arrays. //Let's use a foreach loop to loop through them and generate an unordered list $s = $loggedin ? 1 : 0; //if the user is logged in use $sub[1] or else use $sub[0] if($subnav==true){ echo '<ul class="subnavul">'; foreach($sub[$s][$page][$mode] as $key => $val) echo '<li class="subnavli"><a href="' . $key . '">' . $val . '</a></li>'; echo '</ul>'; } } ?> Could this function be improved? I'd like to hear how, but I'd also like to know how set up my $_GET variables so I don't have to pass them through the function as parameters. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted September 7, 2008 Share Posted September 7, 2008 a simple solution: <?php function navigation($page, $mode, $get, $subnav=true){ $get1 = $get['x1']; $get2 = $get['x2']; $get3 = $get['x3']; $get4 = $get['x4']; //First, define arrays for the Sub Navigation //$sub[0] They are not logged in //$sub[1] They are logged in $sub = array(); $sub[1] = array(); $sub[1]['foo.php'] = array(); $sub[1]['foo.php'][0] = array( 'foo.php' => 'Unread', 'foo.php?user='.$get[0].'&par='.$get2.'&obj='.$get3 => 'Read', 'foo.php?node=true' => 'Foo', 'foo.php?node=false' => 'Bar' ); $sub[1]['foo.php'][1] = array( 'foo.php' => 'Unread', 'foo.php?user='.$get.'&par='.$get2.'&obj='.$get3 => 'Read', 'foo.php?node=true' => 'Foo', 'foo.php?node=false' => 'Bar', 'add.php?area='.$get4 => 'Add' ); $sub[1]['bar.php'] = array(); $sub[1]['bar.php'][0] = array( 'x.php' => 'Area X', 'y.php?par='.$get => 'Y Values', 'z.php?param='.$get => 'Z Functions' ); $sub[1]['bar.php'][1] = array( 'x.php' => 'Area X', 'y.php?par='.$get => 'Y Values', 'z.php?param='.$get => 'Z Functions', 'o.php?x='.$get.'&y='.$get2.'&z='.$get3 => 'Options' ); $sub[1]['bar.php'][2] = array( 'x.php' => 'Area X', 'y.php?par='.$get => 'Y Values', 'z.php?param='.$get => 'Z Functions', 'edit.php?x='.$get.'&arr='.$get2 => 'Edit Arguments' ); //We have defined the sub-navigation arrays. //Let's use a foreach loop to loop through them and generate an unordered list $s = $loggedin ? 1 : 0; //if the user is logged in use $sub[1] or else use $sub[0] if($subnav==true){ echo '<ul class="subnavul">'; foreach($sub[$s][$page][$mode] as $key => $val) echo '<li class="subnavli"><a href="' . $key . '">' . $val . '</a></li>'; echo '</ul>'; } } ?> now call it this way: <?php navigation('foo.php', 1, $_GET); // 'foo.php' is the name of the page and also tells the function to use $sub[$s]['foo.php'] // 1 tells the function to use $sub[$s]['foo.php'][1] // The last four parameters are for passing $_GET values through the function. ?> Just FYI: you can pass arrays to a function. Quote Link to comment Share on other sites More sharing options...
Goldeneye Posted September 7, 2008 Author Share Posted September 7, 2008 Ahhh this really does make things simpler. Thanks a lot, I always forget about how useful an array can be. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.