Search code examples
phpvariablesoutputisset

Why does my script output an end • ( • ) and what is the best way to remove it?


I wrote this "You are here" script for a website and it is working, however the output for the story category has an extra trailing bull and I'd like to know why and remove the •

<?php
if (isset($_GET['pagina']) && isset($_GET['storycategory'])) {
$pagina = $_GET['pagina'];
$storycategory = $_GET['storycategory'];
$storypage = $storycategory;
$storypage = str_replace(stories, story, $storypage);
$storypagename = $_GET[$storypage];
$content_path = 'You are here: Home &bull; ' . $pagina . ' &bull; ' . $storycategory . ' &bull; ' . $storypagename;
$content_path = str_replace("-", " ", $content_path);
echo '<strong>' . $content_path . '</strong><hr />'; // Voor een bezoeker die in een bepaald verhaal bezoekt - Output: You are here: Home • all stories • story category Name • Best story ever
}
elseif (isset($_GET['pagina']) && isset($_GET['storycategory'])) { // this is the part that has a trailing  •  that I'd like to remove and find out why it was created by my script
    $pagina = $_GET['pagina'];
    $storycategory = $_GET['storycategory'];
    $content_path = 'You are here: Home &bull; ' . $pagina . ' &bull; ' . $storycategory; // Note: I did NOT add a  •  here but it is generated in output...
    $content_path = str_replace("-", " ", $content_path);
    echo '<strong>' . $content_path . '</strong><hr />'; // Voor een bezoeker die in een bepaalde alles uit deze story category pagina is! Output: You are here: Home You are here: Home • all stories • story category Name • 
    }
    elseif (isset($_GET['pagina'])) {
        $pagina = $_GET['pagina'];
        $content_path = 'You are here: Home &bull; ' . $pagina;
        $content_path = str_replace("-", " ", $content_path);
        echo '<strong>' . $content_path . '</strong><hr />'; // Voor een bezoeker die op de alle sex stories pagina is! Output: Home • all stories
        }
        else {
            // Als iemand verdwaald is en daardoor bovenstaande totaal niet van toepassing is...
            echo '<strong>Thank you for visiting our site!</strong><hr />';
            }
            ?>

Pagina is just fine, no trailing bull but when I visit any category there will be a • that I don't like to show up (See comment output and note in the code) but when I visit any story page in any category there is no • at the end...

How should I remove the bull and why was it generated?

Thank you for reading!

EDIT I would like to explain the used variables in my code, I hope it will help to understand this question.

I have some URL's and would like to tell visitors where they are:

Page for a story: www.example.com/index.php?pagina=all-stories&storycategory=the-best-stories&the-best-story=my-first-computer

Page for all stories inside "the best stories": www.example.com/index.php?pagina=all-stories&storycategory=the-best-stories

Page for all story categories: www.example.com/index.php?pagina=all-stories

I have a folder called pagina, in this folder you will find a folder called storycategory, in the folder storycategory you will find a huge number of folders and all of them have stories as last part of their names and a lot of php files that have story at the end of there mame (same names but changed stories to story for the php files in storycategory) in the folders inside storycategory folder are my stories stored..

Part 1 to echo "you are here: Home • all stories • the best story • my first computer" this is working just fine

        if (isset($_GET['pagina']) && isset($_GET['storycategory']) && isset($_GET['storycategory'])) {
$pagina = $_GET['pagina'];
$storycategory = $_GET['storycategory'];
$storypage = $storycategory;
$storypage = str_replace(stories, story, $storypage);
$storypagename = $_GET[$storypage];
$content_path = 'You are here: Home &bull; ' . $pagina . ' &bull; ' . $storycategory . ' &bull; ' . $storypagename;
$content_path = str_replace("-", " ", $content_path);
echo '<strong>' . $content_path . '</strong><hr />'; // Voor een bezoeker die in een bepaald verhaal bezoekt - Output: You are here: Home • all stories • story category Name • Best story ever
}

Part 2 where it's going wrong with the unwanted •

    elseif (isset($_GET['pagina']) && isset($_GET['storycategory'])) { // this is the part that has a trailing  •  that I'd like to remove and find out why it was created by my script
$pagina = $_GET['pagina'];
$storycategory = $_GET['storycategory'];
$content_path = 'You are here: Home &bull; ' . $pagina . ' &bull; ' . $storycategory; // Note: I did NOT add a  •  here but it is generated in output...
$content_path = str_replace("-", " ", $content_path);
echo '<strong>' . $content_path . '</strong><hr />'; // Voor een bezoeker die in een bepaalde alles uit deze story category pagina is! Output: You are here: Home You are here: Home • all stories • story category Name • 
}

