【问题标题】:Why does my script output an end • ( • ) and what is the best way to remove it?为什么我的脚本输出结束 • ( • ) 以及删除它的最佳方法是什么?
【发布时间】:2014-11-24 12:35:01
【问题描述】:

我为一个网站编写了这个“你在这里”脚本并且它正在运行,但是故事类别的输出有一个额外的尾随牛,我想知道为什么并删除 •

<?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 很好,没有尾随公牛,但是当我访问任何类别时,会有一个我不喜欢显示的 • (请参阅代码中的评论输出和注释)但是当我访问任何类别末尾没有•...

我应该如何移除公牛,为什么会产生它?

感谢您的阅读!

编辑 我想解释一下我的代码中使用的变量,希望它有助于理解这个问题。

我有一些 URL,想告诉访问者他们在哪里:

故事页面:www.example.com/index.php?pagina=all-stories&storycategory=the-best-stories&the-best-story=my-first-computer

“最佳故事”中所有故事的页面:www.example.com/index.php?pagina=all-stories&storycategory=the-best-stories

所有故事类别的页面:www.example.com/index.php?pagina=all-stories

我有一个名为 pagina 的文件夹,在这个文件夹中你会找到一个名为 storycategory 的文件夹,在文件夹 storycategory 中你会发现大量的文件夹,所有的文件夹都以故事作为他们名字的最后一部分和很多 php在storycategory文件夹内的文件夹中,以故事结尾的文件(名称相同但将storycategory中的php文件更改为故事)是我存储的故事。

第 1 部分呼应“你在这里:首页 • 所有故事 • 最好的故事 • 我的第一台计算机”,这工作得很好

        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
}

第 2 部分不想要的地方出了问题 •

    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 • 
}

第 3 部分也很好,没有问题

    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
    }

如果没有设置故事相关变量或未知错误,我的代码的第 4 部分是 else

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

第 2 部分有什么问题,或者为什么我在最后得到了牛市?

编辑 2 编辑的第 2 行 更换 if (isset($_GET['pagina']) && isset($_GET['storycategory']) && isset($_GET['storycategory'])) { 和 if (isset($_GET['pagina']) && isset($_GET['storycategory'])) {

故事类别页面上仍然存在 • 结尾的问题,$storypage 用于使用 str_replace 从 storycategory 创建 $storypagename,而 Home 是硬编码的。

编辑 3(已解决) isset($_GET[$storypagename]))(第 2 行)和 $storypagename = $_GET[$storypagename]; 确实解决了 srories 全局文件中的问题,但是任何变量都必须在我之前在 cmets 中所说的请求之前定义,所以我必须找到一种方法来定义 $storypagename before 第 2 行。我在每个页面的所有元标记文件中都定义了它,这是完美的。

注意:最后 2 个粗体部分动态的,因此如果访问者属于任何其他类别,最后一个 url 变量将会改变。 故事页面: www.example.com/index.php?pagina=all-stories&storycategory=the-best-stories&the-best-story=my-first -电脑 此类别的故事页面:www.example.com/index.php?pagina=all-stories&storycategory=the-best-stories 所有故事类别的页面: www.example.com/index.php?pagina=all-stories

首先我尝试使用 str_replace() 使用 storycategory 值,但我之前无法定义它,所以它从来没有工作过,我相信元标记文件 $storypagename = 'the-best-story'; 中的一个额外的小行是最好的解决方案

我的网站有 1000 个页面,所以没有选择 array(),每个页面都有一个元标记 php 文件并在此脚本之前加载,我在那里定义了 $storypagename 变量以确保它会被定义在被要求之前。此解决方案使 php 代码更小。

我要感谢 mudasobwaKonstantin,这两个答案都有助于找到解决此错误的方法。 现在所有 3 个部分都工作正常,再次感谢! 最终工作代码

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

【问题讨论】:

    标签: php variables output isset


    【解决方案1】:

    您错误地复制粘贴了第一个 if 条件:

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

    然后你输入这个空$storypagename

    $storypagename 是否正在计算,这里应该计算和检查,因为这个条件很可能总是true 而你永远不会进入else if 部分。

    【讨论】:

    • 是的,我明白了,应该是这样,但是在我的代码开头 storypagename 不存在,所以我应该先定义它或者不检查 $storypagename(它在第 7 行定义,所以不能在第 2 行使用它),我会尝试在第 2 行之前定义它,我认为这样会更好,否则如果我更改它会返回错误。我可以将我的链接发布到故事页面但不确定它是允许发布成人故事网址。第一部分和最后一部分正在工作。唯一的问题是 • 在第 2 部分结束时
    • 这是第 1 部分,而不是第 2 部分。您的代码永远不会到达第 2 部分。
    【解决方案2】:

    正如 mudasobwa 所说:因为你的第一个条件错误(第 2 行)

    if (isset($_GET['pagina']) && isset($_GET['storycategory']) && isset($_GET['storycategory'])) {
    

    你永远不会进入第一个 elseif 块(在第 12 行)

    被执行的行是第 8 行:

    $content_path = 'You are here: Home &bull; ' . $pagina . ' &bull; ' . $storycategory . ' &bull; ' . $storypagename;
    

    但是$storypagename 是空的。所以你最后会得到一个子弹

    【讨论】:

    • 我编辑了我的帖子来解释我的代码,$storypagename 是在第 7 行创建的,这是第一部分,第一部分正在工作,我可以添加一个指向我的网站的链接,但我我不确定我是否可以在这里发布,所有故事都仅供成人使用...但是,如果我发布链接,您将能够看到有效的方法,谢谢您的回答,但它并没有解决我的问题我在第 2 行发现重复的代码并将其删除,再次感谢您将我指向第 2 行!
    • 这两个答案对我都有帮助,你的答案更详细,它确实帮助我编写了 EDIT 3 中可用的最终代码,最后 storycategory 已更改为$storypagename我需要在每个页面的每个元标记文件中定义它,所以我仍然需要为最后500页写500行,谢谢你回答这个问题,我接受你的回答,它解决了一半的问题,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 2011-10-03
    • 2015-06-05
    • 2022-01-31
    • 2023-04-02
    • 2017-02-25
    相关资源
    最近更新 更多