【发布时间】: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 • ' . $pagina . ' • ' . $storycategory . ' • ' . $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 • ' . $pagina . ' • ' . $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 • ' . $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 • ' . $pagina . ' • ' . $storycategory . ' • ' . $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 • ' . $pagina . ' • ' . $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 • ' . $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 代码更小。
我要感谢 mudasobwa 和 Konstantin,这两个答案都有助于找到解决此错误的方法。 现在所有 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 • ' . $pagina . ' • ' . $storycategory . ' • ' . $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 • ' . $pagina . ' • ' . $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 • ' . $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