【问题标题】:Undefined variable on index.phpindex.php 上的未定义变量
【发布时间】:2016-01-09 21:59:48
【问题描述】:

我一直在调试一个 php 站点,该站点在页面标题 navsection 的顶部实现了一个导航栏和一个名为 sidenavcat 的侧导航栏。在运行时,我的导航部分显示:

Notice: Undefined variable: navsection in 
/Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 25
Hospitality 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 28
Sustainability 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 31
Events 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 34
Press 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 37
Catalogues 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 40
Locations 
Notice: Undefined variable: navsection in /Applications/XAMPP/xamppfiles/htdocs/NOALivingWebsite/header.php on line 43
Shop Online

header.php中对应的行如下

<li <?php echo ($navsection == 'hospitality') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/hospitality.php">Hospitality</a>
</li>
<li <?php echo ($navsection == 'sustainability') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/sustainability.php">Sustainability</a>
</li>
<li <?php echo ($navsection == 'events') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/events.php">Events</a>
</li>
<li <?php echo ($navsection == 'press') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/press.php">Press</a>
</li>
<li <?php echo ($navsection == 'catalogue') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/catalogue.php">Catalogues</a>
</li>
<li <?php echo ($navsection == 'retail') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/code/locations.php">Locations</a>
</li>
<li <?php echo ($navsection == 'register') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/index.php">Shop Online</a>
</li>

您可以提供任何建议,我们将不胜感激。感谢您的宝贵时间

【问题讨论】:

  • 你在哪里定义了那个变量? $navsection
  • 我在一些人后面工作,所以我确定这就是需要添加的内容,但是否应该在同一个 .php 文件中声明?

标签: php apache


【解决方案1】:

错误消息不是直截了当的。在执行 ternary 运算符时,未定义变量 $navsection。 一种可能的解决方案是将该变量定义为 Session 变量并像这样访问它:

$_SESSION["navsection"]

所以您的代码将如下所示:

<li <?php echo ($_SESSION["navsection"] == 'register') ? ' class="current"' : ''; ?> >
   <a href="http://localhost/NOALivingWebsite/index.php">Shop Online</a>
</li>

注意:确保您的 session 变量 navsection 在上述检查之前已定义,可能在另一个 .php 文件中。

您还需要在 html 文件中使用 this 之一。这应该像这样在您的 html 文件的顶部使用:

<?php
  session_start( );
  $_SESSION["navsection"] = "register"; // Temporary fix.
?>
<html>
.
.
.

这样你就可以访问你的会话变量了。

【讨论】:

  • 在我的 index.php 中开始会话但是我应该在哪里声明变量?
  • 这取决于你。您正在检查变量的特定值。这个值应该在检查之前的某个地方声明。它可能在 session_start(); 之后在同一个文件或另一个文件中。无论哪种方式,会话变量都将可用。根据您的设计,您可以选择您想要的。
  • 感谢您的帮助,仍然没有运气
  • 您希望变量 $navsection 保持什么?它是硬编码值还是使用文本框或其他东西动态设置?
  • 我在任何地方都没有看到它,这就是为什么自从开始这项工作以来我一直在挠头。我需要这个页面来显示
猜你喜欢
  • 1970-01-01
  • 2018-05-13
  • 2015-09-11
  • 1970-01-01
  • 2022-10-08
  • 2011-06-11
  • 1970-01-01
  • 2021-02-27
  • 2015-06-01
相关资源
最近更新 更多