Tuesday 15 January 2013

php - Change title on page -



php - Change title on page -

i have next code alter title on page:

class.php

<?php class title_and_page { public $pagekey; public $title; public $page; private $pages = array( 'start_page' => array("startsida", "start_page.php"), 'products' => array("produkter", "products.php"), 'max_ot' => array("max-ot", "max_ot.php"), 'blog' => array("blogg", "blog.php"), 'tools' => array("verktyg", "tools.php"), 'about_us' => array("om oss", "about_us.php")); public function __construct($pagekey) { $this->pagekey = $pagekey; } public function settitle() { if(array_key_exists($this->pagekey, $this->pages)) { $this->title = $this->pages[$this->pagekey][0]; //returns value in title, gets when constructs run homecoming $this->title; } } public function includepage() { if(array_key_exists($this->pagekey, $this->pages)) { $this->page = $this->pages[$this->pagekey][1]; //returns value in page, included homecoming $this->page; } } } ?>

here code index.php

if(isset($_get['page'])) { $page = $_get['page']; } $page_title = new title_and_page($page); <title><?= $page_title->settitle(); ?></title> <li id="info"><a href="?page=products" class='clickme'>produkter</a></li> <li id="info"><a href="?page=max_ot">max-ot</a></li> <li id="info"><a href="?page=blog">blogg</a></li> <li id="info"><a href="?page=tools">verktyg</a></li> <li id="info"><a href="?page=about_us">om oss</a></li>

this works. however, have articles in blog page contains "read more"-links. when click on "read more"-link, url changes this: index.php?page=blog&readmore_from_firstpage=1&article_header=vilken kolhydrat är bäst att äta efter träningen?

how can alter title of page, value in $_get['article_header'] can see above?

just extend checks:

if(isset($_get['article_header'])) { $page = $_get['article_header']; } elseif(isset($_get['page'])) { $page = $_get['page']; }

but since you're checking in class whether page whitelisted, you'd need either add together variable forcefulness avoiding such check or print out title if article_header present.

here's illustration of latter:

$avoidclass = false; if(isset($_get['article_header'])) { $page = $_get['article_header']; $avoidclass = true; } elseif(isset($_get['page'])) { $page = $_get['page']; }

then in html:

<title><?= $avoidclass ? $page : $page_title->settitle(); ?></title>

or, simplest way:

if(isset($_get['article_header'])) { $page_title = $_get['article_header']; } elseif(isset($_get['page'])) { $page_title = new title_and_page($_get['page'])->settitle(); } else { $page_title = 'default title here'; }

html

<title><?= $page_title ?></title>

php url get

No comments:

Post a Comment