Kryllster Posted December 22, 2008 Share Posted December 22, 2008 for 2 + hours trying to find a solution to my problem. I have taken some code from the internet and I am trying to learn how to write and use functions. My code for the function is like this: <?php function pageTitle($arg) { switch ($arg) { case "main": $title = "Dark Mountain Home"; break; case "music": $title = "My Music"; break; } return $title; } ?> Should be simple I have a basic index.php controller page something like this: <?php include('templates/header_view.php'); include('templates/navigation_view.php'); // check for main if(!isset($_GET['show'])){ $show = "main"; } else{ $show = $_GET['show']; } switch($show) { case 'music': include('templates/music_view.php'); break; case 'main': default: include('templates/main_view.php'); break; } // End include('templates/footer_view.php'); ?> How would I use the function in this code I have tried pageTitle(); below the case switch and pageTitle("My Main Page"); as well. also tried including it in the template file. I am at a loss. Thanks for any help. Kryllster Quote Link to comment https://forums.phpfreaks.com/topic/137992-solved-ok-i-have-searched/ Share on other sites More sharing options...
Lamez Posted December 22, 2008 Share Posted December 22, 2008 <?php $title = "My Page"; echo '<html>'; echo pageTitle($title); echo '<body>Ya My Body!</body></html>'; function pageTitle($title){ $t = '<head><title>'.$title.'</title></head>'; return $t; } ?> returns like: <html><head><title>My Page</title><body>Ya My Body!</body></html> Something like that? Quote Link to comment https://forums.phpfreaks.com/topic/137992-solved-ok-i-have-searched/#findComment-721216 Share on other sites More sharing options...
Adam Posted December 22, 2008 Share Posted December 22, 2008 <?php include('templates/header_view.php'); include('templates/navigation_view.php'); function pageTitle($arg) { switch($arg) { case "main": $title = 'main'; include 'templates/main_view.php'; break; case "music": $title = 'music'; include 'templates/music_view.php'; break; } return $title; } // check for main if (!isset($_GET['show'])) { $show = "main"; } else { $show = $_GET['show']; } $pageTitle = pageTitle($show); // End include('templates/footer_view.php'); ?> Something like that? A Quote Link to comment https://forums.phpfreaks.com/topic/137992-solved-ok-i-have-searched/#findComment-721221 Share on other sites More sharing options...
Adam Posted December 22, 2008 Share Posted December 22, 2008 By the way you could condense: if (!isset($_GET['show'])) { $show = "main"; } else { $show = $_GET['show']; } Into: $show = (!isset($_GET['show'])) ? 'main' : $_GET['show']; Though you may want to secure your $_GET/POST vars in future! Quote Link to comment https://forums.phpfreaks.com/topic/137992-solved-ok-i-have-searched/#findComment-721227 Share on other sites More sharing options...
phpSensei Posted December 22, 2008 Share Posted December 22, 2008 You should include the function first, then the header_view and have the pageTitle inside the header_view... but since i have no idea what header_view.php actually is, you going to have to show me the script for it. Quote Link to comment https://forums.phpfreaks.com/topic/137992-solved-ok-i-have-searched/#findComment-721228 Share on other sites More sharing options...
Kryllster Posted December 22, 2008 Author Share Posted December 22, 2008 Ok have a lot to work with here thanks for the replies. And btw I wanted to tell Lamez I love ur signature because I was in real life actually attacked by and osterich lol it was not fun. The header_view.php is just a plain html page with the php extension and inside the <title><?php echo $title; ?></title> I am trying to do this with just 1 header I guess its called dynamic or something or other. Quote Link to comment https://forums.phpfreaks.com/topic/137992-solved-ok-i-have-searched/#findComment-721233 Share on other sites More sharing options...
Kryllster Posted December 22, 2008 Author Share Posted December 22, 2008 Ok is there a better to do this besides using a function. I am not able to get everything going from any of the suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/137992-solved-ok-i-have-searched/#findComment-721299 Share on other sites More sharing options...
Adam Posted December 22, 2008 Share Posted December 22, 2008 You're obviously trying to create a template system. There are many, many ways of doing this without the need for custom functions. It would seem you are just including specific files for the page, for example: templates/main_view.php Unless your system gets really robust - in which case you'd be better at looking a much more sophisticated template system, perhaps even using something like SMARTY - I'd try and keep it simple. Perhaps store the template info in an array or database? That way you can search the array/database for the $show val (i.e. music / main) and return some data such as the 'title' or 'src' of the template... A Quote Link to comment https://forums.phpfreaks.com/topic/137992-solved-ok-i-have-searched/#findComment-721315 Share on other sites More sharing options...
Kryllster Posted December 22, 2008 Author Share Posted December 22, 2008 your right I guess if I'm gonna be that complicated I should do that I have used smarty for years as a matter of fact my first script I made or cped were from there and I was thinking of using that but now I'm not sure. I'm working on an array now but I'm not sure about functions and I want to be able to use them just because. Right now I'm using CMSimple but I want to do a roll ur own tyoe thingy. Well if there are any more suggestions ? Id like to figure this out. Thanks all for the replies Kryllster Quote Link to comment https://forums.phpfreaks.com/topic/137992-solved-ok-i-have-searched/#findComment-721375 Share on other sites More sharing options...
Kryllster Posted December 22, 2008 Author Share Posted December 22, 2008 Ok another double post but is this how a normal php header looks like cause Im starting to think Im doing something wrong here. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title><?php echo $title; ?></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="icon" href="/favicon.ico"> <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"> <link rel="stylesheet" type="text/css" href="mystyle.css" media="screen"> <body> <br> <center><img src="images/dm_logo.jpg"></center> <table class="content" width="829px" cellpadding="3" cellspacing="3"> <tr colspan="2"> <!-- *** Navigation Starts Here *** --> Quote Link to comment https://forums.phpfreaks.com/topic/137992-solved-ok-i-have-searched/#findComment-721474 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.