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
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';
?>

Link to comment
Share on other sites

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';
}

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.