Forum Moderators: coopster

Message Too Old, No Replies

Pulling a variable to a function, then putputting values?

         

doodlebee

4:41 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



I know this sounds weird.

What I've done is set up a PHP site that uses includes - it's a static site so I'm trying to make it as easy as possible to edit.

My setup is that I have an index.html page at the root, and a functions.php page at the root. I have several functions that do what needs to be done, including pulling the header, footer, and sidebar from the "includes" folder. It's working great :)

However, I had forgotten about the meta stuff - title and description - so now I need to write a function that will make the changes I need. I have the function working as a general rule, but rather than use a ton of switch() stuff, I wanted to set a variable at the top of all my pages for $title, $keywords and $description, so when the client goes in to edit the title, keywords and description for each page, they can do it right there within the file they're going to edit - then the function will gran the variable from the page, and output the content in the header.html file.

My function looks like so:


function metadata($type) {
global $keywords;
$page = test_page('class');
$hometext = 'Home Page Title';
if($page == '') { // this means we're on the "Home" page
if($type == 'title')
echo $hometext;
elseif($type == 'description')
echo 'description here';
elseif($type == 'keywords')
echo 'keyword list here';

} else { // if we're not on the home page...
if($type == 'title')
echo ' » ' . $hometext;
elseif($type == 'description')
echo $page;
elseif ($type == 'keywords')
echo $keywords;
}
}


Most of that stuff is just test stuff to see if the words put in show up. the Home stuff works fine. But I'd rather not put in a switch or "if/then" conditional for every single page - I'd rather just have it look at what page it's on (the $page is another function I've written that will get the name of the page we're on - so I can grab that no problem) and see the $title, $keyword and $description variables I've put in the very top of the page (after the call to the header - should it be before?), like so (this is just an example of one of the inside/subpages):


<?php include('../loader.php');
get_include('header');
$keywords = 'inside keyword'; ?>

<div id="main" class="clear">
<h1>Vision, Mission</h1>

<h3>Vision Page Test</h3>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin rhoncus, massa tincidunt molestie blandit, velit urna porta enim, tempor mollis metus ipsum sed mi. Quisque luctus tristique turpis. Sed vel sem dignissim orci rutrum vestibulum. Proin pretium molestie est id blandit. Duis posuere congue quam. Duis elementum nulla a massa interdum facilisis. Cras id libero quis elit blandit blandit. Praesent eros est, viverra nec eleifend sed, cursus at dui. Curabitur eu magna eu lectus imperdiet vehicula. Nulla velit leo, consectetur eget rutrum ut, dapibus quis nunc. Mauris ligula nibh, lacinia non vehicula id, luctus volutpat erat. Nulla sed tortor dolor, ut ullamcorper elit. Integer eros leo, rhoncus id pretium vitae, tempus a eros. Vivamus nec eleifend mi. Nam augue enim, iaculis laoreet dictum quis, hendrerit in nisi. Sed risus metus, varius nec varius in, laoreet id risus. Suspendisse potenti. Pellentesque ornare quam orci. Aliquam rutrum dapibus varius. Mauris ac consectetur massa.</p>

<p>Nunc quis mauris eleifend orci tempor tincidunt. Etiam id lorem ut felis scelerisque ornare a ut mauris. Duis molestie odio sed nulla eleifend dignissim. Suspendisse consectetur adipiscing elit.</p>
<!--/left-->
</div>

<?php get_include('sidebar');
get_include('footer'); ?>


I thought globalizing the $keywords variable would work, but it's coming through empty. Would anyone know what I'm doing wrong?

rocknbil

5:46 pm on Apr 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



when the client goes in to edit the title, keywords and description for each page, they can do it right there within the file they're going to edit.


I think you may be making more of it than it needs to be. :-) First a little tangent about functions and why yours probably is not doing what you expect: you're doing echo, and what you want is return. Send a function parameters, it returns values, or a list of values. Example,

list ($total,$even_odd) = calc(3,3,'multiply');
echo "$total, $even_odd";
// Should echo 9, odd if I have no errors. :-)
//

function calc($num1,$num2,$operand) {
switch $operand {
case add:
$tot=$num1+$num2;
break;
case subtract:
$tot=$num1-$num2;
break;
case multiply:
$tot=$num1*$num2;
break;
case divide:
// No division by zero
$tot=($num2 > 0)?$tot=$num1/$num2:'INVALID DIVISOR';
break;
default:
$tot='INVALID OPERAND';
}
$mod = ($num%$num2 > 0)?'odd','even';
return Array($tot,$mod);
}


Though you've declared $keywords as a global within the function, I **think** you'd also need to declare it outside. But anyway, I don't think you need a function.

I don't see why you can't do something like this, without a function. In each page you have

<?php
$title="page title"; // Make them all unique, same for meta descr.
$meta_desc="My meta description";
$meta_kwd="foo, bar, this, that, the other thing";
include($_SERVER['DOCUMENT_ROOT'] . "/header.php");
?>

Then in header.php, using full valid html instead of my stock example,

<?php
// just some defaults
if (! isset($title)) { $title = 'Default Page'; }
if (! isset($meta_desc)) { $meta_desc = 'Some default description'; }
if (! isset($meta_kwd)) { $meta_kwd = 'Some default keywords'; }
?>
<html>
<head>
<title><?php echo $title;?></title>
<meta name="description" content="<?php echo $meta_desc;?>">
<meta name="keywords" content="<?php echo $meta_kwd;?>">
</head>
<body>
<!-- etc -->

Unless I'm missing something . . . no function required.

doodlebee

5:50 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



Got it! Maybe this'll help someone else in the future :)

So, I changed my header to include a globalized statement. This appears just after the opening
<head>
tag in my header.html include:

 <?php global $thistitle, $thisdesc, $thiskey; ?> 


the title, description and keywords tags now say the following:


<title><?php echo metadata('title', $text = $thistitle); ?></title>
<meta name="description" content="<?php echo metadata('description', $text = $thisdesc); ?>" />
<meta name="keywords" content="<?php echo metadata('keywords', $text = $thiskey); ?>" />



and my function looks like so:


function metadata($type, $text='') {
global $thistext;
$page = test_page('class');

$default_title = 'Catalysis: Foundation for Health';
$default_description = 'Default Description';
$default_keywords = 'Default Keywords';

if($type == 'title') {
if($text == '') $text = $default_title;
else $text = $text;
} elseif($type == 'description') {
if($text == '') $text = $default_description;
else $text = $text;
} elseif($type == 'keywords') {
if($text == '') $text = $default_keywords;
else $text = $text;
}
return $text;
}


then on each page on the site, I simply have this at the very tippy-top - just before the call to the header.html include:


// edit meta stuff here
$thistitle = 'About';
$thisdesc = 'About page description here';
$thiskey = 'About keywords here';
// end editing


now all they have to do is edit the keywords, etc for each page for those variables, and it's all set!

Thanks!

doodlebee

6:16 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



ha! rocknbil - thank you - you must haveposted at the same time I did!

I need the function because I'm doing some other stuff. but you're right, I don't particularly need a function for this *specific* case. I wrote one anyway to set a default - but the individual stuff on each page overrides the default set in the function.

So it looks like you and I came to the same conclusion - thank you!