/    Sign up×
Community /Pin to ProfileBookmark

Is there a better way to do this?

I have been using the following method for a long time now, but someone recently said to me its ugly and there are ways to do it that will perform quicker too. He mentioned using and array in a separate file but I am not familiar with this method. Can anyone give me some guidance please?

[code=php]<?php
$about = isset($_GET[‘d’]) ? $_GET[‘d’] : ‘about’;

switch($about) {
/*———————– PAGES ———————————–*/
case ‘profile’:
$title = ‘My Title’;
$keyword = ‘some keywords’;
$description = ‘A great description.’;
break;

default:
$title = ‘My Title’;
$keyword = ‘some keywords’;
$description = ‘A great description.’;
break;
}
include($_SERVER[‘DOCUMENT_ROOT’]. ‘/include/header.php’);

include($_SERVER[‘DOCUMENT_ROOT’].’/about/’ .$about.’.php’);

include($_SERVER[‘DOCUMENT_ROOT’]. “/include/footer.php”);

?> [/code]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 15.2012 — Maybe they mean something like this:

pages.php:
[code=php]
<?php
$pages = array(
'profile' => array(
$title = 'My Title';
$keyword = 'some keywords';
$description = 'A great description.';
),
'another_page' => array(
$title = 'Another Title';
$keyword = 'some more keywords';
$description = 'A better description.';
),
'default' => array(
$title = 'My Title';
$keyword = 'some keywords';
$description = 'A great description.';
)
);
$page = 'default';
if(isset($_GET['page']) and isset($pages[$_GET['page']])) {
$page = $_GET['page'];
}
?>
[/code]

Some page:
[code=php]
<?php
require_once 'pages.php';
?>
<html>
<head>
<title><?php echo $pages[$page]['title'];?></title>
<meta name='keywords' content='<?php echo $pages[$page]['keyword'];?>' />
<meta name='description' content='<?php echo $pages[$page]['description'];?>' />
[/code]
Copy linkTweet thisAlerts:
@chrisgukauthorApr 15.2012 — Thanks NogDog,

I have been doing some reading and your suggestion seems to be the most practical method. Are there any issues with performance in that example?
Copy linkTweet thisAlerts:
@chrisgukauthorApr 15.2012 — Thanks NogDog,

I have been doing some reading and your suggestion seems to be the most practical method. Are there any issues with performance in that example?[/QUOTE]


In fact just to add to that.

The code I supplied is contained within my index.php, which of you can see pulls the content pages in. My header and footer are includes.

because the variable $page is declared in the pages.php how would the index.php know what the variable $page is?

Or!!!

Is the construction of my index.php completely wrong in that I dont need an include for the header and footer?
Copy linkTweet thisAlerts:
@NogDogApr 15.2012 — There are many, many ways to organize such things, and I'm not going to claim that one is better than another. One way to approach it, though, would be to functionalize things as much as possible. You could put the code I cobbled together into a function that would just return a single, simple array based on the value provided to it. You could then put the header and footer parts of the page into a function. Then you would just call those functions as needed, e.g.:

page_stuff.php:
[code=php]
<?php
/**
* Get the attributes of a page
* @return array
* @param string $pageName Name of the page
*/
function getPageInfo($pageName)
{
$pages = array(
'profile' => array(
$title = 'My Title';
$keyword = 'some keywords';
$description = 'A great description.';
),
'another_page' => array(
$title = 'Another Title';
$keyword = 'some more keywords';
$description = 'A better description.';
),
'default' => array(
$title = 'My Title';
$keyword = 'some keywords';
$description = 'A great description.';
)
);
$page = 'default';
if(isset($_GET['page']) and isset($pages[$_GET['page']])) {
$page = $_GET['page'];
}
return $pages[$page];
}

/**
* Get the top-of-page stuff
* @return string
* @param array $pageData
*/
function htmlHead(array $pageData)
{
/*
might want some error-checking here to make sure $pageData is complete
*/
$html = "<!DOCTYPE html>
<html>
<head>
<title>".htmlspecialchars($pageData['title'])."</title>
<meta name='keywords' content='".htmlspecialchars($pageData['keyword'])."' />
<meta name='description' content='".htmlspecialchars($pageData['description'])."' />
</head>
<body>";
return $html;
}
[/code]

index.php:
[code=php]
<?php
require_once 'page_stuff.php';
$pageName = isset($_GET['page']) ? $_GET['page'] : 'default';
echo htmlHead(getPageInfo($pageName));
[/code]

Those functions could be in the same include file, or separate files, or you could even get all object-oriented about it and put them into class definitions.

