Dynamically output the page title with php - php

I am trying to output the page title dynamically. I am using induces and this script is withing the header.php the goal is to output the header dynamically using a case/switch statement. here is my code:
<?php $title ;
switch($_SERVER['PHP_SELF']) {
case '/index.php':
$title = 'Home';
break;
case '/about.php':
$title = 'About';
break;
case '/services.php':
$title = 'Services';
break;
case '/portfolio.php':
$title = 'Portfolio';
break;
case '/staff.php':
$title = 'Staff';
break;
case '/contact.php':
$title = 'Contact us';
break;
} ?> <title><?php echo $title ?></title>
I am getting a error telling me my variable $title is not defined?
What i am doing wrong?

In your first line, you have
<?php $title ;
This $title ; shouldn't be there.
And, as Kailash Ahirwar already mentioned, it's always a good idea to provide a default value for your $title:
switch($_SERVER['PHP_SELF']) {
[...]
default:
$title = "Default title goes here";
}

Try to define $title before switch:
$title = "";

in $_SERVER global array, $_SERVER[PHP_SELF] contains full path of file like
/project_name/index.php or /project_name/about.php or /project_name/services.php
Here project_name is name of your project.
replace
case '/index.php'
case '/about.php'
case '/services.php'
....
to
case '/project_name/index.php'
case '/project_name/about.php'
case '/project_name/services.php'
.....
& also initialize $title in start of php file.
<?php $title = "";
switch ($_SERVER['PHP_SELF']) {
case '/project_name/index.php':
$title = 'Home';
break;
case '/project_name/about.php':
$title = 'About';
break;
case '/project_name/services.php':
$title = 'Services';
break;
case '/project_name/portfolio.php':
$title = 'Portfolio';
break;
case '/project_name/staff.php':
$title = 'Staff';
break;
case '/project_name/contact.php':
$title = 'Contact us';
break;
}
?>
for testing purposes
print_r($_SERVER);
and check for $_SERVER(PHP_SELF) value.

The code looks basically fine though you are missing a "default" block to catch anything that is not caught by any of the "case" statements.

though a beginner in php, i sorted this out on my own, and it is very simple and logical, consider title as string and it is initially empty one write on top $title = "";
when including header in home page just after inclusion write the value of title(reassigning value of variable)

Related

Why won't my switch statement generate the correct results?

