-6

I am trying to incorporate logic into my website so that the $customTitle is working with the $nav1 variable. Below is how the page in question is structured.

<?
    $customTitle = "$nav1 | Example.com";
    include_once("_common.php");
    include_once(G5_PATH."head.php");
?>

Currently, the HTML title is just showing as | Example.com so $nav1 is not being included.

I have a switch statement in head.php but because that is loaded after (needs to be) it doesn't actually get the data. Below is an example of that switch statement.

switch($gm_code){
    case "pd01" : $nav1 = "Category 1"; break;
    case "pd02" : $nav1 = "Category 2"; break;
    case "pd03" : $nav1 = "Category 3"; break;
    case "pd04" : $nav1 = "Category 4"; break;
    case "pd05" : $nav1 = "Category 5"; break;
}

So with this said, the final title would be Category 1 | Example.com.

I have tried to implement logic into this by using an if statement that looks at $gm_code and gives $nav1 different values based on that code. Below is an example of that.

<?
    if ($gm_code == "pd01") {
        $nav1 = "Category 1";
    }
    $customTitle = "$nav1 | Example.com";
    include_once("_common.php");
    include_once(G5_PATH."head.php");
?>

How can I make this logic make sense and include the $nav1 variable so that it reflects the correct category name in the HTML <title>?

Edit:

I cannot just switch the order because it then defaults to the title in head.php which uses <title><?php echo $g5_head_title; ?></title>. I have incorporated the following if logic to make $customTitle work, do I have to switch this up to allow the order to change?

if (isset($customTitle)) {
  $g5['title'] = $customTitle;
  $g5_head_title = $customTitle;
} else if (!isset($g5['title'])) {
  $g5['title'] = $config['cf_title'];
  $g5_head_title = $g5['title'];
}
else {
  $g5_head_title = $g5['title'];
  $g5_head_title .= " | ".$config['cf_title'];
}
  • `` short opening tags is very bad practise and you should be using ` – Martin Mar 01 '23 at 17:16
  • You need to include `head.php` *before* you use the `$nav1` variable in `$customTitle` – Barmar Mar 01 '23 at 17:16
  • You need to ensure that `$gm_code` has been set before running either `switch` or `if` Without that set both are useless – RiggsFolly Mar 01 '23 at 17:18
  • @RiggsFolly See edit in question. – Blake Weston Mar 01 '23 at 17:26
  • Which script is that added code in? – Barmar Mar 01 '23 at 17:27
  • @Barmar It's in `header.sub.php` which is being called by `_common.php` – Blake Weston Mar 01 '23 at 17:30
  • Then you have to move `_common.php` down as well. – Barmar Mar 01 '23 at 17:31
  • If you are struggling with include order and conflicting code, ie. you're iterating one long procedure with criss-cross dependencies, then stop including files with side-effects, and instead wrap your stuff in functions that you call in the necessary order, and build your output and echo it in one go, instead of having echo statements all over the place. – Markus AO Mar 01 '23 at 17:35

2 Answers2

0

Move head.php up so you set $nav1 before you try to use it in the value of $customTitle.

<?
    include_once(G5_PATH."head.php");
    $customTitle = "$nav1 | Example.com";
    include_once("_common.php");
?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    See edits in question. – Blake Weston Mar 01 '23 at 17:26
  • Unfortunately, when I do this the page doesn't load at all. `_common.php` needs to be loaded before `head.php` and `head.php` needs to be loaded before `$customTitle` in order for it to work and that way it doesn't know what the variable is. – Blake Weston Mar 09 '23 at 21:31
  • Can you add me to a chat by chance? I would really appreciate more help with this. It's been a tough one for me. – Blake Weston Mar 09 '23 at 21:32
0

I was able to get it to work using a specific order combined with if logic for all of the dynamic categories. As long as _common.php is loaded before the if statements and the $customTitle is loaded after the if statements and before the head.php, it works.

<?
    include_once("_common.php");

    if($gm_code == "pd01"){$nav1 = "Category 1";}
    if($gm_code == "pd02"){$nav1 = "Category 2";}
    if($gm_code == "pd03"){$nav1 = "Category 3";}
    if($gm_code == "pd04"){$nav1 = "Category 4";}
    ... etc

    $customTitle = "$nav1 | EXAMPLE.COM";
    include_once(G5_PATH."/_head.php");
?>

So for example, if the dynamic page I'm trying to load uses the $gm_code == "pd01" to get its content then the <title> is shown as Category 1 | EXAMPLE.COM.

  • does anything in _head.php need the code above its include? if index.php need common.php and common.php also need head.php please include both head.php and common.php in order – Irvan Hilmi Mar 16 '23 at 06:55