You could even separate the data from the code more by putting the $pages array into a separate, non-PHP file that gets read by that function. The JSON format is very handy for this sort of thing, as it can be quickly and easily parsed into a PHP array (or object) via json_decode().
Copy linkTweet thisAlerts:
@chrisgukauthorApr 20.2012 — There are many, many ways to organize such things, and I'm not going to claim that one is better than another. One way to approach it, though, would be to functionalize things as much as possible. You could put the code I cobbled together into a function that would just return a single, simple array based on the value provided to it. You could then put the header and footer parts of the page into a function. Then you would just call those functions as needed, e.g.:

page_stuff.php:
[code=php]
<?php
/**
* Get the attributes of a page
* @return array
* @param string $pageName Name of the page
*/
function getPageInfo($pageName)
{
$pages = array(
'profile' => array(
$title = 'My Title';
$keyword = 'some keywords';
$description = 'A great description.';
),
'another_page' => array(
$title = 'Another Title';
$keyword = 'some more keywords';
$description = 'A better description.';
),
'default' => array(
$title = 'My Title';
$keyword = 'some keywords';
$description = 'A great description.';
)
);
$page = 'default';
if(isset($_GET['page']) and isset($pages[$_GET['page']])) {
$page = $_GET['page'];
}
return $pages[$page];
}

/**
* Get the top-of-page stuff
* @return string
* @param array $pageData
*/
function htmlHead(array $pageData)
{
/*
might want some error-checking here to make sure $pageData is complete
*/
$html = "<!DOCTYPE html>
<html>
<head>
<title>".htmlspecialchars($pageData['title'])."</title>
<meta name='keywords' content='".htmlspecialchars($pageData['keyword'])."' />
<meta name='description' content='".htmlspecialchars($pageData['description'])."' />
</head>
<body>";
return $html;
}
[/code]

index.php:
[code=php]
<?php
require_once 'page_stuff.php';
$pageName = isset($_GET['page']) ? $_GET['page'] : 'default';
echo htmlHead(getPageInfo($pageName));
[/code]

Those functions could be in the same include file, or separate files, or you could even get all object-oriented about it and put them into class definitions.

You could even separate the data from the code more by putting the $pages array into a separate, non-PHP file that gets read by that function. The JSON format is very handy for this sort of thing, as it can be quickly and easily parsed into a PHP array (or object) via json_decode().[/QUOTE]


Hi,

I have tried your code and keep getting an error regarding this line:

Parse error: syntax error, unexpected ';', expecting ')' in /var/www/test/page_stuff.php on line 7

[code=php]$title = 'My Title';[/code]
Copy linkTweet thisAlerts:
@WyCnetApr 20.2012 — There were typographic mistakes on each set corresponding to the following correction:

'another_page' =&gt; array(
$title =&gt; 'Another Title';
$keyword =&gt; 'some more keywords';
$description =&gt; 'A better description.';
),



and you should set beforehand:

$title = 'title';
$keyword = 'key'
$description = 'desc';

then try it again...
Copy linkTweet thisAlerts:
@chrisgukauthorApr 20.2012 — There were typographic mistakes on each set corresponding to the following correction:

'another_page' =&gt; array(
$title =&gt; 'Another Title';
$keyword =&gt; 'some more keywords';
$description =&gt; 'A better description.';
),



and you should set beforehand:

$title = 'title';
$keyword = 'key'
$description = 'desc';

then try it again...[/QUOTE]


Hi,

That has not fixed it. This is what I have:

[code=php]<?php
$pages = array(
'another_page' => array(
$title => 'Another Title';
$keyword => 'some more keywords';
$description => 'A better description.';
),
'another_page' => array(
$title => 'Another Title';
$keyword => 'some more keywords';
$description => 'A better description.';
),
'another_page' => array(
$title => 'Another Title';
$keyword => 'some more keywords';
$description => 'A better description.';
)
);
$page = 'default';
if(isset($_GET['page']) and isset($pages[$_GET['page']])) {
$page = $_GET['page'];
}
?> [/code]


and this is the error:

Parse error: syntax error, unexpected ';', expecting ')' in /var/www/test/pages.php on line 6
Copy linkTweet thisAlerts:
@NogDogApr 20.2012 — Commas instead of semi-colons between each array element.
Copy linkTweet thisAlerts:
@chrisgukauthorApr 20.2012 — lol ? :p

This is driving me crazy ?

Now im at this stage and it still doesnt work ?

[code=php]<?php
$pages = array(
'another_page' => array(
$title = 'Another Title',
$keyword = 'some more keywords',
$description = 'A better description.',
),
'another_page' => array(
$title = 'Another Title',
$keyword = 'some more keywords',
$description = 'A better description.',
),
'default' => array(
$title = 'Another Title',
$keyword = 'some more keywords',
$description = 'A better description.',
)
);
$page = 'default';
if(isset($_GET['page']) and isset($pages[$_GET['page']])) {
$page = $_GET['page'];
}
?> [/code]
Copy linkTweet thisAlerts:
@NogDogApr 20.2012 — No comma after the last element of an array -- it's a separator, not a terminator. ?
×

Success!

Help @chrisguk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...