Part 3 that is just fine too, without problems

    elseif (isset($_GET['pagina'])) {
    $pagina = $_GET['pagina'];
    $content_path = 'You are here: Home &bull; ' . $pagina;
    $content_path = str_replace("-", " ", $content_path);
    echo '<strong>' . $content_path . '</strong><hr />'; // Voor een bezoeker die op de alle sex stories pagina is! Output: Home • all stories
    }

Part 4 of my code is the else for if no story related variable was set or unknown error

    else {
        // Als iemand verdwaald is en daardoor bovenstaande totaal niet van toepassing is...
        echo '<strong>Thank you for visiting our site!</strong><hr />';
        }

What is wrong with part 2 or why do I get the bull at the end?

EDIT 2 edited line 2 Replaced if (isset($_GET['pagina']) && isset($_GET['storycategory']) && isset($_GET['storycategory'])) { with if (isset($_GET['pagina']) && isset($_GET['storycategory'])) {

Problem with • at the end is still there on the storycategory pages, $storypage is used to create $storypagename from storycategory using str_replace and Home is hard coded.

EDIT 3 (Solved) isset($_GET[$storypagename])) (Line 2) and $storypagename = $_GET[$storypagename]; did solve the problem in the srories global file, but any variable must be defined before a request like I said in comments before so I had to find a way to define $storypagename before line 2. I defined it in all of the meta tag files for every single page and this is working perfect.

NOTE: The last 2 bold parts are dynamic, so the last url variable will change if a visitor is in any other category. Story page: www.example.com/index.php?pagina=all-stories&storycategory=the-best-stories&the-best-story=my-first-computer Page for stories inthis category: www.example.com/index.php?pagina=all-stories&storycategory=the-best-stories Page for all story categories: www.example.com/index.php?pagina=all-stories

First I tried to use str_replace() using the storycategory value but I was not able to define it before so it never worked, I believe a small extra line in meta tag file $storypagename = 'the-best-story'; is the best available solution

My site has a 1000's of pages so an array() was no option, every page has got a meta tag php file and is loaded before this script, there I defined the $storypagename variable to make sure it will be defined before it will be requested. this solution made php code smaller.

I'd like to say thank you to both mudasobwa AND Konstantin, both answers were helpful to find this method to solve this error. All 3 parts are working fine now, thanks again! Final working code

    <?php 
    if (isset($_GET['pagina']) && isset($_GET['storycategory']) && isset($_GET[$storypagename])) {
    $pagina = $_GET['pagina'];
    $storycategory = $_GET['storycategory'];
    $storypagename = $_GET[$storypagename];
    $content_path = 'You are here: Home &bull; ' . $pagina . ' &bull; ' . $storycategory . ' &bull; ' . $storypagename;
    $content_path = str_replace("-", " ", $content_path);
    echo '<strong>' . $content_path . '</strong><hr />'; // Voor een bezoeker die in een bepaald verhaal bezoekt - Output: You are here: Home • all stories • story category Name • Best story ever
    }
    elseif (isset($_GET['pagina']) && isset($_GET['storycategory'])) { // this is the part that has a trailing  •  that I'd like to remove and find out why it was created by my script
$pagina = $_GET['pagina'];
$storycategory = $_GET['storycategory'];
$content_path = 'You are here: Home &bull; ' . $pagina . ' &bull; ' . $storycategory;  // Note: I did NOT add a  •  here but it is generated in output...
$content_path = str_replace("-", " ", $content_path);
echo '<strong>' . $content_path . '</strong><hr />'; // Voor een bezoeker die in een bepaalde alles uit deze story category pagina is! Output: You are here: Home You are here: Home • all stories • story category Name • 
}
    elseif (isset($_GET['pagina'])) {
    $pagina = $_GET['pagina'];
    $content_path = 'You are here: Home &bull; ' . $pagina;
    $content_path = str_replace("-", " ", $content_path);
    echo '<strong>' . $content_path . '</strong><hr />'; // Voor een bezoeker die op de alle sex stories pagina is! Output: Home • all stories
    }
    else {
        // Als iemand verdwaald is en daardoor bovenstaande totaal niet van toepassing is...
        echo '<strong>Thank you for visiting our site!</strong><hr />';
               }
               ?>

Solution

  • You copy-pasted the very first if condition erroneously:

    if (
      isset($_GET['pagina']) && 
      isset($_GET['storycategory']) && 
      isset($_GET['storycategory'])) { // ERROR, should be $_GET['storypagename']
    

    and you enter this with empty $storypagename.

    Whether $storypagename is being calculated, it should be calculated and checked here, because this condition is likely always true and you never enter else if sections.