|
Posted by Paul Lautman on 06/14/06 14:07
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'];
}
Navigation:
[Reply to this message]
|