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
https://forums.phpfreaks.com/topic/172837-solved-squish-a-functions-call/
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);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.