By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Problem creating working array value pair

P: n/a
I am working on some scripts to help me automate the website creation
process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'

I actually have a working system, but I thought I would improve by
using comma-separated key => value pairs.

The code is below.

I am using the part of the page filename as the key and the value as
the <title></title>

Problem is that the first 9 work, that is I get the correct page
linked.

When I enter
'http://localhost/template.php?page=plan2', I get the default 'home'
page.

For the life of me I don't see what is wrong.
I use HTML-Kit and notepad. (in the process or weening of wysiwyg
products)

I considered it was a text encoding problem but the same whether I use
notepad or HTML-Kit.
<?php

$allpages=array(
'Home Page'=>'home',
'Purpose'=>'purpose',
'Spiritual Gifts'=>'spiritgifts',
'Reaching Potential'=>'potential',
'Weight Loss'=>'weightloss',
'Contact Me'=>'contactme',
'Spiritual Gifts2'=>'gifts2',
'Spiritual Gifts3'=>'gifts3',
'Links'=>'links',
'The Plan'=>'plan1',
'The Plan'=>'plan2',
'The Plan'=>'plan3'
);
if (isset($_GET['page']) AND (array_search($_GET['page'], $allpages)))
{ $thispage=$_GET['page'];
$title=array_search($thispage, $allpages);
}
else
{ $thispage='home';
$title="Home Page";
}

?>

Jun 14 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
alanbe wrote:
I am working on some scripts to help me automate the website creation
process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'

Beware that search engines such as google will not follow thes links and
thus the search engine ranking of the site will suffer.
Jun 14 '06 #2

P: n/a
alanbe wrote:
I am working on some scripts to help me automate the website creation
process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'

I actually have a working system, but I thought I would improve by
using comma-separated key => value pairs.

The code is below.

I am using the part of the page filename as the key and the value as
the <title></title>

Problem is that the first 9 work, that is I get the correct page
linked.

When I enter
'http://localhost/template.php?page=plan2', I get the default 'home'
page.

For the life of me I don't see what is wrong.
I use HTML-Kit and notepad. (in the process or weening of wysiwyg
products)

I considered it was a text encoding problem but the same whether I use
notepad or HTML-Kit.
<?php

$allpages=array(
'Home Page'=>'home',
'Purpose'=>'purpose',
'Spiritual Gifts'=>'spiritgifts',
'Reaching Potential'=>'potential',
'Weight Loss'=>'weightloss',
'Contact Me'=>'contactme',
'Spiritual Gifts2'=>'gifts2',
'Spiritual Gifts3'=>'gifts3',
'Links'=>'links',
'The Plan'=>'plan1',
'The Plan'=>'plan2',
'The Plan'=>'plan3'
);
if (isset($_GET['page']) AND (array_search($_GET['page'], $allpages)))
{ $thispage=$_GET['page'];
$title=array_search($thispage, $allpages);
}
else
{ $thispage='home';
$title="Home Page";
}


Further on the actual problem, it seems that if a key value is repeated,
then array_search will only return that key if the value found is the last
one in the list with the same keys. So for your example, it'll work for
'plan3' but not for 'plan2'. Likewise, is the keys for Spiritual Gifts were
both 'Spiritual Gifts' then it would work for 'gifts3' but not for 'gifts2'.

Why not make use of the associative properties of the array and swap all the
keys and values thus:

$allpages = array (
'home' => 'Home Page',
'purpose' => 'Purpose',
'spiritgifts' => 'Spiritual Gifts',
'potential' => 'Reaching Potential',
'weightloss' => 'Weight Loss',
'contactme' => 'Contact Me',
'gifts2' => 'Spiritual Gifts2',
'gifts3' => 'Spiritual Gifts3',
'links' => 'Links',
'plan1' => 'The Plan',
'plan2' => 'The Plan',
'plan3' => 'The Plan'
);

$thispage=$_GET['page'];

if (!$title = $allpages[$thispage])
{
$title = $allpages[$thispage = 'home'];
}
Jun 14 '06 #3

P: n/a
Rik
Paul Lautman wrote:
alanbe wrote:
I am working on some scripts to help me automate the website creation
process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'

Beware that search engines such as google will not follow thes links
and thus the search engine ranking of the site will suffer.


That was ages ago.
As long as he is using a real domain instead of localhost they will de
indexed fine. Maybe they'll give a light penalty, but nothing really
serious.

Grtz,
--
Rik Wasmus
Jun 14 '06 #4

P: n/a
Rik
alanbe wrote:
Problem is that the first 9 work, that is I get the correct page
linked. if (isset($_GET['page']) AND (array_search($_GET['page'],
$allpages))) { $thispage=$_GET['page'];
$title=array_search($thispage, $allpages);
}


try:
if (isset($_GET['page']) && in_array($_GET['page'],$allpages)){
//code
}

array_search returns a key, not a boolean. Might work in a lot of cases, but
highly quircky here.
Like it states in the maual:

