Jump to content

[SOLVED] squish a functions call


saltedm8

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.