geektasic Posted March 29, 2009 Share Posted March 29, 2009 Hello all! I started learning PHP today (it's awesome) but already I'm stuck! I'm trying to save time in my code by using functions. Here's the code <!-- Link one --> <? function link_one(){ echo " <div id=\"container\"> <!-- Navigation --> <div id=\"navigation\"> <ul> <!-- Link one --> <li><a href=\"index.php?page=link_one\">Link 1</a></li> <!-- Link two --> <li><a href=\"index.php?page=link_two\">Link 2</a></li> <!-- Link three --> <li><a href=\"index.php?page=link_three\">Link 3</a></li> <!-- Link four --> <li><a href=\"index.php?page=link_four\">Link 4</a></li> <!-- Link five --> <li><a href=\"index.php?page=link_five\">Link 5</a></li> <!-- Link six --> <li><a href=\"index.php?page=link_six\">Link 6</a></li> </ul> </div> <div id=\"body\">Link one content</div> <!--End container --> </div> "; }?> <!-- Link two --> <? function link_two(){ echo " <div id=\"container\"> <!-- Navigation --> <div id=\"navigation\"> <ul> <!-- Link one --> <li><a href=\"index.php?page=link_one\">Link 1</a></li> <!-- Link two --> <li><a href=\"index.php?page=link_two\">Link 2</a></li> <li><a href=\"index.php?page=sub_link_one\">Sub link one</a></li> <li><a href=\"index.php?page=sub_link_two\">Sub link two</a></li> <li><a href=\"index.php?page=sub_link_three\">Sub link three</a></li> <li><a href=\"index.php?page=sbu_link_four\">Sub link four</a></li> <!-- Link three --> <li><a href=\"index.php?page=link_three\">Link 3</a></li> <!-- Link four --> <li><a href=\"index.php?page=link_four\">Link 4</a></li> <!-- Link five --> <li><a href=\"index.php?page=link_five\">Link 5</a></li> <!-- Link six --> <li><a href=\"index.php?page=link_six\">Link 6</a></li> </ul> </div> <div id=\"body\">Link two content</div> <!--End container --> </div> "; }?> <!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>My first PHP function</title> </head> <html> <body> <? $page = $_GET['page']; switch($page) { //Link one case "link_one": $content = link_one(); break; //Link two case "link_two": $content = link_two(); break; default: $content = link_one(); break; } //Where my content loads include($content); ?> </body> </html> Link one: Loads basic navigation and body content. Link two: Loads navigation with submenu and different content. You get the idea! As you can see, this function is a waste of time because I'm repeating the code with slight variation. What's the best way to to load my content using PHP function and switch? I don't want to use lots of includes and would prefer if all my data is in one php file. Is this possible? Thanks! Link to comment https://forums.phpfreaks.com/topic/151669-using-php-function-with-switch/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.