saltedm8 Posted September 2, 2009 Share Posted September 2, 2009 Hi I am pretty new to php and have hit an issue that I cant seem to solve on my own I want to create a menu, eventually the menu items will be called from the database which would have been sent there by user input ( hence the temporary variables ) I wanted to make the function I have below into a simpler form, instead of having to type in menu($hi, $count, $menu) every time I want my menu, all I want to have to do is call($menu); but I do need the variables to be set outside the function, as they are there so I can change the values once I do the forms etc ( eventually I want to make call($whatever) standard all over the sites I am making ) could you tell me how I can do that with this <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $count = 5; $hi = 'hello'; $menu = 'this'; function menu($hi, $count, $menu) { echo '<div id="'; echo $menu . '">' ."\n"; echo '<ul>'."\n"; for ($i=0;$i<$count;$i++){ echo '<li>' . $hi . '</li>'."\n"; } echo '</ul>' ."\n"; echo '</div>'; } menu($hi, $count, $menu) ?> </body> </html> thank you... p.s.. please don't get too complex, because as I said, I am only just learning and hope to be able to understand what you do so I can use it again with other functions.... thanks Link to comment https://forums.phpfreaks.com/topic/172837-solved-squish-a-functions-call/ Share on other sites More sharing options...
MatthewJ Posted September 2, 2009 Share Posted September 2, 2009 Something like this should work <?php $params = array('hi'=>'hello', 'menu'=>'this', 'count'=>5); function menu($params) { echo "<div id='".$params['menu']."'>\n"; echo "<ul>\n"; for ($i=0;$i<$params['count'];$i++) { echo "<li>".$params['hi']."</li>\n"; } echo "</ul>\n"; echo "</div>\n"; } menu($params); ?> Link to comment https://forums.phpfreaks.com/topic/172837-solved-squish-a-functions-call/#findComment-910941 Share on other sites More sharing options...
saltedm8 Posted September 2, 2009 Author Share Posted September 2, 2009 fantastic.. will work a treat, I suppose all I have to do when I change it is to use $menu = array('hi'=>$var1, 'menu'=>$var2, 'count'=>$var3); if that would work ? thanks again Link to comment https://forums.phpfreaks.com/topic/172837-solved-squish-a-functions-call/#findComment-910966 Share on other sites More sharing options...
MatthewJ Posted September 2, 2009 Share Posted September 2, 2009 It should Link to comment https://forums.phpfreaks.com/topic/172837-solved-squish-a-functions-call/#findComment-911035 Share on other sites More sharing options...
saltedm8 Posted September 3, 2009 Author Share Posted September 3, 2009 thanks.. fingers crossed lol Link to comment https://forums.phpfreaks.com/topic/172837-solved-squish-a-functions-call/#findComment-911460 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.