Jump to content

[SOLVED] Ok I have searched


Kryllster

Recommended Posts

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

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 *** -->

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.