Jump to content

[SOLVED] php array for title tags


adamlacombe

Recommended Posts

I can't seem to get this here to work:

$r= array("forum", "tos");
$r2= array("Forum", "Terms Of Service");
$f = str_replace($r, $r2, $f);
if($_GET['action'] == $r){
$title = $f;
}else{
$title="Home";
}

 

$title is in the title tags like this:

<title>$sitename - $title</title>

in another file

but I want it so if, for example:

?action=tos  it displays Terms of Service in the title tags instead of tos.

 

Any idea why what I have might not be working?

Link to comment
https://forums.phpfreaks.com/topic/167139-solved-php-array-for-title-tags/
Share on other sites

if($_GET['action'] == $r){

 

$r is an array, so $_GET['action'] would not equal that.

 

Why not do this:

<?php
switch($_GET['action']){
    case 'forum': $title = 'Forum';
    break;

    case 'tos': $title = 'Terms of Service';
    break;

    default: $title = 'Home'
    break;
}
?>

 

 

OR

 

 

<?php
$names = array(
    'tos' => 'Terms of Service',
    'forum' => 'Forum'
);
if (in_array($_GET['action'],$names)){
    $title = $names[$_GET['action']];
}
else $title = 'Home';
?>

Why not just do like:

// Setup an array with the pages and their names
$titleArray = array('forum'=>'Forum', 'tos'=>'Terms of Service');
// if there is a page in the array with the one in the url
if(isset($titleArray[$_GET['action']])) {
   // use the one in the array
   $title = $titleArray[$_GET['action']];
} else {
   // otherwise use a default
   $title = 'Home';
}

<?php
$names = array(
    'tos' => 'Terms of Service',
    'forum' => 'Forum'
);
if (in_array($_GET['action'],$names)){
    $title = $names[$_GET['action']];
}
else $title = 'Home';
?>

in_array() looks for array values, not keys. Instead, you should use array_key_exists, or use isset like I did.

 

Also, @OP: You probable want to do something like: $action = strtolower($_GET['action']); to make sure they match up since it is case sensitive. That way if a user puts script.php?action=TOS it still goes to the tos page.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.