I am using a condition and a switch to determine some default values for a few variables, but I am not getting the results that I expect.
When the link is clicked, I expect to execute the first case in the switch, but the desired variables are not overwritten.
This is my code that are into my index.php:
$titulo = '';
$keywords = '';
$descricao = '';
$post = empty($_GET['post']) ? '' : $_GET['post'];
$pagina = empty($_GET['p']) ? 'home' : $_GET['p'];
if (empty($post)) {
switch ($pagina):
case 'posts/myFile':
$titulo = 'this variable doesnot change the value on this file';
$keywords = 'this variable doesnot change the value on this file';
$descricao = 'this variable doesnot change the value on this file';
break;
case 'privacidade':
$titulo = 'Privacidade ';
break;
case 'ultimasnoticias':
$titulo = 'Ultimas Noticias';
break;
default:
$titulo = 'Home';
$pagina = 'home';
endswitch;
} else {
$titulo = 'Post';
}
My current results are:
$_GET["post"] = "myFile";
$titulo = "Post";
$keywords = "";
$descricao = "";
$pagina = "home";
My desired results are:
$_GET["post"] = "myFile";
$titulo = 'this variable doesnot change the value on this file';
$keywords = 'this variable doesnot change the value on this file';
$descricao = 'this variable doesnot change the value on this file';
$pagina = "home";
Why am I not able to update the variables using the first case statement?
Edit:
enter image description here
This is the code that are into my index.php to the nav bar:
<nav>
<ul>
<li>Início</li>
<li>Últimas Notícias
<li>Contato</li>
</ul>
</nav>
When you load index.php?post=myFile, you are generating the following element in the $_GET superglobal array:
$_GET = array ("post" => "myFile");
Then $post = empty($_GET['post']) ? '' : $_GET['post']; declares $post = "myFile".
This means that if (empty($post)) { evaluates to false (because it is not empty, it is myFile) and the switch-case block is ignored.
The else condition is executed. $titulo = 'Post';
Now, if you want the switch-case block to be executed, you must:
Change if (empty($post)) { to if (!empty($post)) {.
As for future trouble you may have within the switch-case block, make sure that you are identically matching the value for $pagina when writing each case. If you aren't sure about what values you are working with or what is being executed, just add echoes in each case to clarify.
I say this because posts/myFile is not the same string as myFile.
After more discussion about other hyperlinks, I'll recommend changing the default value for $pagina and extending the if logic to be more inclusive.
$pagina = empty($_GET['p']) ? '' : $_GET['p']; // I changed 'home' to ''
if ($post != '' || ($post == '' && $pagina != '')) { // I changed the logic

How to set a default page id?

I am trying to display content depending on page id, however when I add nothing to the url, so just index.php I get an error saying that $p is not defined, how can I give this var a default value that's outside the switch case?
<?php
$p = $_GET['p'];
switch ($p) {
case 1:
$content1 = new login();
$content = $content1->displayLogin();
break;
case 2:
echo "ID is 2";
break;
case 3:
echo "ID is 3";
break;
default:
$content1 = new dbconnection();
$content = $content1->displayTable();
}
I understand that you want to make $p have a default value if $_GET['p'] is not defined.
You can do it like this:
$p = isset($_GET['p']) ? $_GET['p'] : 'defaultValue';
or, if you're on PHP 7:
$p = $_GET['p'] ?? 'defaultValue';
Replace:
$p = $_GET['p'];
With:
$p = !empty($_GET['p']) ? $_GET['p'] : default_id_value_here;
You can do the following:
$p = $_GET['p'] ?? <default value>; // e.g 1 or 3 etc.
Read more about it at this question: PHP syntax question: What does the question mark and colon mean?

After adding detection of browser language, the language href anchor (hl) stops working

I know how to detect language browser in PHP and also how to detect the language href anchor for switching to another.
My old codes which doesn't have locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);:
if (isset($_GET['hl']))
{
$langOption = $_GET['hl'];
}
else
{
$langOption = '';
}
switch ($langOption):
case 'ca':
$language = 'cat';
break;
case 'el':
$language = 'el';
break;
case 'en':
$language = 'en';
break;
case 'en_GB':
$language = 'en_GB';
break;
case 'es':
$language = 'es';
break;
case 'fr':
$language = 'fr';
break;
case 'ka':
$language = 'ka';
break;
case 'nl':
$language = 'nl';
break;
case 'pt_PT':
$language = 'pt_PT';
break;
case 'pt_BR':
$language = 'pt_BR';
break;
case 'ro':
$language = 'ro';
break;
default:
$language = 'pt_BR';
break;
endswitch;
require_once("idiomas/{$language}.php");
These old codes worked very for language href anchors. Like:
<a class="waves-effect" href="?hl=en_GB" name="en">English</a>
<a class="waves-effect" href="?hl=pt_BR" name="pt_BR">Português Brasileiro</a>
I changed to codes to condense, economise and shorten the codes. Here are new codes:
$language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (isset($_GET['hl']))
{
$lang = $_GET['hl'];
}
else
{
$lang = '';
}
switch ($lang):
case $language:
$language = $language;
break;
default:
$language = $language;
break;
endswitch;
require_once("idiomas/{$language}.php");
Only the detection of browser language worked very well, but the language href anchors (hl) do not work, because if you switch to Portuguese, the page still in English.
Similar like:
switch ($lang):
case "en":
$language = "en";
break;
case "en_GB":
$language = "en_GB";
break;
case "pt_BR":
$language = "pt_BR";
break;
case "pt_PT":
$language = "pt_PT";
break;
endswitch;
But I wouldn't like to repeat these old codes. I want to keep condensing and shortening the same new codes.
I don't understand what the switch statement is doing here?
The reason it's not working is because you're not using the $lang variable for anything, it's just setting $language to itself for all cases.
I think this would do what you want :
$language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (isset($_GET['hl']))
{
$language = $_GET['hl'];
}
As query params can be modified by the user, I'd also suggest using in_array to check that the value is something expected.

PHP switch() not working

I have this PHP switch:
<?php
$destination = isset($_GET['act']);
switch ($destination) {
default:
echo "test";
break;
case "manage":
echo "manage things";
break;
case "create":
echo "create things";
break;
}
?>
However, when I go to test.php?act=create, the output is manage things not create things.... and when I go to test.php?act=manage -- of course I get manage things...
So ... how do I fix this? Thank you
php's isset returns a boolean. So $destination is either true or false, not a string.
Try
if(isset($_GET['act']))
$destination = $_GET['act'];
Your problem is:
$destination = isset($_GET['act']);
isset returns either true or false, never any of the string values you are using.
You could use something like:
$destination = isset($_GET['act']) ? $_GET['act'] : '';
You have to use:
<?php
if(isset($_GET['act'])) $destination = $_GET['act'];
switch ($destination) {
case "manage":
echo "manage things";
break;
case "create":
echo "create things";
break;
default:
echo "test";
}
Or just use:
$destination = #$_GET['act'];

if statement switch

<?php
if ($_GET['user_id']) $content = $_GET['user_id'];
else $content = $_GET['content'];
switch($content) {
//The default page
default:
include_once('main.php');
break;
//The news related stuff
case 'news':
include_once('news.php');
break;
//Show the profile of a user
case 'user_id':
include_once('profile.php');
break;
}
?>
index.php?user_id=id won't work. Any ideas?
Maybe you intended this:
if (isset($_GET['user_id'])) $content = 'user_id';
Instead of:
if ($_GET['user_id']) $content = $_GET['user_id'];
else $content = $_GET['content'];
Then the switch would execute the user_id case and include profile.php.
the default case needs to be the last one.
switch($content) {
//The news related stuff
case 'news':
include_once('news.php');
break;
//Show the profile of a user
case 'user_id':
include_once('profile.php');
break;
//The default page
default:
include_once('main.php');
break;
}
Plus, you said that this doesn't work: index.php?user_id=id
The $content variable is being populated by the value of user_id in the _GET string, therefore if you wanted to see something other than the default page, you'd have to do one of these:
index.php?user_id=news
index.php?user_id=user_id
This probably isn't how you want it to behave, but given your code, this is what it's doing...

Categories