Warning:
This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE, such as 0 or "". Please read the section on
Booleans for more information. Use the === operator for testing the return
value of this function.

I would have reversed the array though:
'plan1' => 'The Plan' etc..

if(isset($_GET['page']) && array_key_exists($_GET['page'],$allpages)){
$thispage = $_GET['page'];
$title = $allpages[$_GET['page']];
}

Grtz,
--
Rik Wasmus
Jun 14 '06 #5

P: n/a
Rik wrote:
Paul Lautman wrote:
alanbe wrote:
I am working on some scripts to help me automate the website
creation process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'

Beware that search engines such as google will not follow thes links
and thus the search engine ranking of the site will suffer.


That was ages ago.
As long as he is using a real domain instead of localhost they will de
indexed fine. Maybe they'll give a light penalty, but nothing really
serious.

Grtz,


Google still says that they do not look at them

"Don't use "&id=" as a parameter in your URLs, as we don't include these
pages in our index. "
Jun 14 '06 #6

P: n/a

Paul Lautman wrote:
'http://localhost/template.php?page=links'

Beware that search engines such as google will not follow thes links and
thus the search engine ranking of the site will suffer.


On my host, if I call http://server/bla/links, and there is no bla
directory, bla.php is executed. This makes it possible to have nicer
URLs, all served by one page. I think this is called MultiViews.

Jun 14 '06 #7

P: n/a
Paul Lautman wrote:
When I enter
'http://localhost/template.php?page=plan2', I get the default 'home'
page.

For the life of me I don't see what is wrong.
<?php
$allpages=array( .... 'The Plan'=>'plan1',
'The Plan'=>'plan2',
'The Plan'=>'plan3'
);


Further on the actual problem, it seems that if a key value is repeated,
then array_search will only return that key if the value found is the last
one in the list with the same keys. So for your example, it'll work for


Without intending bearing on the OP's problem ... If you want to search
for multiple instances of the same value in an array, you can use
array_intersect to return all the matching instances, complete with
original keys:
$aRes = array_intersect($allpages, array("search term"));
So if you wanted to get the last matching key you could do:
$lastKey = array_pop(array_keys(array_intersect($allpages,
array("search term"))));

Csaba Gabor from Vienna

Jun 14 '06 #8

P: n/a
Here is my method.
<?php
$allpages = array(
'home' => 'Home Page',
'purpose' => 'Purpose',
'spiritgifts' => 'Spiritual Gifts',
'potential' => 'Reaching Potential',
'weightloss' => 'Weight Loss',
'gifts2' => 'Spiritual Gifts 2',
'gifts3' => 'Spiritual Gifts 3',
'links' => 'Links',
'plan' => 'The Plan',
'plan2' => 'The Plan',
'plan3' => 'The Plan'
);
// Make an associative array by use its key as page name.

if (!isset($allpages[$thispage = $_GET['page']]))
$thispage = 'home';
// If the requested page does not exists, just show the home!

$title = $allpages[$thispage];
// Get its title.

echo "<h1>$title</h1>";
// Display its title to test.
?>

Jun 14 '06 #9

P: n/a
Rik
Paul Lautman wrote:
Rik wrote:
Paul Lautman wrote:
alanbe wrote:
I am working on some scripts to help me automate the website
creation process.

I want to use a template for the whole website and call pages using
something like

'http://localhost/template.php?page=links'
Beware that search engines such as google will not follow thes links
and thus the search engine ranking of the site will suffer.


That was ages ago.
As long as he is using a real domain instead of localhost they will
de indexed fine. Maybe they'll give a light penalty, but nothing
really serious.

Grtz,


Google still says that they do not look at them

"Don't use "&id=" as a parameter in your URLs, as we don't include
these pages in our index. "


google recognizes a lot in the query string.
id=, PHPSESSID= etc. are indeed not indexed.

&page=something will

Grtz,
--
Rik Wasmus
Jun 14 '06 #10

P: n/a

DtTvB

I tried this and it works!
Thanks alot DtTvB and everyone else who replied..

alanbe

the DtTvB wrote:
Here is my method.
<?php
$allpages = array(
'home' => 'Home Page',
'purpose' => 'Purpose',
'spiritgifts' => 'Spiritual Gifts',
'potential' => 'Reaching Potential',
'weightloss' => 'Weight Loss',
'gifts2' => 'Spiritual Gifts 2',
'gifts3' => 'Spiritual Gifts 3',
'links' => 'Links',
'plan' => 'The Plan',
'plan2' => 'The Plan',
'plan3' => 'The Plan'
);
// Make an associative array by use its key as page name.

if (!isset($allpages[$thispage = $_GET['page']]))
$thispage = 'home';
// If the requested page does not exists, just show the home!

$title = $allpages[$thispage];
// Get its title.

echo "<h1>$title</h1>";
// Display its title to test.
?>


Jun 14 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.